问题与各协会:与导轨设计 [英] Rails with Devise: Problems with associations
问题描述
我都取得了新的Rails应用。它只有一个用户模型(即通过设计产生的)和与支架产生Post模型完全新鲜的。在Post模型我有一个名为 USER_ID
数据库中的列。
问题是, USER_ID
中邮表总是无
(它不会更改为<被张贴的用户的code> USER_ID )。我怀疑是有一些做设计,但我不能完全确定。做什么有什么建议?
user.rb
类用户&LT; ActiveRecord的::基地
设计:database_authenticatable,:可注册,
:可恢复的,:rememberable,:可追踪,:可验证 attr_accessible:电子邮件,:密码:password_confirmation,:remember_me 的has_many:帖子,取决于:摧毁
结束
post.rb
类帖子&LT; ActiveRecord的::基地
attr_accessible:标题,:USER_ID
belongs_to的:用户
结束
的Gemfile
来源https://rubygems.org创业板导轨,3.2.13
创业板自举 - 萨斯','2.1'
创业板色器件组:做开发
宝石'sqlite3的,1.3.5
结束组:资产做
创业板SASS护栏,3.2.5
宝石'咖啡轨,3.2.2 创业板uglifier,1.2.3
结束创业板的jQuery护栏,2.0.2组:生产做
宝石PG,0.12.2提供
结束创业板will_paginate','&GT; 3.0'
post_controller(创建)
DEF创建
@post = Post.new(PARAMS [:帖]) 做的respond_to |格式|
如果@ post.save
format.html {redirect_to的@post,通知:后已成功创建。 }
format.json {渲染JSON:@post,状态:创建,地点:@post}
其他
format.html {渲染动作:新}
format.json {渲染JSON:@ post.errors,状态:unprocessable_entity}
结束
结束
结束
我猜 USER_ID
属性没有得到设置,因为你不设置它。 :)
DEF创建
@post = Post.new(PARAMS [:帖])
@ post.user_id = current_user.id#你需要加入这行 做的respond_to |格式|
如果@ post.save
format.html {redirect_to的@post,通知:后已成功创建。 }
format.json {渲染JSON:@post,状态:创建,地点:@post}
其他
format.html {渲染动作:新}
format.json {渲染JSON:@ post.errors,状态:unprocessable_entity}
结束
结束
结束
附注:我会建议把一个非空约束对 post.user_id
以及(如果你还没有的话)外键约束从 post.user_id
到 user.id
。这些限制有助于双方prevent,更容易诊断这些类型的问题。
I have made new Rails app. It is totally fresh with just one User model (that is generated through Devise) and a Post model that is generated with scaffold. In the Post model I have a column in the database that is named user_id
.
The problem is that user_id
in the Post table always is nil
(it won't change to the user_id
of the user that is posting). I suspect that is has something to do with Devise, but I'm not totally sure. Any suggestions on what to do?
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :posts, dependent: :destroy
end
post.rb
class Post < ActiveRecord::Base
attr_accessible :title, :user_id
belongs_to :user
end
gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.13'
gem 'bootstrap-sass', '2.1'
gem 'devise'
group :development do
gem 'sqlite3', '1.3.5'
end
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :production do
gem 'pg', '0.12.2'
end
gem 'will_paginate', '> 3.0'
post_controller (create)
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
I'm guessing the user_id
attribute isn't getting set because you're not setting it. :)
def create
@post = Post.new(params[:post])
@post.user_id = current_user.id # You need to add this line
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
Side note: I would recommend putting a not-null constraint on post.user_id
as well as (if you haven't already) a foreign key constraint from post.user_id
to user.id
. Those constraints help to both prevent and more easily diagnose these kinds of problems.
这篇关于问题与各协会:与导轨设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!