【问题标题】:Unable to show replies in my view无法在我的视图中显示回复
【发布时间】:2015-07-30 14:24:18
【问题描述】:

所以我正在建立一个论坛,除了查看回复外,一切似乎都正常。当我去访问该页面时,它说该方法未定义。由于我的回复是关于我的讨论显示操作,我知道以下行是问题所在:

@posts = @discussion.posts

在我看来,我使用了@posts.reply 但失败了。我已经检查了控制台,并且 user_id 和 discussion_id 通过帖子是正确的,所以我不确定出了什么问题。如前所述,我认为我用上面的代码错误地调用了帖子?如果没有,那么请让我知道您希望看到什么代码(如果有),我可以为您安排。

谢谢!

#Post Model (Replies)

class Post < ActiveRecord::Base
    belongs_to :discussion
    belongs_to :user
end

#Discussion Model (Thread)

class Discussion < ActiveRecord::Base
    belongs_to :user
    has_many :posts
    extend FriendlyId
  friendly_id :subject, use: :slugged
end

#User Model

class User < ActiveRecord::Base

    has_many :articles
    has_many :discussions
    has_many :posts, :through => :discussions
    ...

#Discussions Controller to SHOW Posts

class DiscussionsController < ApplicationController

...
    def show
    @discussion = Discussion.friendly.find(params[:id])
    @users = User.all.order("created_at DESC")
    @posts = @discussion.posts
    render :layout => 'discussion'
  end
...
end

#Posts Controller

class PostsController < ApplicationController

  before_action :authenticate_user!
  before_action :set_discussion, only: [:new, :create, :destroy]

  def new
    @post = @discussion.post.new
  end

  def create
    # new does not insert the record into the database
    @post = @discussion.posts.build(create_params)
    @post.user = current_user  
    if @post.save
      redirect_to @discussion, notice: "It has been posted!" 
    else
      render :new # or redirect back
    end
  end

  def destroy
    @post = @discussion.posts.find(params[:id]).destroy
    flash.notice = "Deleted"
    redirect_to discussion_path(@discussion)
  end

  private    

  def create_params
    # Only permit the params which the user should actually send!
    params.require(:post).permit(:reply) 
  end

  # Will raise an ActiveRecord::NotFoundError
  # if the Discussion does not exist
  def set_discussion
    @discussion = Discussion.friendly.find(params[:discussion_id])
  end
end

#View for showing Replies

<%= @posts.reply %>
Posted: <%= @posts.created_at.strftime("%b. %d %Y") %></p>
<p><%= link_to "Delete Comment", [@posts.discussion], data: {confirm: "Are you sure you wish to delete?"}, method: :delete %></p>

#View for Reply Form

<h2>Reply</h2>
<%= form_for [ @discussion, @posts ] do |f| %>
  <p>
    <%= f.label :reply, "Reply" %><br/>
    <%= f.text_field :reply %>
  </p>
    <p>
    <%= f.submit 'Submit' %>
  </p>
<% end %>

错误:

undefined method `to_key' for #<ActiveRecord::Associations::CollectionProxy []> 
(Highlighting the post form)

undefined local variable or method `posts' for #<#<Class:0x007fa71bd31228>:0x007fa721e5f018>
(Highlighting `<%= posts.reply %>`)

【问题讨论】:

  • 向我们展示您的模型和关联以及用于显示消息的代码。没有它,任何人都无法提供帮助。
  • 没问题,现在会添加。我还要补充一点,现在似乎出现了一个错误,甚至可以回复。
  • 只需更新信息,如果需要更多信息,请告诉我。我可以添加控制器
  • 刚刚将两者都添加到帖子中 =)

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


【解决方案1】:

这段代码:

<%= @posts.reply %>
Posted: <%= @posts.created_at.strftime("%b. %d %Y") %></p>
<p><%= link_to "Delete Comment", [@posts.discussion], data: {confirm: "Are you sure you wish to delete?"}, method: :delete %></p>

应该是这样的:

<% @posts.each do |post| %>
    Posted: <%= post.created_at.strftime("%b. %d %Y") %></p>
    <p><%= link_to "Delete Comment", [post.discussion], data: {confirm: "Are you sure you wish to delete?"}, method: :delete %></p>
<% end %>

但您的代码中似乎存在更多错误,您需要修复这些错误。

希望这会有所帮助。

【讨论】:

  • 谢谢你确实解决了一个问题,我很傻。另一个问题仍然存在。
  • 第二个? undefined local variable or method posts 因为没有 posts 它是 @posts 并且 @posts 也是一个集合,所以直接没有任何东西可以处理它,你需要将它的第一个、最后一个或每个元素放在循环中。
  • 不,第一个。您的解决方案帮助了第二个,谢谢。 to_key 现在是问题。
  • 将此&lt;%= form_for [ @discussion, @posts ] do |f| %&gt; 更正为&lt;%= form_for @discussion.post do |f| %&gt; 并尝试。
  • #<0x007fa71967a5f8>
猜你喜欢
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-11
  • 2015-04-02
相关资源
最近更新 更多