How should I use rails and simple_form for nested resources?

纵然是瞬间 提交于 2019-11-27 23:34:04
Bigxiang

Use f.simple_fields_for instead of simple_fields_for:

<%= f.simple_fields_for :profile do |p| %>
    <%= p.input :name %>
<% end %>

In my case I had the object "book" which belongs to "tour" and "tour" has_many "books".

In the "BookController" in the method "new" I find the tour and initialize the book object:

@tour = Tour.find(params[:tour_id])

@book = Book.new

This is the partial form to create a book: _form.html.erb

<%= simple_form_for [@tour, @book] do |f| %>
  <%= f.input :name, label: "Name"%>
  <%= f.input :NoReservations, label: "Number of Reservations" %>
  <%= f.input :email, label: "Email" %>
  <h3>Num of available places</h3>
  <%= f.button :submit %>
<% end %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!