Rails在嵌套attirbutes的show页面中形成

时间:2018-03-29 12:24:19

标签: ruby-on-rails

我有模型ProjectProjectLine

projects/show我有:

<%= @project.name %>

<% @project_lines.each do |project_line| %>

  <%= project_line.description %>

  <%= semantic_form_for project_line do |f| %>
    <%= f.hidden_field :status, :value=>4 %>
    <%= f.submit 'COMPLETE', class: 'btn btn-primary btn-md' %>
  <% end %>
<% end %>

我想要做的是,在项目展示页面上,显示项目及其项目行,并有一个按钮,将project_line的状态更改为&#39;完成&# 39;

project.rb

has_many :project_lines
accepts_nested_attributes_for :project_lines, reject_if: :all_blank, allow_destroy: true

project_line.rb

belongs_to :project

在projects_controller中:

def show
  @project = Project.includes(:user).find_by_id(params[:id])
  @project_line = ProjectLine.includes(:user).find_by_id(params[:id])
  @project_lines = @project.project_lines
end

但它返回:

ActionView::Template::Error (undefined method `project_line_path' for #<#<Class:0x007f8235644f60>:0x0055eb72a0d4b8>
Did you mean?  project_path
               projects_path):
    109:               </div>
    110: 
    111:               <div class="col-sm-2">
    112:                 <%= semantic_form_for project_line do |f| %>
    113:                     <%= f.hidden_field :status, :value=>4 %>
    114:                   <%= f.submit 'COMPLETE', class: 'btn btn-primary btn-md' %>
    115:                 <% end %>

app/views/projects/show.html.erb:112:in `block in _app_views_projects_show_html_erb___4141078949623736805_47234864376780'
app/views/projects/show.html.erb:93:in `_app_views_projects_show_html_erb___4141078949623736805_47234864376780'
^[[Started POST "/__web_console/repl_sessions/440752809903b2f394f0a107e957670f/trace" for 127.0.0.1 at 2018-03-29 20:55:44 +0800

我该如何解决?

2 个答案:

答案 0 :(得分:0)

错误的含义是您没有与project_line_path对应的路由 - 这通常与update控制器中的project_lines操作相对应。

这是一个Rails默认设置,其中包含一些约定,用于将表单提交到非持久化实例的create操作,update用于保留。

因此,您的问题是:

  • 您错过了控制器和处理表单的操作
  • 您希望表单提交到其约定以外的其他地方

要解决其中的第一个,请创建一个product_lines控制器,其中包含根据需要处理表单的update操作,以及此路由(即resources :product_lines, only: :update,可能嵌套在产品路线)。

对于后者,您可以向表单助手提供url参数,告诉它提交到哪里,即<%= semantic_form_for project_line, url: your_custom_path do %>...

最后,如果您想使用Formtastic的嵌套表单功能,您需要以下内容:

<%= @project.name %>

<%= semantic_form_for @project do |f| %>
  <% @project_lines.each do |project_line| %>

    <%= project_line.description %>

    <%= f.semantic_fields_for project_line do |project_line_form| %>
      <%= project_line_form.checkbox :status, value: 4, class: 'btn btn-primary btn-md' %>
    <% end %>
  <% end %>

  <%= f.submit %>
<% end %>

请注意,这会使用嵌套产品系列的复选框,这些产品系列将与项目表单一起提交。我不确定这是你所采用的方法,所以会建议第一种方法之一,但我想如果你提到使用嵌套表格,我会把它弹出来。

答案 1 :(得分:0)

我用这种方式解决了,基本上是使用链接而不是表单。

路由

resources :projects do
  resources :project_lines do
    post 'accepted', :on => :member
  end
end

project_lines_controller

  def accepted
  @project_line = ProjectLine.find_by_id(params[:id])
      @project_line.update(status: "4")
      @project_line.save!
      redirect_to project_lines_path
      flash[:notice] = "MARKED AS ACCEPTED"
end

项目/显示

  <%= link_to "Accept", accepted_project_project_line_path(project_line.project, project_line), method: :post, data: { confirm: "Are you sure?" } %>