编辑多个记录,控制器&路由

时间:2011-06-30 17:58:50

标签: ruby-on-rails controller routes railscasts

我正在关注railscast 198 http://railscasts.com/episodes/198-edit-multiple-individually,尝试将其更新为rails 3,并且遇到路由错误。路线http://localhost:3000/orders/edit_individual给出了错误:

ActiveRecord::RecordNotFound in OrdersController#show - Couldn't find Order with ID=edit_individual

我已更新并使用了他的rails 2路由

map.resources :products, :collection => { :edit_individual => :post, :update_individual => :put }

到engineyard http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/描述的rails 3约定(根据我的需要更新)

  resources :orders do
    collection do
      post :edit_individual
      put :update_individual
    end
  end

这是我尝试过的:

我已尝试按照以下答案的建议更改路线资源:Adding an action to an existing controller (Ruby on Rails)但仍会显示相同的错误。我已经尝试删除cancan加载并授权资源,'资源:订单'路由条目,以及控制器中的'show'条目,但另一个错误表明它仍在尝试显示ID ='edit_individual'的记录而不是像路线一样对待“edit_individual”。

这是我的路线和控制器,

myapp::Application.routes.draw do

      resources :products

      resources :roles

      devise_for :users #, :controllers => { :registrations => "registrations" }

    #  match 'dashboard' => 'user_dashboard#index', :as => 'user_root'

      resources :orders do
        collection do
          post :edit_individual
          put :update_individual
        end
      end

      resources :orders   


class OrdersController < ApplicationController
    load_and_authorize_resource # cancan method

      def index
      end

      def show
      end

      def edit_individual #from railscast 198
        @orders = current_user.customer_orders
      end

      def update_individual
        @orders = Order.update(params[:orders].keys, params[:orders].values).reject { |p| p.errors.empty? }
        if @orders.empty?
          flash[:notice] = "Orders updated"
          redirect_to orders_url
        else
          render :action => "edit_individual"
        end
      end # ...etc.

我已经删除了cancan方法,但它仍然是罪魁祸首吗?我已经尝试了所有我能想到的东西并且处于死胡同......任何想法?

编辑:

命令提示符的输出:

Started GET "/orders/edit_individual" for 127.0.0.1 at Thu Jun 30 11:19:02 -0700
 2011
  Processing by OrdersController#show as HTML
  Parameters: {"id"=>"edit_individual"}
  ←[1m←[36mOrder Load (0.0ms)←[0m  ←[1mSELECT "orders".* FROM "orders" WHERE "or
ders"."id" = 0 LIMIT 1←[0m
Completed   in 2584ms

ActiveRecord::RecordNotFound (Couldn't find Order with ID=edit_individual):

和我的html中的路线:

    <%= link_to 'Update Payments Received', edit_individual_orders_path %>

和rake路线:

edit_individual_orders POST   /orders/edit_individual(.:format)         {:action=>"edit_individual", :controller=>"orders"}
 update_individual_orders PUT    /orders/update_individual(.:format)       {:action=>"update_individual", :controller=>"orders"}
                   orders GET    /orders(.:format)                         {:action=>"index", :controller=>"orders"}
                          POST   /orders(.:format)                         {:action=>"create", :controller=>"orders"}
                new_order GET    /orders/new(.:format)                     {:action=>"new", :controller=>"orders"}
               edit_order GET    /orders/:id/edit(.:format)                {:action=>"edit", :controller=>"orders"}
                    order GET    /orders/:id(.:format)                     {:action=>"show", :controller=>"orders"}
                          PUT    /orders/:id(.:format)                     {:action=>"update", :controller=>"orders"}
                          DELETE /orders/:id(.:format)                     {:action=>"destroy", :controller=>"orders"}

2 个答案:

答案 0 :(得分:4)

您应该运行“rake routes”并查看它为“edit_individual”操作提供的路线。

也是你的日志

Started GET "/orders/edit_individual" for 127.0.0.1 at Thu Jun 30 11:19:02 -0700

说你正在调用post post作为get。

尝试以下

  resources :orders do
    collection do
      get :edit_individual
      put :update_individual
    end
  end

或者你可以使用

<%= link_to 'Update Payments Received', edit_individual_orders_path, :method => "post" %>

答案 1 :(得分:0)

所以错误消息:OrdersController #show - 找不到ID = edit_individual的订单告诉我,无论出于什么原因你被路由到'show'动作,这本身就是期待像

/orders/1

意思是它不是集合的成员。您确定服务器输出中的网址与/ orders / edit_individual匹配吗?