【问题标题】:Why am I getting error "undefined method `id' for nil:NilClass" even though there's a id in Rails?即使 Rails 中有 id,为什么我会收到错误“nil:NilClass 的未定义方法 `id'”?
【发布时间】:2016-04-23 04:15:26
【问题描述】:

您好,我正在制作 Rails 邮件操作。

routes.rb:

  resources :textbook_giveaways do
    member do
      get 'contact', to: 'textbook_giveaways#new_contact', as: 'new_contact'
      post 'contact', to: 'textbook_giveaways#send_contact', as: 'send_contact'
    end
  end

new_contact_html.erb:

<div class="container">
<div class="row">
  <h2>ID is: <%= @textbook_giveaway.id %></h2>
  <form role="form" action="" method="post" >
    <!--<div class="col-sm-6">-->
    <!-- <div class="col-sm-offset-4 col-sm-4 col-sm-offset-4"> -->
    <div class="col-sm-offset-4 col-sm-4 col-sm-offset-4">
      <div class="form-group">
        <!--<label for="InputMessage">Message</label>-->
        <div class="input-group">
            <%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>

                <label><h4>I would like to buy <strong><%=@texbook_giveaway.title%></strong></h4></label>

这条线&lt;h2&gt;ID is: &lt;%= @textbook_giveaway.id %&gt;&lt;/h2&gt; 工作正常。它显示了身份证号码。

但是&lt;%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %&gt; 这给了我错误。

picture 以上是错误图片

当我运行 rake 路线时,我得到:

new_contact_textbook_giveaway GET    /textbook_giveaways/:id/contact(.:format) textbook_giveaways#new_contact
send_contact_textbook_giveaway POST   /textbook_giveaways/:id/contact(.:format) textbook_giveaways#send_contact
            textbook_giveaways GET    /textbook_giveaways(.:format)             textbook_giveaways#index
                               POST   /textbook_giveaways(.:format)             textbook_giveaways#create
         new_textbook_giveaway GET    /textbook_giveaways/new(.:format)         textbook_giveaways#new
        edit_textbook_giveaway GET    /textbook_giveaways/:id/edit(.:format)    textbook_giveaways#edit
             textbook_giveaway GET    /textbook_giveaways/:id(.:format)         textbook_giveaways#show
                               PATCH  /textbook_giveaways/:id(.:format)         textbook_giveaways#update
                               PUT    /textbook_giveaways/:id(.:format)         textbook_giveaways#update
                               DELETE /textbook_giveaways/:id(.:format)         textbook_giveaways#destroy


  [1]: http://i.stack.imgur.com/MbiUz.png

我的控制器是这样的:

class TextbookGiveawaysController < ApplicationController
  before_action :set_textbook_giveaway, only: [:show, :edit, :update, :destroy, :new_contact, :send_contact]

  # GET /textbook_giveaways
  # GET /textbook_giveaways.json
  def index
    @textbook_giveaways = TextbookGiveaway.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10)
  end

  # GET /textbook_giveaways/1
  # GET /textbook_giveaways/1.json
  def show
  end

  # GET /textbook_giveaways/new
  def new
    @textbook_giveaway = TextbookGiveaway.new
  end

  # GET /textbook_giveaways/1/edit
  def edit
    if not @textbook_giveaway.user_email == current_user.email || current_user.email == "codeinflash@gmail.com"
      redirect_to @textbook_giveaway
    end
  end

  # POST /textbook_giveaways
  # POST /textbook_giveaways.json
  def create
    @textbook_giveaway = TextbookGiveaway.new(textbook_giveaway_params)
    @textbook_giveaway.user_email = current_user.email
    @textbook_giveaway.giveaway = true

    respond_to do |format|
      if @textbook_giveaway.save
        format.html { redirect_to @textbook_giveaway }
        flash[:success] = "Textbook giveaway was successfully created."
      else
        format.html { render :new }
        format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /textbook_giveaways/1
  # PATCH/PUT /textbook_giveaways/1.json
  def update
    respond_to do |format|
      if @textbook_giveaway.update(textbook_giveaway_params)
        format.html { redirect_to @textbook_giveaway }
        flash[:success] = 'Textbook giveaway was successfully updated.'
      else
        format.html { render :edit }
        format.json { render json: @textbook_giveaway.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /textbook_giveaways/1
  # DELETE /textbook_giveaways/1.json
  def destroy
    @textbook_giveaway.destroy
    respond_to do |format|
      format.html { redirect_to textbook_giveaways_url }
      format.json { head :no_content }
      flash[:alert] = "Textbook giveaway was successfully destroyed."
    end
  end

  #here for contact
  def new_contact
  end

  def send_contact
    message = params[:message]
    if Contact.send_contact(@textbook_giveaway, current_user, message).deliver
      flash[:success] = "Email sent."
      redirect_to @textbook_giveaway
    else
      flash[:alert] = "There was a problem sending the email."
      render :new_contact
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_textbook_giveaway
      @textbook_giveaway = TextbookGiveaway.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def textbook_giveaway_params
      params.require(:textbook_giveaway).permit(:title, :subject, :user_email, :description, :giveaway, :slug, :thumbnail)
      #params.fetch(:textbook_giveaway, {})
    end
end

【问题讨论】:

  • 这是你的new 对吗?所以对象@textbook_giveaway 只是一个初始化对象(尚未保存)。那么它怎么会有一个id呢?
  • @Alfie 我认为new 是对的,因为&lt;h2&gt;ID is: &lt;%= @textbook_giveaway.id %&gt;&lt;/h2&gt; 向我显示了正确的身份证号码。我也做了这个 TexbookGiveaway 的脚手架。所有其他操作,如编辑/销毁/显示..等都工作正常。只是邮件操作给了我错误。
  • 是的,你是对的。糟糕的是,我正在查看 new 操作而不是 new_contact 操作
  • 尝试将send_contact_textbook_giveaway_path(@texbook_giveaway.id) 替换为send_contact_textbook_giveaway_path(@texbook_giveaway)
  • 然后我得到错误No route matches {:action=&gt;"send_contact", :controller=&gt;"textbook_giveaways", :id=&gt;nil} missing required keys: [:id]

标签: ruby-on-rails ruby heroku actionmailer sendgrid


【解决方案1】:

这是您代码中的一个小错字。

这一行是正确的:

<h2>ID is: <%= @textbook_giveaway.id %></h2>

在下面的行中,您拼错了变量名。 @texbook_giveaway 应该是 @textbook_giveaway

<%= form_tag send_contact_textbook_giveaway_path(@texbook_giveaway.id) do %>

在 Ruby 中调用未分配的实例变量不会引发错误。它只返回nil

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2015-12-20
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多