我完全不确定自己哪里出错了,令我惊讶的是我无法理解,所以任何建议都会非常感激。
userr has_many :feedbackr
belongs_to :userr
userr_id
列添加到表反馈器:userr_id
添加到feedbackr_params
问题是,当我创建一个反馈评论作为招聘人员和&走 进入我的控制台并输入Feebackr.find(4)
(最后创建的 反馈)userr_id
显示nil
- 它 假设显示创建了a的用户(招募者)的id
反馈 - 我不确定为什么它显示nil
,因为我的协会似乎都正确 -
任何建议都会非常感激 - 我的文件如下 -
控制台
2.1.2 :055 > ap Feedbackr.find(4)
Feedbackr Load (0.3ms) SELECT "feedbackrs".* FROM "feedbackrs" WHERE "feedbackrs"."id" = ? LIMIT 1 [["id", 4]]
#<Feedbackr:0x007fd89136e018> {
:id => 4,
:email => "richill@gmail.com",
:created_at => Sun, 28 Jun 2015 00:29:52 UTC +00:00,
:updated_at => Sun, 28 Jun 2015 00:29:52 UTC +00:00,
:category_feedbackr_id => 6,
:content => "feedback1",
:userr_id => nil
}
=> nil
2.1.2 :056 >
模式
ActiveRecord::Schema.define(version: 20150627235330) do
create_table "feedbackrs", force: true do |t|
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "category_feedbackr_id"
t.text "content"
t.integer "userr_id"
end
create_table "userrs", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
end
add_index "userrs", ["confirmation_token"], name: "index_userrs_on_confirmation_token", unique: true
add_index "userrs", ["email"], name: "index_userrs_on_email", unique: true
add_index "userrs", ["reset_password_token"], name: "index_userrs_on_reset_password_token", unique: true
end
feedbackr.rb
class Feedbackr < ActiveRecord::Base
belongs_to :userr
end
userr.rb
class Userr < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :feedbackrs
end
feedbackr_controller.rb
class FeedbackrsController < ApplicationController
respond_to :html, :xml, :json
before_action :set_feedbackr, only: [:show, :edit, :update, :destroy]
def index
@feedbackrs = Feedbackr.all.order("created_at DESC")
respond_with(@feedbackrs)
end
def show
respond_with(@feedbackr)
end
def new
@feedbackr = Feedbackr.new
respond_with(@feedbackr)
end
def edit
end
def create
@feedbackr = Feedbackr.new(feedbackr_params)
respond_to do |format|
if @feedbackr.save
format.html { redirect_to dashboard_path, :notice => 'Thank you for your feedback' }
format.xml { render :xml => @feedbackr, :status => :created, :location => [@feedbackr] }
else
format.html { render :action => "new" }
format.xml { render :xml => @feedbackr.errors, :status => :unprocessable_entity }
end
end
end
def update
...
end
def destroy
...
end
private
def set_feedbackr
@feedbackr = Feedbackr.find(params[:id])
end
def feedbackr_params
params.require(:feedbackr).permit(:email, :category_feedbackr_id, :content, :userr_id)
end
end
视图/ feedbackrs / _form.html.erb
<%= simple_form_for(@feedbackr) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input_field :email, label: 'email address', value: current_userr.email %>
<%= f.association :category_feedbackr, as: :radio_buttons, label: 'How likely are you to recommend us?' %>
<%= f.input :content, label: 'What is the primary reason for your score?'%>
</div>
<div class="form-actions">
<%= f.button :submit, 'Provide Feedback' %>
</div>
<% end %>
<div><%= render 'shared/footer' %></div>
答案 0 :(得分:1)
您是否要将current_user
设为userr
?
def create
@feedbackr = Feedbackr.new(feedbackr_params)
@feedbackr.userr = current_user # And make sure the user is authenticated (though you probably did that in your ApplicationController ?)
respond_to do |format|
if @feedbackr.save
format.html { redirect_to dashboard_path, :notice => 'Thank you for your feedback' }
format.xml { render :xml => @feedbackr, :status => :created, :location => [@feedbackr] }
else
format.html { render :action => "new" }
format.xml { render :xml => @feedbackr.errors, :status => :unprocessable_entity }
end
end
end
答案 1 :(得分:1)
在@feedbackr.userr = current_user
文件中分配feedbackrs_controller.rb
会自动生成user_id字段。目前,您在生成feebackr
时没有向user_id分配任何内容,因此您的userr_id = nil
也是如此。