Ruby on rails 使用嵌套属性将多个belogs_指定给指定

Ruby on rails 使用嵌套属性将多个belogs_指定给指定,ruby-on-rails,nested-attributes,belongs-to,Ruby On Rails,Nested Attributes,Belongs To,我的问题是,如何将多个属性分配给具有嵌套属性的关联 我想建立一个发行系统。创建问题时,我还希望创建第一条注释作为问题正文 因此,我有以下模型: class Issue < ActiveRecord::Base has_many :comments, as: :commentable validates :title, presence: true accepts_nested_attributes_for :comments end class Comment <

我的问题是,如何将多个属性分配给具有嵌套属性的关联

我想建立一个发行系统。创建问题时,我还希望创建第一条注释作为问题正文

因此,我有以下模型:

class Issue < ActiveRecord::Base
  has_many :comments, as: :commentable

  validates :title, presence: true

  accepts_nested_attributes_for :comments
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :commentable, polymorphic: true

  validates :content, :user, presence: true
end
在这种情况下,我不知道如何将
current_user
分配给嵌套属性创建的注释


有什么建议或其他方法吗?谢谢

正如我在评论中所写,有两种方法可以做到这一点。 第一种方法是在子窗体中添加隐藏字段以设置当前用户:

= simple_nested_form_for(@issue) do |f|
  = f.input :title
  = f.fields_for(:comments) do |cf|
    = cf.input(:content)
    = cf.hidden(:user, current_user)
  = f.submit
如果您不信任这种方法,担心用户在浏览器中摆弄字段,那么您也可以在控制器中这样做

class IssuesController < ApplicationController
  before_action :authenticate_user! #devise authentication

  def new
    @issue = Issue.new
    @issue.comments.build
  end

  def create
    @issue = Issue.new(issue_params)
    @issue.comments.first.user = current_user
    @issue.save
  end

  private

  def issue_params
    params.require(:issue).permit(:title, comments_attributes: [:content])
  end
end
类发布控制器

通过这种方式,您可以获取通过表单创建的第一条注释,然后手动将用户分配给它。然后您就可以确定第一个创建的注释属于您的当前用户。

当您使用参数时,还可以将
user\u id
添加为
current\u user.id

class IssuesController < ApplicationController
  before_action :authenticate_user! #devise authentication

  def new
    @issue = Issue.new
    @issue.comments.build
  end

  def create
    @issue = Issue.new(issue_params)
    @issue.save
  end

  private

  def issue_params
    params[:issue][:comment][:user_id] = current_user.id
    params.require(:issue).permit(:title, comments_attributes: [:content, :user_id])
  end
end
类发布控制器
隐藏字段名为:评论区内的用户?我已经尝试过这种方法,它很有效。但是我认为它不安全。你说不安全是什么意思?害怕用户摆弄字段?然后在控制器中执行此操作。在构建注释时,将当前用户作为属性传递,并在create上再次检查它。您的意思是:
@issue.comments.each{c | c.user=current_user}
?差不多。但这会将每个注释设置为当前用户。不确定这是否是您的预期行为。这是否意味着我需要向问题模型添加“注释”方法或关联?
class IssuesController < ApplicationController
  before_action :authenticate_user! #devise authentication

  def new
    @issue = Issue.new
    @issue.comments.build
  end

  def create
    @issue = Issue.new(issue_params)
    @issue.comments.first.user = current_user
    @issue.save
  end

  private

  def issue_params
    params.require(:issue).permit(:title, comments_attributes: [:content])
  end
end
class IssuesController < ApplicationController
  before_action :authenticate_user! #devise authentication

  def new
    @issue = Issue.new
    @issue.comments.build
  end

  def create
    @issue = Issue.new(issue_params)
    @issue.save
  end

  private

  def issue_params
    params[:issue][:comment][:user_id] = current_user.id
    params.require(:issue).permit(:title, comments_attributes: [:content, :user_id])
  end
end