[英]url_for does not work with custom route
I have a custom route that looks like: 我有一条自定义路线,如下所示:
match 'firsttime' => 'registrations#new', :via => :get, \
:defaults => {:promotion_path => :firsttime}
The goal with the above route is to be able to have a url like http://www.mysite.com/firsttime which maps to the "new" method for the registrations controller, with the promotion for that registration being the "firsttime" promotion. 上述路线的目标是能够拥有一个网址,例如http://www.mysite.com/firsttime ,该网址映射到注册控制器的“新”方法,而该注册的升级是“首次”晋升。
In one of my models, I have a shortcut method to try and generate this url: 在我的一个模型中,我有一个尝试生成此url的快捷方法:
class Registration < ActiveRecord::Base
include ActionView::Helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers
belongs_to :promotion
def get_share_link()
promotion_path = promotion.url_path
url_to_share = url_for :controller => 'registrations', :action => 'new', :promotion_path => promotion_path
end
end
Calling the method get_share_links() fails with the error: 调用方法get_share_links()失败,并显示以下错误:
No route matches {:controller=>"registrations", :action=>"new", :promotion_path=>"firsttime"} 没有路由匹配{:controller =>“ registrations”,:action =>“ new”,:promotion_path =>“ firsttime”}}
What am I doing wrong, or am I even using the right method for this? 我在做什么错,还是为此使用了正确的方法?
routes are used in the Controller. 控制器中使用了路由。 Stick this as a before filter in your controller instead. 请将其作为前置过滤器放置在控制器中。
Is this devise or another gem for authentication? 这是用于身份验证的设备还是其他工具?
Can you get to http://www.mysite.com/registrations/new?promotion=firsttime
presuming it's localhost for now of course, since you are in dev mode. 当然,由于您处于开发人员模式,因此您现在可以转到http://www.mysite.com/registrations/new?promotion=firsttime
假定它现在是localhost。
What do you mean by "In one of my models, I have a shortcut method to try and generate this url:" what are you generating this for and where is it used? “在我的一个模型中,我有一个尝试生成此url的快捷方法:”的意思是什么?生成该文件的目的是什么?在何处使用它? mailings? 邮件? It seems your route works, it's just not being made when you want it to, and it's unclear where you want it to be used. 看来您的路线有效,只是在您需要时才制作,并且不清楚您要在哪里使用它。
does it work with only_path: false
supplied as an option? 它与only_path: false
提供吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.