Rails 使用嵌套资源将 Edit/Update 方法重定向到 Create 方法

Rails with nested resources redirect Edit/Update method to Create method

使用嵌套资源处理 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 %>

行项目 - 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

点击"Edit"link时,跳转到url,如:

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

那么,为什么在 Edit 页面中,Edit/Update 重定向到 Create 方法? 任何人都可以帮忙吗? 谢谢!

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

resources :models do
  resources :lineitems
end

另外 运行 rake routes 从控制台查看正在形成的路由。

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