Ruby on Rails Sunday, April 1, 2012

Hi Ayesha,

it might be a typo in your routes:

resource :accounts insted of resourceS :accounts

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 http://guides.rubyonrails.org/migrations.html
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.

HTH!
YogiZoli

PS. Highly recommend you this tutorial instead of Guides first:
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book?version=3.2
this helped me a lot in understanding full MVC in action!


On Apr 1, 9:06 am, ayesha fernando <li...@ruby-forum.com> wrote:
> Thanks for your tips Bente,
>
> I ran rake routes, and this is what I got:
> student_account POST   /students/:student_id/account(.:format)
> accounts#create
>
> I thought my routes.rb file HAD to be like this:
> resources :students do
>   resource :account
> end
>
> because my Student model only 'has_one' Account (not a has_many)
>
> I changed my form_for to:
> form_for ([@student, @student.build_account], :url =>
> student_account_path(@student))
>
> And I am no longer getting the 'NoMethodError' BUT the account
> information is not being saved to the accounts table.
>
> What key concept am I not understanding?
>
> --
> Posted viahttp://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment