2015-06-28 76 views
-1

我完全不確定我哪裏出錯,它令我驚訝,我無法弄清楚,所以任何建議將不勝感激。Rails協會 - user_id狀態無軌道4

  1. 我有2個模型userr(招募者)& feedbackr(招募者反饋)
  2. [userr模型] userr has_many :feedbackr
  3. [feedbackr模型] belongs_to :userr
  4. [架構] i的已添加userr_id列對錶反饋者
  5. [feedbackrs_controller]我已將:userr_id添加到feedbackr_params

的問題是,當我創建一個反饋意見作爲招聘&去 到我的控制檯,然後輸入Feebackr.find(4)(最後創建 反饋)的userr_id顯示nil - 它 是假設顯示userr的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 => "[email protected]", 
       :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

嗯...你沒有在你的表單中傳遞userr_id信息?你是否將'current_user'指定爲'userr_id'?你應該在控制器中執行此操作#create action –

回答

1

難道你要設置的current_useruserr

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 
+0

@cryrilldd謝謝你soo !! – ARTLoe

1

feedbackrs_controller.rb文件分配@feedbackr.userr = current_user會自動生成USER_ID領域。由於目前,當您生成feebackr時,您沒有爲user_id分配任何內容,因此您的userr_id = nil