Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 - Fatal编程技术网

Ruby on rails 如何将嵌套表单的当前范围限制为第一项

Ruby on rails 如何将嵌套表单的当前范围限制为第一项,ruby-on-rails,Ruby On Rails,我正在Rails中创建一个简单的讨论板。每个新的主题也会创建包含内容的第一个回复。这是我当前的模式 Topic > title:string > user_id: integer has_many :replies accepts_nested_attributes_for :replies Reply > topic_id: integer > user_id: integer > content: text belongs_to :topic 当前的top

我正在Rails中创建一个简单的讨论板。每个新的
主题
也会创建包含内容的第一个
回复
。这是我当前的模式

Topic
> title:string
> user_id: integer
has_many :replies
accepts_nested_attributes_for :replies

Reply
> topic_id: integer
> user_id: integer 
> content: text
belongs_to :topic
当前的
topics/_form.html.haml
是这样的

= form_for @topic fo |f|
  = f.text_field :title
  = f.fields_for :replies 
    = reply.text_area :content
问题是,当试图编辑一个主题时,我看到所有回复列表都是可编辑的,因为它正在对部分表单中的
字段\u:reples
字段进行迭代。我应该只看第一个

如果一个主题是新的,那么在构建一个新的回复的同时,将此迭代限制为当前的第一个可用回复的方便方法是什么

我最终得到了这样的效果,但我想应该有更好的方法

# Topic model
has_one :owner_reply, class_name: 'Reply'
accepts_nested_attributes_for :owner_reply

# Form partial view
= form_for @topic fo |f|
  - reply_resource = (@topic.new_record? ? :replies : :owner_reply)
  = f.text_field :title
  = f.fields_for :replies 
    = reply.text_area :content
这些是完整的
TopicsController#创建
更新
操作

  def create
    @board = Board.find(params[:board_id])
    @topic = @board.topics.new(topic_params)
    @topic.user_id = current_user.id
    @topic.replies.each { |reply| reply.user_id = current_user.id }
    if @topic.save
      respond_to do |format|
        format.html { redirect_to topic_path(@topic) }
      end
    else
      render :new 
    end
  end

  def update
    @topic = Topic.find(params[:id])
    if @topic.update_attributes(topic_params)
      respond_to do |format|
        format.html { redirect_to topic_path(@topic) }
      end
    else
      render :edit
    end
  end

我将使用范围关联,与您使用
:owner\u reply
的方式相同,但添加范围以限制第一条记录,如果需要,您还可以向其添加
订单

class Topic
has_many :replies
has_many :first_replies, -> { first }, class_name: 'Reply'
accepts_nested_attributes_for :replies
accepts_nested_attributes_for :first_replies
在你看来

= form_for @topic fo |f|
  ...
  = f.fields_for :first_replies
    = reply.text_area :content

主题
上创建一个类方法,返回第一个
回复

class Topic
  accepts_nested_attributes_for :first_reply

  def self.first_reply
    self.replies.first
  end
  # ...
end

然后为

调用
fields\u中的class方法,您不应该像这样使用强大的扶手。这暗示了数据结构设计不当。为什么编辑主题也会编辑回复?因为内容在回复中。你认为哪种结构更适合这种情况?那么,如果要编辑的内容在回复中,为什么要编辑主题呢?编辑回复。标题与主题相关,内容作为回复。我很想知道如何做得更好。嵌套属性适用于
新建
操作,但不适用于
编辑
操作。我建议您对主题和回复有单独的编辑操作/视图。我将使用您的方法为#"获取#的"未定义的方法"除外"??你在哪里使用这个,我看不到你解析的代码中的任何地方?