未为资源定义的路径变量

时间:2021-05-14 23:13:49

I try to change the language of my application from a drop down menu followed by a submit button. I introduced a controller named 'language_change' defined in the route.rb as:

我尝试从下拉菜单中按照提交按钮更改应用程序的语言。我在route.rb中引入了一个名为“language_change”的控制器:

resources :language_change, :only => [:create, :new] 

In the view home.html.erb, I put it this way:

在home.html.erb视图中,我这样说:

<%= form_tag language_change_path, :method=>:post do %>
<div class="field">
    <%= select_tag(params[:l],
       options_for_select(
      [[t('language.english'),'en'],
       [t('language.french'), 'fr'],
       [t('language.dutch'), 'nl']], params[:l]),
       )   
     %>  
 </div>
 <div class="actions"> <%= submit_tag "Change language!" %>
<% end %>

Finally in the controller I put:

最后在控制器中我放了:

class LanguageChangeController < ApplicationController
  def create
    I18n.locale=params[:l]
  end 
end

When loading the home page I get the following error message:

加载主页时,我收到以下错误消息:

undefined local variable or method `language_change_path'

Does anybody can tell me what is wrong? What I don't understand is when in the view I replace the 'language_change_path' by another controller defined elsewhere in the app, eg. 'password_reset_path' it displays the page and even handle the submit.

有谁能告诉我出了什么问题?我不明白的是,在视图中我用应用程序中其他地方定义的另一个控制器替换'language_change_path',例如。 'password_reset_path'它显示页面甚至处理提交。

Thanks for your helage

谢谢你的机身

1 个解决方案

#1


1  

You probably need to change this line in routes.rb:

您可能需要在routes.rb中更改此行:

resources :language_change, :only => [:create, :new]

to this:

对此:

resource :language_change, :only => [:create, :new]

Otherwise, the helper method language_change_path is expecting a specific instance of LanguageChange to link to. By specifying the resource as singular, you don't need to pass an instance to the helper since it's required that only one such instance exists.

否则,辅助方法language_change_path期望链接到的LanguageChange的特定实例。通过将资源指定为单数,您不需要将实例传递给帮助程序,因为它只需要存在一个这样的实例。

I'm guessing your password_reset resource is also singular.

我猜你的password_reset资源也很奇怪。

#1


1  

You probably need to change this line in routes.rb:

您可能需要在routes.rb中更改此行:

resources :language_change, :only => [:create, :new]

to this:

对此:

resource :language_change, :only => [:create, :new]

Otherwise, the helper method language_change_path is expecting a specific instance of LanguageChange to link to. By specifying the resource as singular, you don't need to pass an instance to the helper since it's required that only one such instance exists.

否则,辅助方法language_change_path期望链接到的LanguageChange的特定实例。通过将资源指定为单数,您不需要将实例传递给帮助程序,因为它只需要存在一个这样的实例。

I'm guessing your password_reset resource is also singular.

我猜你的password_reset资源也很奇怪。