@comments nil而@ event.comments在show partial中有效

时间:2015-11-10 09:36:34

标签: ruby-on-rails partials

我无法弄清楚为什么@comments在尝试循环时返回nil。如果我使用Comment而不是它可以正常工作。我目前的结构是用户/事件/评论。

评论控制器:

android {
    ...
    dexOptions {
       javaMaxHeapSize "4g"
    }
    ...
}

事件控制器显示操作:

@event.comments.each do

评论模型:

class CommentsController < ApplicationController
  before_action :authenticate_user!, only: [:create, :destroy]

  def create
    @event = Event.find(params[:event_id])
    @comment = @event.comments.create(comment_params)
    @comment.user = current_user
    if @comment.save
      flash[:notice] = "Comment Added"
      redirect_to @event
    else 
      flash[:alert] = "Comment Not Added"
      redirect_to @event
    end
  end

  def show
    @event = Event.find(params[:id])
    @comments = @event.comments
  end

  def destroy
  end

    private

      def comment_params
        params.require(:comment).permit(:body)
      end
end

用户模型:

class EventsController < ApplicationController
  before_action :authenticate_user!, only: [:new, :create,:edit, :update, :show,
                                           :index, :destroy]  
 def show
    @event = Event.find(params[:id])
  end

 private 

    def event_params 
      params.require(:event).permit(:start_date, :start_time, :location,                                            :title, :description, :size, :difficulty,
                                    :activity, :duration)
    end

end 

活动模型:

class Comment < ActiveRecord::Base
  belongs_to :event
  belongs_to :user

  validates :body, presence: true

  scope :newest, -> { order("created_at DESC") }
end

最后,评论/ _show.html.erb partial:

class User < ActiveRecord::Base
  has_many :created_events, class_name: 'Event', :foreign_key => "creator_id",  
            dependent: :destroy
  has_many :registers, :foreign_key => "attendee_id", dependent: :destroy
  has_many :attended_events, through: :registers, dependent: :destroy

  has_many :comments, through: :events

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable,            :lockable
  validates :name, presence: true, uniqueness: true, length: { maximum: 50 }
  validates :email, presence: true, uniqueness: { case_sensitive: true }

  validate :validate_name

  def validate_name
    if User.where(email: name).exists?
      errors.add(:name, :invalid)
    end
  end
end

从活动中显示表单:

class Event < ActiveRecord::Base
  scope :latest, -> { order(date: :asc, time: :asc)}

  belongs_to :creator, class_name: 'User'
  has_many :registers, :foreign_key => 'attended_event_id', dependent: :destroy
  has_many :attendees, through: :registers, dependent: :destroy

  has_many :comments, dependent: :destroy

  validates :title, presence: true, length: { maximum: 50 }
  validates :description, presence: true, length: { maximum: 500 }
  validates :location, presence: true
  validates :start_time, presence: true
  validates :start_date, presence: true
  validates :activity, presence: true
  validates :difficulty, presence: true

end

同样,如果我将部分中的@comments更改为@ events.comments,它将识别出特定事件的注释并循环遍历它们。这让我在5个小时的大部分时间里都疯了。感谢。

2 个答案:

答案 0 :(得分:0)

正如Pardeep Saini所述,您需要将@comments添加到events#show

def show
    @event = Event.find params[:id]
    @comments = @event.comments
end

问题是@comments是一个变量,需要定义。如果没有定义,那么您将收到相当于 undefined 错误。

因此,要修复它,您需要确保调用已定义的变量; @comments(如果您已定义)或@event.comments

我认为您的结构存在更深层次的问题(通过查看您的代码)。

你最好这样设置:

#config/routes.rb
resources :events do
   resources :comments, only: [:create, :destroy] #-> url.com/events/:event_id/comments...
end

#app/controllers/comments_controller.rb
class EventsController < ApplicationController
   def show
      @event    = Event.find params[:id]
      @comments = @event.comments
   end
end

这将允许您使用以下内容:

#app/views/events/show.html.erb
<%= @event.title %>
<%= render @comments %>
<%= render "new_comment" %>

#app/views/events/_comment.html.erb
<%= comment.user.name %>: <%= time_ago_in_words(comment.created_at).capitalize %> ago
<%= comment.body %>

#app/views/events/_new_comment.html.erb
<%= form_for @event.comments.build do |f| %>
   <%= f.text_field :body %>
   <%= f.submit %>
<% end %>

这将使得如果你浏览url.com/events/1,它将输出所有事件的评论。

此设置的额外好处是能够创建 / 销毁评论:

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
   before_action :set_event

   def create
     @comment = @event.comments.new comment_params
     @comment.user = current_user
     @comment.save
   end

   def destroy
     @comment = @event.comments.find params[:id]
     @comment.destroy
   end

   private

   def comment_params
       params.require(:comment).permit(:body, :user)
   end

   def set_event
       @event = Event.find params[:event_id]
   end
end 

答案 1 :(得分:0)

解决了这个问题。这是一个非常愚蠢的错误,我在我的事件控制器中列出了两次显示。最下面一个是骑在上面。