2015-11-01 77 views
1

这是我在路线路由文件的ActionController :: UrlGenerationError在帐户#计划

get '/signup/:plan/:discount' =>'accounts#new', plan: nil, discount: 0, as: :new_account 

而且我打电话我的html文件,这个路线如下图所示

<a href="<%= new_account_url('$129', params[:discount]) %>" class="signup"><img src="/images/sign-up.png"></a> 

后耙航线运行命令后端然后我得到以下

new_account GET /signup/:plan/:discount(.:format) 
    accounts#new {:plan=>nil, :discount=>0} 

但我不知道它给出的路线有什么问题我错误,如

No route matches {:action=>"new", :controller=>"accounts", :discount=>nil, :format=>nil, :plan=>"$129"} missing required keys: [:discount] 
+0

'params [:discount]'的价值是什么? – Pavan

+0

也尝试将路线中的折扣:零折扣改为折扣:零。 – Pavan

+0

我也尝试打折作为零,但仍然无法正常工作。和params [:折扣]是不是强制性的价值时,那么它取代零或零。 –

回答

1

您的params[:discount]缺少一个值,因此在您的路由中调用它时ES,它没有被分配:

{:action=>"new", :controller=>"accounts", :discount=>nil, :format=>nil, :plan=>"$129"} 

既然你已经做出:discount路线的必需部分,你需要调用帮助时的值传递给它。


要做的最重要的事情是确保您的params[:discount]变量已填充。如果没有,你不妨用bound parameters

get '/signup/:plan(/:discount') =>'accounts#new', discount: 0, as: :new_account 

这将使:discount参数非必需。


另外,每当创建一个链接时,您应该始终使用link_to。它使你的HTML最新且符合规格:

<%= link_to new_account_path("$129", params[:discount]), class: "signup" do %> 
    <%= image_tag "sign-up.png" %> 
<% end %> 

-

你还需要确保你自己适当的资源的范围内设置你的路由:

#config/routes.rb 
resources :accounts, only: [:new], path_names: { new: "signup" } do 
    get ":plan(/:discount)", on: :new 
end 
2

当你有一个网址'/signup/:plan/:discount',然后:plan:discount都需要的关键。您不能将这些密钥的值设为nil。如果你有,你会一直得到你现在的错误。

由于这些值是可选的,我的建议是将它们作为查询参数发送,并在控制器内部进行检查。

尝试重组路线定义:

get '/signup' =>'accounts#new', as: :new_account 

你也可以如下使用link_to重写URL:

<%= link_to new_account_path(plan: '$129', discount: params[:discount]), class: "signup" do %> 
    <img src="/images/sign-up.png" /> 
<% end %> 

现在,你会一直网址,如:

/signup?plain="xx"&discount="xx" 
相关问题