开发者

Display data in form from 2 tables in Ruby on Rails 3

开发者 https://www.devze.com 2023-03-10 06:23 出处:网络
I used nifty scaffolding for create controller \'A\'. For this controller I have in DB table(again called \'A\').

I used nifty scaffolding for create controller 'A'. For this controller I have in DB table(again called 'A').

I have view, in which is a form for entering information. These data will be stored in database. But was request have in this form input that isn't in table 'A' (column), but in table 开发者_如何学JAVA'B'.

<%= form_for @a do |u| %>
  <%= u.error_messages %>
  <p>
    <%= u.label :name %><br />
    <%= u.text_field :name %>
  </p>
  <p>
    <%= u.label :age %><br />
    <%= u.text_field :age %>
  </p>
  <p>
    <%= u.label :email %><br />
    <%= u.text_field :email %>
  </p>
...

Input called 'name' will be stored in table 'A' (into column 'name'), input called 'age' will be stored in table 'B' (into column 'age').

I want display this form in browser, I am getting error:

undefined method `age' for...

Can you help me, please, what is needed for this situation for right displaying the form?


Use the fields_for helper Rails provides.


In your code you have done some mistake because as your code all the fields should be at the same table. But actually you have fields with different tables. So that's why it shows that error message.


You can use fields_for to save age in "B", but this will slowdown your loading process only ,you not need to create such extra table.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号