2015-12-27 28 views
0

我有一個用戶和鏈接MVC。我將它們列在下面。基本上,當我創建一個新鏈接時,我希望它與我的用戶綁定,然後在我的顯示頁面上顯示帶有引號的用戶電子郵件,但是當我進行身份驗證時,我仍然得到一個零值用戶:用戶和鏈接關聯困難

  1. 用戶和鏈路ASSOCATION
  2. 允許在我的強烈參數:user_id
  3. 已要求用戶一個的before_filter作出新的請求
  4. 當記錄有USER_ID我的鏈接模式

如果您看看我的show.html.erb和行<%= @link.user.try(:email) %>,這是發佈該鏈接的用戶電子郵件應該出現的位置,但它們都以零值表示。

我現在有點迷路,爲什麼我不能得到這個工作,任何幫助將非常感激!

型號:

class User < ActiveRecord::Base 
    has_many :links 
    acts_as_voter 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
end 

class Link < ActiveRecord::Base 
    belongs_to :user 
    acts_as_votable 
    attr_accessor :avatar 
    mount_uploader :avatar, AvatarUploader 
end 

控制器:

class LinksController < ApplicationController 
    before_filter :authenticate_user!, except: [:index, :show] 

    def index 
    @links = Link.all 
    end 

    def show 
    @link = Link.find(params[:id]) 
    end 

    def new 
    @link = Link.new 
    end 

    def edit 
    end 

    def create 
    @link = Link.new(link_params) 

    if @link.save 
     redirect_to root_path 
    else 
     render 'new' 
    end 
    end 

    private 

    def link_params 
    params.require(:link).permit(:title, :url, :avatar, :user_id) 
    end 
end 

show.html.erb:

<p id="notice"><%= notice %></p> 

<p> 
    <strong>Author:</strong> 
    <%= @link.title %> 
</p> 

<p> 
    <strong>Quote:</strong> 
    <%= @link.url %> 
</p> 

<small class="author">Submitted <%= time_ago_in_words(@link.created_at) %> ago by <%= @link.user.try(:email) %></small> 

模式:

create_table "links", force: :cascade do |t| 
    t.string "title" 
    t.string "url" 
    t.datetime "created_at",      null: false 
    t.datetime "updated_at",      null: false 
    t.integer "user_id" 
    t.integer "cached_votes_total", default: 0 
    t.integer "cached_votes_score", default: 0 
    t.integer "cached_votes_up", default: 0 
    t.integer "cached_votes_down", default: 0 
    t.string "avatar" 
    end 

create_table "users", force: :cascade 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",       null: false 
    t.datetime "updated_at",       null: false 
    end 

回答

1

它聽起來不像user_id曾經設置過。碰巧,你可能不想通過參數設置它(因爲這些可以由用戶操縱)。

相反,更換

@link = Link.new(link_params) 

隨着

@link = current_user.links.build(link_params) 

如果鏈接始終有一個用戶,我也將標誌着user_id列不爲空,而不是亂拋垃圾我的電話應用try(和我一般寧願try!try

+0

謝謝你的回答,協會現在正常工作。出於我自己的好奇心,你爲什麼認爲link_params沒有設置用戶?至於你最後的評論,我應該運行一個遷移改變user_id爲null:我的鏈接表中的false? – Jbur43

+0

因爲你的表單很可能沒有user_id字段(或者它是空白的)。我會編寫一個遷移來更改user_id的nullness,但這取決於您(並且您需要刪除任何具有空user_id的行) –

+0

感謝您花時間解釋這一點,我非常感謝。 – Jbur43

0

1.據我所知,邏輯是你只能在登錄後創建鏈接。用戶只能爲他/她創建鏈接(只有登錄的用戶可以是他創建的鏈接的所有者)。在這種情況下,你不應該暴露在USER_ID形式,而是將其添加在創建行動:

def create 
    @link = Link.new(link_params) 

    if @link.save 
     current_user.links << @link 
     redirect_to root_path 
    else 
     render 'new' 
    end 
    end 

2.Also,如果你不允許孤兒鏈接(沒有用戶作爲所有者),那麼我建議不使用user.try(:email)而是@link.user.email

  • 如果通過@link訪問用戶對象,那麼你可以在表演的動作,以節省一些DB預裝它查詢

    def show 
    @link = Link.includes(:user).find(params[:id]) 
    end 
    
  • 進行這些更改並重試。此外,電子郵件要求用戶如果使用的是默認色器件配置,使.try(:email)返回nil可能意味着user對象沒有找到,這意味着關聯設置不正確(我的版本可能會解決這個問題)