Ruby on rails 设计如何将当前用户与帖子关联?
如何将登录到我在创建时发布的帖子的当前用户关联起来 我的帖子模型:Ruby on rails 设计如何将当前用户与帖子关联?,ruby-on-rails,ruby-on-rails-3,devise,Ruby On Rails,Ruby On Rails 3,Devise,如何将登录到我在创建时发布的帖子的当前用户关联起来 我的帖子模型: before_create :owner belongs_to :user def owner self.user_id = current_user.id end 但这是可行的,因为我不能在模型中使用当前用户的方法 这是我的控制器: class PostsController < ApplicationController before_filter :authenticate_user! # GET /
before_create :owner
belongs_to :user
def owner
self.user_id = current_user.id
end
但这是可行的,因为我不能在模型中使用当前用户的方法
这是我的控制器:
class PostsController < ApplicationController
before_filter :authenticate_user!
# GET /posts
# GET /posts.xml
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
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.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end
class PostsController@posts}
结束
结束
#获取/posts/1
#GET/posts/1.xml
def秀
@post=post.find(参数[:id])
回应待办事项|格式|
format.html#show.html.erb
format.xml{render:xml=>@post}
结束
结束
#获取/发布/新建
#GET/posts/new.xml
def新
@post=post.new
回应待办事项|格式|
format.html#new.html.erb
format.xml{render:xml=>@post}
结束
结束
#获取/发布/1/编辑
定义编辑
@post=post.find(参数[:id])
结束
#职位
#POST/posts.xml
def创建
@post=post.new(参数[:post])
回应待办事项|格式|
如果@post.save
format.html{redirect_to(@post,:notice=>“post已成功创建”)}
format.xml{render:xml=>@post,:status=>:created,:location=>@post}
其他的
format.html{render:action=>“new”}
format.xml{render:xml=>@post.errors,:status=>:unprocessable_entity}
结束
结束
结束
#PUT/posts/1
#PUT/posts/1.xml
def更新
@post=post.find(参数[:id])
回应待办事项|格式|
如果@post.update_属性(参数[:post])
format.html{redirect_to(@post,:notice=>“post已成功更新”)}
format.xml{head:ok}
其他的
format.html{render:action=>“edit”}
format.xml{render:xml=>@post.errors,:status=>:unprocessable_entity}
结束
结束
结束
#删除/posts/1
#删除/posts/1.xml
def销毁
@post=post.find(参数[:id])
@事后销毁
回应待办事项|格式|
format.html{redirect_to(posts_url)}
format.xml{head:ok}
结束
结束
结束
我如何建立协会?因此,post user\u id column被分配了当前的\u user.id
@post.user = current_user
在您的创建操作中
或者,如果您在用户
模型上有许多:帖子关联,请执行以下操作:
@post = current_user.posts.new(params[:post])
if @post.save
...
我建议通过您的用户创建您的帖子:
# in create
@post = current_user.posts.build(params[:post])
这将自动为您填写
user\u id
。当您通过表单中的hidde\u字段进行操作时,用户是否可以更改当前用户并“黑客”网站,如果可能,在控制器-安全模式下都可以。但是您的显示
,编辑
,更新
和销毁
方法有一点。以您当前的方式,用户A可以查看、更新或删除用户B的帖子。因此,通常最好使用作用域方法来检索帖子,即@post=current_user.posts.find(params[:id])
中的show
。当然,如果你真的想让其他用户更新彼此的帖子,你的方式很好哦,好的,谢谢你的解释。我将在我的应用程序中使用此方法。