开发者

new/edit actions on show page :: best practice question

i am designing a das开发者_StackOverflowhboard page that summarizes data from multiple models, all on one page. and i also want to include forms on that page for creating new items.

after some experimenting, i realized i needed some advice as to the best practice for this situation... here are the 2 methods i was attempting

1) using nested field_fors...

form_for @job do |j|
  j.fields_for :invoice do |i|
    i.fields_for :charges do |c|
      c.text_field :amount

or 2)

form_for @job.invoice.charges.build do |c|
  c.text_field :amount

with the nested approach, the problem is now i have a new object which throws a wrench in my show view when it iterates through existing objects. suddenly trying to render all the created_at times fails since the new object hasn't been saved yet.

the problem with approach 2) is either i post each form to its own controller to handle it, but if it doesnt validate, i need to render the show view... which means i have to provide each controller i have a form for with all the instance variables and methods etc to render the page.

and i havent quite figured out how the error handling works. sometimes my form is marked with fieldWitherrors class, and sometimes it doesnt. i have been trying out both approaches but am having trouble figuring it out.

can anyone offer advice on the direction i should go here?


If new charge is all you need, then the latter approach is the way. Note that these do basically the same:

form_for @job.invoice.charges.build do |c|

and

@charge = Charge.new(:invoice_id => @job.invoice.id)
form_for @charge do |c|

Using this second case you don't connect the new charge with the job until it's saved and if you move the first line with @charge=... to a controller, you can re-render the form when validations fail.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜