ruby-on-rails - 如何将 Simple_Form 与嵌套资源一起使用?

标签 ruby-on-rails ruby-on-rails-4 simple-form rails-routing

我有 3 个模型:FamilyTree、Node、Comment。

FamilyTree 上的每个条目都是一个节点。节点可以是注释。

型号如下:
FamilyTree.rb

# == Schema Information
#
# Table name: family_trees
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  user_id    :integer
#  created_at :datetime
#  updated_at :datetime
#

class FamilyTree < ActiveRecord::Base
  attr_accessible :name
  belongs_to :user
  has_many :memberships, dependent: :destroy
  has_many :members, through: :memberships, source: :user, dependent: :destroy
  has_many :nodes, dependent: :destroy
end
Node.rb
# == Schema Information
#
# Table name: nodes
#
#  id             :integer          not null, primary key
#  name           :string(255)
#  family_tree_id :integer
#  user_id        :integer
#  media_id       :integer
#  media_type     :string(255)
#  created_at     :datetime
#  updated_at     :datetime
#  circa          :datetime
#  is_comment     :boolean
#

class Node < ActiveRecord::Base
  belongs_to :family_tree
  belongs_to :user
  belongs_to :media, polymorphic: true, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :node_comments, dependent: :destroy    
end
Comment.rb
# == Schema Information
#
# Table name: comments
#
#  id         :integer          not null, primary key
#  user_id    :integer
#  message    :text
#  node_id    :integer
#  created_at :datetime
#  updated_at :datetime
#

class Comment < ActiveRecord::Base
  validates :message, presence: true    
  belongs_to :user
  belongs_to :node    
end
routes.rb
  resources :family_trees do
    resources :nodes do
      resources :comments
    end
  end

如何使用 Simple_Form 编辑评论?那看起来像什么?

我试过这个:
<%= simple_form_for [@family_tree, @node, @comment] do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.association :user %>
    <%= f.input :message %>
    <%= f.association :node %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

但这给了我这个错误 - 在该部分的第 1 行:
NoMethodError at /family_trees/1/nodes/4/comments/3/edit
undefined method `family_tree_comment_path' for #<#<Class:0x007f87356c5110>:0x007f8733d338a0>

最佳答案

事实证明,我所要做的只是对我的观点进行微调:

<%= simple_form_for([@family_tree, @node, @comment]) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.association :user %>
    <%= f.input :message %>
    <%= f.association :node %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

那创造了奇迹。

关于ruby-on-rails - 如何将 Simple_Form 与嵌套资源一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26293158/

相关文章:

ruby-on-rails - bundle 安装错误,意外 ':'

python - 如何压缩字符串?

ruby-on-rails - Rails 4.2 语法错误,意外 ':',期待 =>

ruby-on-rails - 创建一个唯一的哈希

ruby-on-rails - 使用 simple_form Bootstrap 输入组包装器

ruby-on-rails-3 - Rails 3 + 简单表单 : Specifiy label class when using f. 关联

ruby-on-rails - 我如何发表评论并与帖子索引页面中的帖子相关联?

ruby-on-rails - Rails/Postgres - 我需要什么类型的数据库锁?

javascript - 如何禁用 twitter bootstrap 电子邮件验证弹出窗口?

ruby-on-rails - 使用简单表单将数据属性添加到自定义包装器