用户中的NoMethodError#取消订阅 [英] NoMethodError in Users#unsubscribe

查看:133
本文介绍了用户中的NoMethodError#取消订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施取消订阅链接到我的导航邮件程序。不幸的是,我的代码打破了这一点:


用户中的NoMethodError#unsubscribe - 未定义的方法`unsubscribe_hash'for nil:NilClass


指向 /app/views/users/unsubscribe.html.erb 行#3

 < h4>取消订阅Mysite电子邮件< / h4> 
< p>通过取消订阅,您将不会再收到电子邮件...< / p>
<%= simple_form_for(@user,unsubscribe_path(id:@ user.unsubscribe_hash))do | f | %GT;
<%= f.hidden_​​field(:subscription,value:false)%>
<%= f.submit'取消订阅'%>
<%= link_to'取消',root_url%>
<%end%>

我的user_controller如下所示

  class UsersController< ApplicationController 
protect_from_forgery

def new
@user = User.new
end

def create
@user = User。 new(secure_params)
if @ user.save
flash [:notice] =谢谢!您订阅了#{@user.email}作为Jobs Alert。
else
flash [:notice] ='错误订阅! '
end
redirect_to root_path
end

def取消订阅
user = User.find_by_unsubscribe_hash(params [:unsubscribe_hash] )
@user = User.find_by_unsubscribe_hash(user)
end
$ b $ def def
@user = User.find(params [:id])
如果@ user.update(secure_params)
flash [:notice] ='订阅已取消'
redirect_to root_url
else
flash [:alert] ='有问题'
渲染:取消订阅
结束
结束

私有

def secure_params
params.require(:user).permit(:电子邮件,:订阅)
结束

结束

路线。 rb

 资源:users,only:[:new,:create] 
get'users /:unsubscribe_hash / unsubscribe '=> 'users#unsubscribe',as::unsubscribe
patch'users / update'

用户.rb

  class User< ActiveRecord :: Base 

before_create:add_unsubscribe_hash,:add_true_to_users_table

验证:email,:uniqueness => true
validates_presence_of:email
validates_format_of:email,:with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z /我

私人

def add_unsubscribe_hash
self.unsubscribe_hash = SecureRandom.hex
结束

def add_true_to_users_table
self.subscription = true
结束
$ b $结束

取消订阅链接在调用取消订阅动作的电子邮件中

 #app / views / job_notifier / send_post_email.html.erb 
.. 。
<%= link_to取消订阅,unsubscribe_url(id:@unsubscribe)%> ;.

错误图示

< a href =https://i.stack.imgur.com/OoXPw.png =nofollow noreferrer>



似乎我错过了一些东西,我需要在我的users_controller中定义一些东西吗?我从来没有在我的生活中能够解决NoMethodError,或者我不明白它是什么。

解决方案

每当你看到任何错误消息格式的红宝石:

 未定义方法'method_name'为nil:NilClass 

你被告知你正试图调用某个无对象的东西,所以你关注的焦点应该放在为什么该对象为零。你也会在你的日志中告诉错误发生在哪里 - 在你的情况下,它指的是 @user $ uc_cache中的 @user



因此 @user 为零,在这种情况下,它是无,因为负责渲染表单的控制器没有设置 @user

  @user = User.find_by_unsubscribe_hash(user)

现在,为什么你试图找到用户,然后将该用户传递到第二行以找到 @user 超出了我,但无论如何,真正的问题是你没有用户匹配 params [:unsubscribe_hash]



所以这个问题与任何正在调用您的取消订阅操作...您已经忽略了将其添加到您的问题中,所以我无法提供帮助,但那是您开始关注的焦点。


I am working on implementing unsubscribe link to my rails mailer. Unfortunately, my code breaks with this:

NoMethodError in Users#unsubscribe - undefined method `unsubscribe_hash' for nil:NilClass

which points to /app/views/users/unsubscribe.html.erb line #3

<h4>Unsubscribe from Mysite Emails</h4>
<p>By unsubscribing, you will no longer receive email...</p>
<%= simple_form_for(@user, unsubscribe_path(id: @user.unsubscribe_hash)) do |f| %>
    <%= f.hidden_field(:subscription, value: false) %>
    <%= f.submit 'Unsubscribe' %>
    <%= link_to 'Cancel', root_url %>
<% end %>

my user_controller is as shown below

class UsersController < ApplicationController
  protect_from_forgery

  def new
    @user = User.new
  end

  def create
    @user = User.new(secure_params)
    if @user.save
      flash[:notice] = "Thanks! You have subscribed #{@user.email} for Jobs Alert."
    else
      flash[:notice] = 'Error Subscribing! Kindly check your email and try again.'
    end
    redirect_to root_path
  end

  def unsubscribe
    user = User.find_by_unsubscribe_hash(params[:unsubscribe_hash])
    @user = User.find_by_unsubscribe_hash(user)
  end

  def update
    @user = User.find(params[:id])
    if @user.update(secure_params)
      flash[:notice] = 'Subscription Cancelled'
      redirect_to root_url
    else
      flash[:alert] = 'There was a problem'
      render :unsubscribe
    end
  end

  private

  def secure_params
    params.require(:user).permit(:email, :subscription)
  end

end

Route.rb

  resources :users, only: [:new, :create]
  get 'users/:unsubscribe_hash/unsubscribe' => 'users#unsubscribe', as: :unsubscribe
  patch 'users/update'

user.rb

class User < ActiveRecord::Base

  before_create :add_unsubscribe_hash, :add_true_to_users_table

  validates :email, :uniqueness => true
  validates_presence_of :email
  validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i

  private

  def add_unsubscribe_hash
    self.unsubscribe_hash = SecureRandom.hex
  end

  def add_true_to_users_table
    self.subscription = true
  end

end

unsubscribe link in the email which calls unsubscribe action

# app/views/job_notifier/send_post_email.html.erb
...
<%= link_to "Unsubscribe", unsubscribe_url(id: @unsubscribe) %>.

diagrammatic view of the error

Its seems that I am missing something, do I need to define something in my users_controller? I have never in my life being able to solve NoMethodError or I don't understand what it's all about.

解决方案

Whenever you see any error messages in ruby of the format:

undefined method 'method_name' for nil:NilClass

you are being told you are trying to call something on a nil object and so the focus of your attention should be on why is that object nil. You are also told in your log where the error occurs - in your case it refers to the line, which refers to @user in @user.unsubscribe_hash in your form declaration.

So @user is nil and in this case it's nil because your controller responsible for rendering the form isn't setting @user:

user = User.find_by_unsubscribe_hash(params[:unsubscribe_hash])
@user = User.find_by_unsubscribe_hash(user)

Now quite why you are attempting to find the user and then pass that user into the second line to find @user is beyond me, but anyway the real issue is that you have no user that matches params[:unsubscribe_hash]

So the issue is related to whatever is invoking your unsubscribe action ... you have neglected to add that to your question so I cannot help with that but that is where your focus start.

这篇关于用户中的NoMethodError#取消订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆