具有嵌套资源的Rails将Edit / Update方法重定向到Create方法

时间:2015-02-07 10:32:29

标签: ruby-on-rails redirect ruby-on-rails-4 nested-attributes

使用嵌套资源处理Rails时,使用重定向到Create方法的Edit / Update方法时出现问题。 以下是我的演示:

模型

class Model < ActiveRecord::Base
    has_many :lineitems, dependent: :destroy
end

class Lineitem < ActiveRecord::Base
  belongs_to :model
end

路线

#resources :lineitems
#resources :models

resources :models do
  resources :lineitems
end

耙路线

            lineitems GET    /lineitems(.:format)lineitems#index
                      POST   /lineitems(.:format)                                    lineitems#create
         new_lineitem GET    /lineitems/new(.:format)                                lineitems#new
        edit_lineitem GET    /lineitems/:id/edit(.:format)                           lineitems#edit
             lineitem GET    /lineitems/:id(.:format)                                lineitems#show
                      PATCH  /lineitems/:id(.:format)                                lineitems#update
                      PUT    /lineitems/:id(.:format)                                lineitems#update
                      DELETE /lineitems/:id(.:format)                                lineitems#destroy
               models GET    /models(.:format)                                       models#index
                      POST   /models(.:format)                                       models#create
            new_model GET    /models/new(.:format)                                   models#new
           edit_model GET    /models/:id/edit(.:format)                              models#edit
                model GET    /models/:id(.:format)                                   models#show
                      PATCH  /models/:id(.:format)                                   models#update
                      PUT    /models/:id(.:format)                                   models#update
                      DELETE /models/:id(.:format)                                   models#destroy
      model_lineitems GET    /models/:model_id/lineitems(.:format)                   lineitems#index
                      POST   /models/:model_id/lineitems(.:format)                   lineitems#create
   new_model_lineitem GET    /models/:model_id/lineitems/new(.:format)               lineitems#new
  edit_model_lineitem GET    /models/:model_id/lineitems/:id/edit(.:format)          lineitems#edit
       model_lineitem GET    /models/:model_id/lineitems/:id(.:format)               lineitems#show
                      PATCH  /models/:model_id/lineitems/:id(.:format)               lineitems#update
                      PUT    /models/:model_id/lineitems/:id(.:format)               lineitems#update
                      DELETE /models/:model_id/lineitems/:id(.:format)               lineitems#destroy
                      GET    /models(.:format)                                       models#index
                      POST   /models(.:format)                                       models#create
                      GET    /models/new(.:format)                                   models#new
                      GET    /models/:id/edit(.:format)                              models#edit
                      GET    /models/:id(.:format)                                   models#show
                      PATCH  /models/:id(.:format)                                   models#update
                      PUT    /models/:id(.:format)                                   models#update
                      DELETE /models/:id(.:format)                                   models#destroy

模型 - show.html.erb

<% @model.lineitems.each do |li| %>
  <tr>
    <td><%= li.tLineitemNumber %></td>
    <td><%= link_to 'Show', model_lineitem_path(@model, li) %></td>
    <td><%= link_to 'Edit', edit_model_lineitem_path(@model, li) %></td>
  </tr>
<% end %>

Lineitem - edit.html.erb

<%= form_for ([@model, @model.lineitems.build]) do |f| %>
  <p>
    <%= f.label :tLineitemNumber, "Line Item Number / Level" %><br>
    <%= f.text_field :tLineitemNumber %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

lineitems_controller.rb

class LineitemsController < ApplicationController
  def create
    @model = Model.find(params[:model_id])
    @lineitem = @model.lineitems.create(lineitem_params)
    if @lineitem.save
      redirect_to model_path(@model)
    else
      render 'new'
    end 
  end

  def edit
    @model = Model.find(params[:model_id])
    @lineitem = @model.lineitems.find(params[:id])
  end

  def update
    @model = Model.find(params[:model_id])
    @lineitem = @model.lineitems.find(params[:id])
    if @lineitem.update(lineitem_params)
        redirect_to model_path(@model)
    else
        render 'edit'
    end
  end

  private
    def lineitem_params
      params.require(:lineitem).permit(:model_id, :id, :tLineitemNumber)
    end
end

点击&#34;编辑&#34;链接,跳转到网址:

http://localhost/models/2/lineitems/16/edit

检查服务器日志,得到这些:

Started GET "/models/2/lineitems/16/edit" for 192.168.32.2 at 2015-02-07 18:24:18 +0800
Processing by LineitemsController#edit as HTML
  Parameters: {"model_id"=>"2", "id"=>"16"}
  Model Load (0.1ms)  SELECT  `models`.* FROM `models`  WHERE `models`.`id` = 2 LIMIT 1
  Lineitem Load (0.1ms)  SELECT  `lineitems`.* FROM `lineitems`  WHERE `lineitems`.`model_id` = 2 AND `lineitems`.`id` = 16 LIMIT 1
  Rendered lineitems/_form.html.erb (2.9ms)
  Rendered lineitems/edit.html.erb within layouts/application (4.1ms)
Completed 200 OK in 238ms (Views: 195.7ms | ActiveRecord: 2.8ms)


Started POST "/models/2/lineitems" for 192.168.32.2 at 2015-02-07 18:26:43 +0800
Processing by LineitemsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"oCGgftEl9emkAhKEkA/NqjXO6qH7nVE7FC9ohZpjmjI=", "lineitem"=>{"tLineitemNumber"=>"bbbb"}, "commit"=>"Create Lineitem", "model_id"=>"2"}
  Model Load (0.1ms)  SELECT  `models`.* FROM `models`  WHERE `models`.`id` = 2 LIMIT 1
   (0.3ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `lineitems` (`created_at`, `model_id`, `tLineitemNumber`, `updated_at`) VALUES ('2015-02-07 10:26:43', 2, 'bbbb', '2015-02-07 10:26:43')
   (62.3ms)  COMMIT
   (0.3ms)  BEGIN
   (0.1ms)  COMMIT
Redirected to http://localhost/models/2

那么,为什么在编辑页面中,编辑/更新重定向到Create方法? 有人可以帮忙吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

您是否定义了两次路由,因为这可能会导致在rails中出现问题。您只应该只定义一次。

resources :models do
  resources :lineitems
end

还从控制台运行rake routes并查看正在形成的路径。

实际上当你使用这个@model.lineitems.build时,你正在重新分配lineitems对象,而你应该使用你在控制器中找到的@lineitem。因此,您需要在编辑中使用@lineitem