简体   繁体   中英

Nested form_for in _form.html.erb

I am trying to understand why for a typical nested resource, why we need to do this

<%= form_for [@project,@ticket] do |f| %>

instead of say:

<%= form_for [@project,@project.ticket] do |f| %> <%= form_for [@project,@project.ticket] do |f| %> which doesn't work, or so on.

Would truly appreciate an explainer on [@project, @ticket] part as I understand it for singular controller, but not nested.

routes.rb

Rails.application.routes.draw do

    root "projects#index"

    resources :projects do 
        resources :tickets 
    end

end

I don't know why must be used this way but, if you are more comfortable, you can use something like:

<%= form_for @project do |p| %>
  ...
  <%= fields_for :ticket, @project.ticket do |t| %>
    ...
  <% end %>
  ...
<% end %>

You can take a look to the FormHelper documentation . It gives examples for every possible association.

I hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM