【问题标题】:undefined method `protect_against_forgery?' for #<#<Class:0x0未定义的方法`protect_against_forgery?对于#<#<类:0x0
【发布时间】:2013-05-21 14:19:57
【问题描述】:

我的 routes.rb 文件中有以下代码。

resources :users  do
  member do
    get :following,:followers
  end
  collection do
    put :activate_email
  end
 end

我有一个这样的用户电子邮件激活链接:

<%= link_to "Activate",activate_email_users_url(email_token: @user.email_token),method: :put  %>

当我点击激活链接时,这是生成的 url

 http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ

更新:好的,所以我想我知道问题出在哪里。当我在我的 gmail 中查看包含 link_to 的激活电子邮件的 html 源时,没有

data-method='put'
。所以这似乎是问题所在。它总是发送一个默认的 GET 请求而不是 PUT。 这是我的 user_mailer/registration_confirmation.html.erb 文件
  <%= javascript_include_tag "application" %>
</head>

请点击以下链接激活您的邮箱

这会产生以下错误:

未定义的方法`protect_against_forgery?'为了 #

所以,代码

导致了这个错误。有没有办法解决 ?

【问题讨论】:

  • 澄清一下,您想要post 还是put,因为您的问题中都有它们。
  • put 用于我们要更新任何数据以便使用 put 您需要创建 form 而不是 linkput 作为方法
  • 对不起..我想要 'put' 。但是“发布”也不起作用。刚刚编辑了问题。
  • 试试&lt;%= link_to "Activate",activate_email_users_url(email_token: @user.email_token, _method: :put), method: :post %&gt;put :activate_email
  • 第一件事。您的控制台日志中给出了哪些错误。

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2


【解决方案1】:

对不起,我不知道你的目的,但显然你有激活用户的目的。 试试这个,如果这个解决方案不起作用,请告诉我你在控制器上的操作(activate_email)!

查看rake routes 输出:

activate_email_users PUT /users/activate_email(.:format) users#activate_email user GET /users/:id(.:format) users#show

当你生成时

http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ

您的问题是activate_email 被认为是:id

users/activate_email =&gt; users/:id

您的问题的解决方案:

尝试从链接中删除method。最好在路由文件中指定method。将路由替换为:

resources :users  do
  member do
    get :following,:followers
  end
end
put "/users/activate_email/:email_token" => "users#activate_email", :as => "activate"

正在观看

<%= link_to "Activate", activate_path(:email_token => @user.email_token)  %>

我还没有测试过,但我想这已经足够了。

更新

问题:未定义的方法`protect_against_forgery?'

将此添加到只有您的邮件模板使用的帮助程序中:

 def protect_against_forgery?
      false
 end

注意:如果您有新问题,请创建新的“”并同意答案对这个问题很有用

【讨论】:

  • 我在 gmail 中的激活电子邮件中没有显示数据方法和其他 Rails 内置 html,例如“data-method='put'”。
  • 感谢您的帮助,我已经更新了我的问题。您能看看吗?
  • 我更新了我的答案,如果您有新问题,请不要编辑此问题,您可以创建新的“”并批准有用的答案..
  • 对不起,我不知道规则。将protect_against_forgery设置为false是否安全?
  • 我应该在哪里定义protect_against_forgery?
【解决方案2】:

如果您尝试激活单个用户帐户,您可能不想在集合上指定您的路由(您将用于对多个用户进行操作的操作)。

这里有一些(未经测试的)代码可以为您指明正确的方向:

controller :users do
    put '/activate/:email_token', :to => :activate,  :as => 'activate_email'
end

应该将 PUT 路由到 /activate/xxxxUsersController#activate 操作,并将 params[:email_token] 设置为 xxxx。它还应该为您提供一个 #activate_email_url 路由,您可以通过该路由传递激活令牌(您可以通过在命令行上运行 rake routes 来检查您的应用提供的路由)。

【讨论】:

  • 我试过了:put '/activate/:email_token', :to => :activate_email, :as => 'activate_email' 但它仍然路由到 'show' 动作。
  • 能否把routes.rbrake routes的相关部分贴出来。你也读过Rails Routing Guide吗?
  • 我也添加了 routes.rb 文件。是的,我已经阅读了路由指南,但我不太擅长(如您所见)。
【解决方案3】:

Google 将我重定向到这个问题,尽管我的问题与将模板呈现为字符串有关,而不仅仅是在浏览器中。我对模板问题的解决方案是这样的:

  action_controller = ActionController::Base.new()

  action_controller.class_eval do
    def protect_against_forgery?
      false
    end
  end

  file_string = action_controller.render_to_string('/some_template/template_file',locals: { local_variable: 1 }

【讨论】:

    猜你喜欢
    • 2020-05-18
    • 2014-11-22
    • 2015-08-21
    • 2013-11-11
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多