Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 @注释无,而@event.comments在show partial中起作用_Ruby On Rails_Partials - Fatal编程技术网

Ruby on rails @注释无,而@event.comments在show partial中起作用

Ruby on rails @注释无,而@event.comments在show partial中起作用,ruby-on-rails,partials,Ruby On Rails,Partials,我不明白为什么@comments在我尝试循环时返回nil。如果我使用@event.comments.each do,它就可以正常工作。我当前的结构是用户/事件/评论 财务主任评论: class CommentsController < ApplicationController before_action :authenticate_user!, only: [:create, :destroy] def create @event = Event.find(params

我不明白为什么@comments在我尝试循环时返回nil。如果我使用
@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 CommentsController
事件控制器显示操作:

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
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
class注释{order(“created_at DESC”)}
结束
用户模型:

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用户“creator\u id”,
依赖::销毁
拥有多个:寄存器,:外键=>“与会者id”,依赖::销毁
通过::registers、dependent::destroy参加了许多活动
有很多:评论,通过::事件
#包括默认设计模块。其他可供选择的项目包括:
#:可确认、:可锁定、:超时和:全授权
设计:数据库可验证,可注册,
:可恢复,:可记忆,:可跟踪,:可验证,:可确认,:可锁定
验证:名称,存在性:true,唯一性:true,长度:{最大值:50}
验证:电子邮件,状态:true,唯一性:{区分大小写:true}
验证:验证\u名称
def验证_名称
如果User.where(电子邮件:name.)存在?
错误。添加(:名称,:无效)
结束
结束
结束
事件模型:

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

  validates :body, presence: true

  scope :newest, -> { order("created_at DESC") }
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
class事件{订单(日期::asc,时间::asc)}
属于:创建者,类名:“用户”
有多个:寄存器,:foreign\u key=>'attended\u event\u id',依赖::destroy
有很多:与会者,通过::注册,依赖::销毁
有很多:注释,依赖::销毁
验证:title,presence:true,长度:{最大值:50}
验证:description,presence:true,length:{maximum:500}
验证:位置、状态:true
验证:开始时间,状态:真
验证:开始日期,状态:true
验证:活动,状态:true
验证:困难,存在:正确
结束
最后,注释/_show.html.erb部分:

<% if @comments %>
    <span class="results-number color-aqua-show">Comments</span>
    <% @comments.each do |comment| %>
      <p class="comments">
        <i class="color-green fa fa-user ride-i"></i>
            <%= comment.user.name %>: <%= time_ago_in_words(comment.created_at).capitalize %> ago
        </p>
      <p>
       <i class="color-aqua fa fa-comment ride-i"></i>
        <%= comment.body %>
      </p>
        <div class="bottom-breaker"></div>
    <% end %>
<% else %>
    <span class="results-number color-aqua-show">Be the first to comment!</span>
<% end %>

评论

:以前

第一个发表评论!
从事件中显示表单:

<div class="container s-results margin-bottom-50">
  <div class="row">
    <div class="col-md-9">

                <%= render partial: 'comments/show' %>

                <%= render partial: 'comments/form' %>

          </div>
    </div>
</div>


同样,如果我将部分中的@comments更改为@events.comments,它将识别特定事件的注释并循环它们。这已经让我疯狂了5个小时了。谢谢。

正如
Pardeep Saini
所说,您需要在
事件#show
中添加
@comments

def show
    @event = Event.find params[:id]
    @comments = @event.comments
end
问题在于
@comments
是一个变量,需要定义它。如果没有定义,那么您将收到与未定义错误等效的错误

因此,要修复它,您需要确保调用的是已定义的变量;
@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

解决了这个问题。这是一个非常愚蠢的错误,我在我的事件控制器中显示了两次。底部的是顶部。

您从哪个视图渲染注释/显示部分?您需要在事件显示操作中添加
@comments=@event.comments
。已经尝试过了。在您的
事件
控制器中生成零?是的。顺便说一句,Rich,我读了你所有关于这方面的帖子,以及我能找到的每一篇帖子。有希望:)但无法找到解决方案。修改答案。它不起作用,我们应该跳进聊天室来修复它,直到它不起作用。如果我在评论上打@event.comments显示部分内容,效果很好。我基本上遵循rails指南如何添加注释,但当我想为注释添加分页时,我发现唯一的方法是使用(at)注释,这就是我遇到这个问题的原因。为了确认,您已经在
显示
操作中显式设置了
@comments
。。。它仍然返回一个
nil
错误?正确。如果我将show partial中的If语句设置为@event.comments,那么它将返回一个no-method-found“each”nil错误。如果我保持原样(如果(at)评论),它将始终跳过该部分并显示“第一个评论”。很抱歉,我必须不断添加(at)来代替@,但我只能在评论出现时使用(at)符号:(