Rails passing form_for object to partial

前端 未结 3 883
情歌与酒
情歌与酒 2020-12-29 03:03

I would like to pass the form_for object to a partial:

<%= form_for @price do |f| %>
   ...
   <%= render :partial => \"price_page\"         


        
3条回答
  •  萌比男神i
    2020-12-29 03:53

    I ran across this question trying to figure out how to get a form builder into a partial without an additional form tag. That's the primary use case I could think of for this question, so I'm adding this answer for future visitors.

    To solve my problem, I have my form_for in my layout and I render my partial passing only the model. In my partial I use fields_for.

    Looks (something) like this:

    = form_for @price do |f|
       ...
       = render partial: "price_page", object: @price, as: 'price %>
       ...
    

    Then, my partial has this:

    = fields_for price do |f|
       ...
    

提交回复
热议问题