简体   繁体   中英

Multiple forms for the same model with different associations in Ruby on Rails

I am building a Project management with the following associations:

Project
  - Section
    - Milestone
      - Issue

Inside app/views/section/show.html.erb - I want to have the entire section's milestones and its related issues. For each milestone I want to have a different form to create a new associated issue.

This is how the view file looks like:

    <%= @section.title %>
    <% @milestones.each do |milestone| %>
    <div id="milestone">
    <%= milestone.info %>
    </div>
<% milestone.issues.each do |issue| %>
    <div id="issue">
         <p><%= issue.content %></p>
    </div>
<% end %>

At the end of each milestone I have the following form to add a new issue:

    <%= form_for([@project, milestone.issues.build], :url => project_section_milestone_issue_path ) do |f| %>
        <p>
            <%= f.text_field :content   %>
            <%= f.submit %>
        </p>
<% end %>

This is my routes.rb:

resources :projects do 
  resources :sections do
    resources :milestones do 
      resources :issues
    end
  end
end

The form doesn't work, the page (view file) itself doesn't load and errors with wrong path url. I'm guessing something is wrong either with the routes or with the assignment within the form_for method.

The problem was with the form_for statement.

With @Zippie's hint, I changed it to: <%= form_for([@project, @section, milestone, milestone.issues.build] ) do |f| %> <%= form_for([@project, @section, milestone, milestone.issues.build] ) do |f| %> and it fixed the problem.

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