Ruby-on-rails 设计如何关联当前用户发布?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6009295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:09:04  来源:igfitidea点击:

Devise how to associate current user to post?

ruby-on-railsruby-on-rails-3devise

提问by Max

How do I associate my current user that is signed in to my post on create?

我如何将我当前登录的用户与我创建的帖子相关联?

My post model:

我的帖子模型:

before_create :owner
belongs_to :user
def owner
    self.user_id = current_user.id
end

But that dosent work because I cannot use the method current_user in model.

但这并不重要,因为我不能在模型中使用 current_user 方法。

Here is my controller:

这是我的控制器:

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

How do I build the association ? So, that the post user_id coloumn gets assigned the current_user.id

我如何建立协会?因此,post user_id 列被分配了 current_user.id

回答by Sergey Kishenin

Add

添加

@post.user = current_user

in your create action.

在您的创建操作中。

Or if you have has_many :postsassociation on Usermodel do:

或者,如果您has_many :postsUser模型上有关联,请执行以下操作:

@post = current_user.posts.new(params[:post])

if @post.save
  ...

回答by PeterWong

I would suggest creating your post through your user:

我建议通过您的用户创建您的帖子:

# in create
@post = current_user.posts.build(params[:post])

This would automatically fill in the user_idfor you.

这将自动user_id为您填写。