设计:未定义的方法`user ='为nil:NilClass

时间:2014-10-15 20:18:36

标签: ruby-on-rails methods devise gem

我试图在我的网络应用程序中实现设计宝石 - 用户登录和注册的一切正常,但是当用户试图实际留下预测时(您需要登录的唯一内容) ,我收到此错误消息:未定义的方法`user ='为零:NilClass。我似乎无法弄清楚造成这个问题的原因。有人知道吗?

_login_items.html.erb:

<ul>
<% if user_signed_in? %>
  <li>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
  </li>
<% else %>
  <li>
  <%= link_to('Login', new_user_session_path)  %>
  </li>
<% end %>

<% if user_signed_in? %>
  <li>
  <%= link_to('Edit registration', edit_user_registration_path) %>
  </li>
<% else %>
  <li>
  <%= link_to('Register', new_user_registration_path)  %>
  </li>
<% end %>
</ul>

预测模型:

class Prediction < ActiveRecord::Base
  belongs_to :student
  belongs_to :user
end

用户模型:

class User < ActiveRecord::Base
  has_many :predictions
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

预测迁移:

class CreatePredictions < ActiveRecord::Migration
  def change
    create_table :predictions do |t|
      t.string :prediction
      t.belongs_to :student, index: true
      t.belongs_to :user
      t.timestamps
    end
  end
end

用户迁移:

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
  ## Database authenticatable
  t.string :email,              null: false, default: ""
  t.string :encrypted_password, null: false, default: ""

  ## Recoverable
  t.string   :reset_password_token
  t.datetime :reset_password_sent_at

  ## Rememberable
  t.datetime :remember_created_at

  ## Trackable
  t.integer  :sign_in_count, default: 0, null: false
  t.datetime :current_sign_in_at
  t.datetime :last_sign_in_at
  t.inet     :current_sign_in_ip
  t.inet     :last_sign_in_ip

  ## Confirmable
  # t.string   :confirmation_token
  # t.datetime :confirmed_at
  # t.datetime :confirmation_sent_at
  # t.string   :unconfirmed_email # Only if using reconfirmable

  ## Lockable
  # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
  # t.string   :unlock_token # Only if unlock strategy is :email or :both
       # t.datetime :locked_at


      t.timestamps
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

预测控制器:

class PredictionsController <ApplicationController
def create
    @prediction.user = current_user
    Prediction.create(prediction_params)
    redirect_to :back
end

def new
    @prediction = Prediction.new(student_id: params[:student_id])
end

def destroy
    Prediction.find(params[:id]).destroy
    redirect_to :back
end

private
def prediction_params
    params.require(:prediction).permit(:prediction) #, :student_id
end

end

1 个答案:

答案 0 :(得分:1)

问题在于您的PredictionsController。您必须先创建预测并将其分配给变量。将您的创建操作更改为:

def create
    @prediction = Prediction.create(prediction_params)
    @prediction.user = current_user
    redirect_to :back
end

您试图将用户分配给不存在的预测。此更正首先创建预测,然后将用户分配给它。

编辑:我注意到另一个问题:您的prediction_params方法不正确。传递给permit方法的参数必须是您要批量分配的预测模型的属性。 params中的:prediction键是您想要的键,但在该嵌套哈希中,您希望允许prediction模型的属性。因此,假设您的预测模型具有:name:value:student_id属性,您的prediction_params方法应如下所示:

def prediction_params
    params.require(:prediction).permit(:name, :value, :student_id)
end

这将使用params哈希,如下所示:

{
  prediction: {
    name: 'something',
    value: 'stuff',
    student_id: 1
  }
}