Rails:在嵌套资源的索引页面中添加表单

时间:2016-08-21 23:55:39

标签: ruby-on-rails

模型:

订单 has_many 材料
材料 belongs_to 订单

materials_contrller.rb

 def index
    @order = Order.find_by_id(params[:order_id])
    @materials = @order.materials
  end

def update
    @order = Order.find_by_id(params[:order_id])
    @material = @order.materials.find_by_id(params[:id])

    if @material.update_attributes(material_params)
      redirect_to order_materials_path(@order)
    else
      render 'edit'
    end 
end

我想在材料索引页面中添加一个表单, index.html.erb

    <% @materials.each do |material| %>
                <%= form_for material, url: {controller:"materials", action: "update"} do |f| %>
                <%= f.check_box :material_urgent %>
                <%= f.submit %>
                <% end %>
<%end%>

但是当我在页面上尝试时出现错误 http://localhost:3000/orders/1/materials

ActionController::UrlGenerationError in Materials#index
No route matches {:action=>"update", :controller=>"materials", :order_id=>"1"}

有人可以帮忙吗?谢谢!

0 个答案:

没有答案