我的发票链接返回nill

时间:2015-10-07 20:23:39

标签: ruby-on-rails ruby-on-rails-4

当用户点击“发送发票”时,发票显示链接会发送到客户的电子邮件中。但是,每次点击发送

时都会出现此错误
"undefined method `id' for nil:NilClass" 

电子邮件信息视图

<h3>Hi <%= @client.try(:first_name) %></h3>

<p>Click the link below to view invoice</p>

<%= link_to 'INVOICE', edit_invoice_url(@invoice.id) %>

用户邮件控制器

def show
  @user=current_user
  @client=Client.find_by(params[:client_id])
  @invoice = @client.invoices.find_by(id: params[:id])
end

def update
  @user=User.find_by(params[:user_id])
  @client = Client.find_by(params[:client_id])
  @invoice = Invoice.find_by(params[:qty])

  if @invoice
    UserMailer.invoice_mail(@client).deliver
    flash[:change_pass] = "Invoice sent successfully!"
    redirect_to client_path(@user.company_name)
  end
end

发送发票链接

<%= link_to 'send', invoice_path(@invoice), method: :patch %>

1 个答案:

答案 0 :(得分:0)

也许是因为find_by finder需要一个正确的SQL查询属性。

如果你想通过id找到实例 - 你必须使用find方法。

例如:

> Box.find_by(3)
=> Box Load (3.1ms)  SELECT  "boxes".* FROM "boxes" WHERE (3) LIMIT 1
   PG::DatatypeMismatch: ERROR:  argument of WHERE must be type boolean, not type integer

Box.find_by(id: 3)将返回一个对象(如果存在),如Box.find(3)