我有一个用户和链接MVC。我将它们列在下面。基本上,当我创建一个新链接时,我希望它与我的用户绑定,然后在我的显示页面上显示带有引号的用户电子邮件,但是当我进行身份验证时,我仍然得到一个零值用户:用户和链接关联困难
- 用户和链路ASSOCATION
- 允许在我的强烈参数
:user_id
- 已要求用户一个的before_filter作出新的请求
- 当记录有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
谢谢你的回答,协会现在正常工作。出于我自己的好奇心,你为什么认为link_params没有设置用户?至于你最后的评论,我应该运行一个迁移改变user_id为null:我的链接表中的false? – Jbur43
因为你的表单很可能没有user_id字段(或者它是空白的)。我会编写一个迁移来更改user_id的nullness,但这取决于您(并且您需要删除任何具有空user_id的行) –
感谢您花时间解释这一点,我非常感谢。 – Jbur43