Reputation: 225
I'm trying to achieve a simple 1 to 1 relation in Rails 3 where a user can connect a bank account.
class User < ActiveRecord::Base
has_one :bank
accepts_nested_attributes_for :bank
attr_accessible :bank_attributes
end
class Bank < ActiveRecord::Base
belongs_to :user
end
Route
resources :users do
resources :bank
Now when i build a new bank object for a user in users/1/bank/new like this:
def new
@user = User.find(current_user.id)
@bank = @user.build_bank
end
I get an error on my for which looks like this:
<%= simple_form_for(@bank) do |f| %>
The error is:
undefined method `banks_path' for #<#<Class:0x007fa7bd090f08>:0x007fa7c0545b40>
My goal is to have a separate form for a user to add there bank account information.. Hope someone can help me in the right direction to do this. I also use ActiveAdmin and the relation with forms etc works fine there.
Any help is appreciated!
Upvotes: 1
Views: 103
Reputation: 7616
You need to declare resource in plural form regardless of the association type.
So, your resource declaration
resources :users do
resource :banks
end
Upvotes: 0
Reputation: 40277
Since bank is nested under user, you need to give the user to the form:
<%= simple_form_for([@user, @bank]) do |f| %>
In addition, your routes file should be
resources :users do
resource :bank
This will give you a user_bank_path for a user
Upvotes: 1