not able to render form in rails association

守給你的承諾、 提交于 2020-01-26 03:54:28

问题


i created a project scaffold that has many to one association with stage scaffold now i created a task scaffold that has many to one association with stage scffold. but i task form is not rendered i am getting error.

routes.rb

  resources :projects do
    resources :stages do
        resources :tasks
    end
  end

Task form.html.erb

<%= form_with(model: task, url: [@stages, task], local: true) do |form| %>
  <% if task.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(task.errors.count, "error") %> prohibited this task from being saved:</h2>

      <ul>
      <% task.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field columns large-6">
    <%= form.label :task_name %>
    <%= form.text_field :task_name %>
  </div>

  <div class="actions">
    <%= form.submit 'Create', :class=>'button primary small' %>
  </div>
<% end %>

model project.rb

  has_many :stages

model stage.rb

  belongs_to :project
  has_many :tasks

model task.rb

  belongs_to :stage

回答1:


Rails can't find tasks_path in your application, because you moved tasks under projects and stages resources, so full path to tasks_path looks like projects_stages_tasks_path

So, you need to specify this url for given projects and stages

<%= form_with(model: task, url: projects_stages_tasks_path(@stages, @stages.project), local: true) do |form| %>


来源:https://stackoverflow.com/questions/59841158/not-able-to-render-form-in-rails-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!