I keep getting the following error:
NoMethodError in Students#show
undefined method `student_accounts_path' for
#<#Class:0x007ffe2bf6d688>:0x007ffe2c5e5628>
I am trying to implement a has_one model. I followed the rails guide
that used the :post has_many :comments example and tweaked it a bit.
my routes.rb file looks like this:
resources :students do
resource :account
end
My Students.controller, Account.controller files are attached.
Along with the /app/views/students/show.html.erb file
I have no idea what I'm doing wrong.
Can anyone please help me?
Also please if you want to connect these tables you need a couple of
things, not only nested routes.
You also need:
- has_many relationship in student.rb
- belongs_to relationship in account.rb
- you need to add 1-1 table column to both tables which refers to each
others. Best way to do it with migrations like "rails generate
migration AddStudentIdForAccounts" and AddAccountIdForStudents. Follow
this Active Record Migrations — Ruby on Rails Guides
Fill these, then run "rake db:migrate"
Finally you need the proper nested routes you mentioned above, also
controllers and views in place with well-named methods.
So in short way:
2 tables refers to each other (with 2 migrations),
2 model files refers to each other,
routes.rb,
controllers,
views.
I think you meant has_one instead of 'has_many' (in your first bullet
point for student.rb).
I tried what you suggested, and my routes.rb file has
resource :accounts
I also went back to my original "form_for" text as per my attachment and
I no longer see that NoMethodError (YAY!) that I first posted up.
I figured out why (at least i think i did) my 'insert into accounts'
would delete when I rendered the show.html.erb file after creating an
account.... I had the text ":dependent => :destroy" in the model.rb file
I've updated my models to have that 1-1 relationship.
While I was testing it out, I noticed the following in my server logs...
I think I figured it out.
It was to do with my @student.build_account in the students#show method.
(And as per my original post, I think I had that in my form_for tag).
Because I'm returning to the same page, the foreign key was continually
being 'built'.
My students controller looks likes this:
def show
@student = Student.find(params[:id])
@account = @student.account
if @account.nil?
@account = @student.build_account
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: @student }
end
end
And my accounts controller looks like this:
def create
@student = Student.find(params[:student_id])
@account = @student.create_account(params[:account])
redirect_to student_path(@student)
end
end
And my app/views/students/show.html.erb is attached.