Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 无法关联帖子设计用户_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 无法关联帖子设计用户

Ruby on rails 无法关联帖子设计用户,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我很难将帖子与Desive中注册的用户关联 我生成了一个后脚手架,并在Desive中正确设置了所有内容。 我在帖子中添加了一个迁移,其中包含一个user\u id字段 用户模型有很多:帖子 Post模型属于:用户 由于某些原因,我无法将用户与帖子连接。我错过什么了吗 谢谢大家 我的邮政管理员 def create @user = User.find(params[:id]) @post = @user.posts.create(params[:post]) respond_to do |f

我很难将帖子与Desive中注册的用户关联

我生成了一个后脚手架,并在Desive中正确设置了所有内容。 我在帖子中添加了一个迁移,其中包含一个user\u id字段

用户模型有很多:帖子 Post模型属于:用户

由于某些原因,我无法将用户与帖子连接。我错过什么了吗

谢谢大家

我的邮政管理员

def create

@user = User.find(params[:id])
@post = @user.posts.create(params[:post])


respond_to do |format|
  if @post.save
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render action: 'show', status: :created, location: @post }
  else
    format.html { render action: 'new' }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
end

结束

首先,您需要用户关联帖子:

@user = User.find(params[:id]) # or just use current_user as you are using Devise
只要你有很多关联,你可以做以下事情:

@post = @user.posts.build(params[:post]) # to return newly created object without saving it to the database
@post = @user.posts.create(params[:post]) # to create and save record to the database

就是这样。

请发布创建发布的控制器的相关方法。使用发布控制器更新请发布生成的迁移代码。class AddUserIdPosts