未定义的本地变量或方法错误 - 尝试将评论链接到博客

时间:2016-06-26 17:20:10

标签: ruby-on-rails ruby ruby-on-rails-4

我正在开发一个Rails 4项目,我目前有一个与组部分绑定的评论部分 - 该组几乎就像一个博客。我需要将组ID号链接到新评论。我不希望用户选择组ID,而是让它自动显示。我目前在我的评论表中有这个,我的问题来自最后一个表格组,其中包含组ID:

  <div class="form-group">
    <%= form.label :author %>
    <%= form.text_field :author, autofocus: true, class: "form-control",      
      placeholder: "Author's Name" %>
  </div>

  <div class="form-group">
    <%= form.label :comment %>
    <%= form.text_field :comment, class: "form-control", placeholder: "Write  
     your hearts content" %>
  </div>

  <div class="hide">
    <%= form.number_field :user_id, value: current_user.id %>
 </div>

 <div class="form-group">
   <%= form.number_field :group_id, value: group.id %>
 </div>

我想要做的是:user_id,value:current_user.id适用于每个组。用户id行工作正常,我没有问题。我只收到以下错误的组行:未定义的局部变量或方法`group'for#&lt;#:0x007f873a161fc0&gt;

我的架构如下所示:

  create_table "comments", force: :cascade do |t|
    t.string   "author"
    t.text     "comment"
    t.integer  "user_id"
    t.integer  "group_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "groups", force: :cascade do |t|
    t.string   "topic"
    t.integer  "user_id"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.float    "latitude"
    t.float    "longitude"
    t.string   "address"
    t.string   "city"
    t.string   "state"
  end

最后,我的评论控制器如下所示:

class CommentsController < ApplicationController

def new 
  @comment = Comment.new 
end 

def create
  @comment = Comment.new(comment_params)
  if @comment.save 
   redirect_to @group 
  else 
   render 'new'
  end 
end 

def edit 
end

def update
end 

private 

 def comment_params 
   params.require(:comment).permit(:author, :comment, :user_id, :group_id)
 end 

end

如果需要更多信息,请与我们联系。谢谢你的帮助!

以下是群组和评论的模型: 评论:

class Comment < ActiveRecord::Base
  belongs_to :group 
end

组:

 class Group < ActiveRecord::Base
  has_many :collections
  has_many :comments 
  has_many :users, :through => :collections

  validates :topic, presence: true
  validates :description, presence: true, length: { minimum: 10 }

  geocoded_by :address
  after_validation :geocode
end

3 个答案:

答案 0 :(得分:1)

如果没有在:value方法中指定form_for选项,Rails会自动推断列名称中的值,因此将其保留为以下内容应该适用于您的特定用例,否则不会引用group,使用@comment.group_id

  <div class="form-group">
    <%= form.label :author %>
    <%= form.text_field :author, autofocus: true, class: "form-control",      
      placeholder: "Author's Name" %>
  </div>

  <div class="form-group">
    <%= form.label :comment %>
    <%= form.text_field :comment, class: "form-control", placeholder: "Write  
     your hearts content" %>
  </div>

  <div class="hide">
    <%= form.number_field :user_id, value: current_user.id %>
 </div>

 <div class="form-group">
   <%= form.number_field :group_id %>
 </div>

但是,我并不完全确定你是否正在接近一些事情。

我可能会提出一些即时更改:

使用嵌套路线:

# routes.rb
resources :groups do
  resources :comments
end

CommentsController中,您现在可以从路线中获取group_id:

def create
  group = Group.find(params[:group_id])
  @comment = current_user.comments.new(comment_params)
  @comment.group = group
  if @comment.save 
   redirect_to group 
  else 
   render 'new'
  end 
end

private
  def comment_params
    params.require(:comment).permit(:author, :comment)
  end

这样,在您的视图中,您可以轻松地执行以下操作:

  <div class="form-group">
    <%= form.label :author %>
    <%= form.text_field :author, autofocus: true, class: "form-control",      
      placeholder: "Author's Name" %>
  </div>

  <div class="form-group">
    <%= form.label :comment %>
    <%= form.text_field :comment, class: "form-control", placeholder: "Write  
     your hearts content" %>
  </div>

注意:user_id和:group_id已从表单字段中删除,因为这些映射现在已在控制器级别完成。

详细了解nested routes and nested resources here

<强>更新

对于新评论,group_id将为nil,因为它尚未与之关联(嵌套资源的好处之一),但是如果您事先知道group可能是什么,那么您可能需要尝试:

@comment = group.comments.build

而是使用此@comment的值。

答案 1 :(得分:0)

创建评论时,您知道它属于哪个组:?

我认为问题在于这一行

<%= form.number_field :group_id, value: group.id %>

因此,您要分配一个ID,但您的组未初始化。你能在新方法中做这样的事吗

def new
  @group = Group.find(...) # load a group here and assign below
  @comment = Comment.new(group: @group)
end

你也可以重构,所以当你创建评论时,你会去测试路线,如/ groups / ID / comments,然后组ID将在params内

希望有所帮助

答案 2 :(得分:0)

如果您知道评论属于哪个组,那么您可以执行类似的操作

class CommentsController < ApplicationController
  def new
    @group = Group.find(...)
    @comment = Comment.new
  end

  def create
    comment = Comment.create(comments_params)
    if comment.save
      redirect_to comments_path
    else
      redirect_to :new
    end
  end

  private

  def comments_params
    params.require(:comment).permit(:body, :group_id)
  end
end

在表单中,您可以将评论链接到该组

<%= f.number_field :group_id, value: @group.id %>