English 中文(简体)
使用“成员”对习俗资源采取行动的错误
原标题:Routing error on custom resource action using "member do"

这在我的道路上。 rb file:

resources :orders do
  member do
    get  confirm 
    get  cancel 
  end
end

Of course I wrote the confirm and cancel actions in my orders_controller.rb:

def confirm
  @order = Order.find(params[:id])
  #...
end

def cancel
  @order = Order.find(params[:id])
  #...
end

并且在我的“申请/调查/命令/”文件夹中创建了“confirm.html.erb”和“cancel.html.erb”。

但当我试图访问confirm_order_urlcancel_order_url时。 我总是犯这一rou错:

No route matches {:action=>"confirm", :controller=>"orders"}

Can t figure out what s missing! Any idea please?

PS: I m, using RoR v.3.1.0

EDIT#1: It seems the routes are set properly:

$ rake routes
[..]
 confirm_order GET    /orders/:id/confirm(.:format) {:action=>"confirm", :controller=>"orders"}
  cancel_order GET    /orders/:id/cancel(.:format)  {:action=>"cancel", :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"}
[...]

EDIT#2: Maybe when I call confirm_order_url Rails doesn t know how to generate the route which should be like "orders/:id/confirm" because @order.id hasn t been assigned yet. But I am calling this method after @order.save. Namely, here:

  if @order.save
  response = PAYPAL_EXPRESS_GATEWAY.setup_purchase(@order.price_in_cents,
  :ip                => @order.ip_address,
  :return_url        => confirm_order_url,
  :cancel_return_url => cancel_order_url 
  )
  redirect_to PAYPAL_EXPRESS_GATEWAY.redirect_url_for(response.token)

So this leads to a subquestion: is @order.id set after the call to @order.save? And if not, how can I set it properly before calling an helper method such ad confirmation_order_url?

SOLUTION Thanks to Jayendra Patil i fixed my code this way:

  if @order.save
  response = PAYPAL_EXPRESS_GATEWAY.setup_purchase(@order.price_in_cents,
  :ip                => @order.ip_address,
  :return_url        => confirm_order_url(@order),
  :cancel_return_url => cancel_order_url(@order) 
  )
  redirect_to PAYPAL_EXPRESS_GATEWAY.redirect_url_for(response.token)

我错误地认为,铁路可以猜测,而I号令称之为“成员”,因此,答案是把“顺序”作为成员路线的理由。 谢谢。

最佳回答

由此形成了以下路线:

confirm_order GET    /orders/:id/confirm(.:format) {:action=>"confirm", :controller=>"orders"}
cancel_order GET    /orders/:id/cancel(.:format)  {:action=>"cancel", :controller=>"orders"}

您应使用订单标的或与这些路线相仿的:

confirm_order_url(:id => @order.id)
confirm_order_url(@order)

铁路还自动将id转让给护堤后。 援助应当奏效。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签