编辑多个记录、控制器和路由

发布于 2024-11-17 17:18:21 字数 4205 浏览 2 评论 0 原文

我正在关注 Railscast 198 http://railscasts.com/episodes/198-edit-multiple - 单独,尝试将其更新到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 }

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

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

这是我尝试过的:

我尝试按照答案的建议更改路线资源:<一个href="https://stackoverflow.com/questions/401241/adding-an-action-to-an-existing-controller-ruby-on-rails/401340#401340">向现有控制器添加操作(Ruby on Rails) 但仍然显示相同的错误。我尝试删除 cancan 加载和授权资源、“资源:订单”路由条目以及控制器中的“显示”条目,但另一个错误表明它仍在尝试显示 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"}

I am following railscast 198 http://railscasts.com/episodes/198-edit-multiple-individually, trying to update it to rails 3, and am stuck with a routing error. The route http://localhost:3000/orders/edit_individual gives me the error:

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

I have updated and used his rails 2 routing

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

to the rails 3 convention as described by engineyard http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ (updated to my needs)

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

Here's what I've tried:

I've tried changing the routes resources as suggested by the answer to: Adding an action to an existing controller (Ruby on Rails) but the same error still shows. I've tried removing the cancan load and authorize resources, the 'resources :orders' route entry, as well as the 'show' entry in the controller, but another error indicates it's still trying to show a record with ID='edit_individual' instead of treating "edit_individual" like a route.

Here are my routes and controller,

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.

I've removed the cancan methods, but is it the culprit still? I've tried everything I can think of and am at a dead end.. any ideas?

edit:

the output from command prompt:

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):

and the route in my html:

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

and rake routes:

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"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-11-24 17:18:21

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

另外,您的日志

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

表明您正在将后操作调用为 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" %>

You should probably run "rake routes" and see what route it gives for "edit_individual" action.

Also your log

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

says that you are calling a post action as get.

Try below

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

OR either way you can use

<%= link_to 'Update Payments Received', edit_individual_orders_path, :method => "post" %>
迷路的信 2024-11-24 17:18:21

因此,错误消息:OrdersController#show - Couldn't find Order with ID=edit_individual 告诉我,无论出于何种原因,您都会被路由到“show”操作,该操作本质上期望类似于

/orders/1

表示它是成员而不是集合的内容。您确定服务器输出中点击的 url 与 /orders/edit_individual 匹配吗?

So the error message: OrdersController#show - Couldn't find Order with ID=edit_individual tells me that for whatever reason you're being routed to the 'show' action, which is inherently expecting something like

/orders/1

meaning it's a member not a collection. Are you certain that the url being hit in your server output matches /orders/edit_individual?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文