Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/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 试图添加评论部分,获取:canned';我找不到没有身份证的帖子_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 试图添加评论部分,获取:canned';我找不到没有身份证的帖子

Ruby on rails 试图添加评论部分,获取:canned';我找不到没有身份证的帖子,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我在我的页面上创建了一个类似于newsfeed的功能,其中显示了您关注的人创建的所有帖子。就像Facebook一样,我希望用户能够直接从帖子下方的新闻提要发表评论 这可能是一个简单的修复方法,但我还没有找到最好的方法,甚至没有找到如何获得post_id 我当前的新闻提要/index.html.erb如下所示: <ul> <% @activities.each do |activity| %> <li> <%= image_tag(activity.

我在我的页面上创建了一个类似于newsfeed的功能,其中显示了您关注的人创建的所有帖子。就像Facebook一样,我希望用户能够直接从帖子下方的新闻提要发表评论

这可能是一个简单的修复方法,但我还没有找到最好的方法,甚至没有找到如何获得post_id

我当前的新闻提要/index.html.erb如下所示:

<ul>
 <% @activities.each do |activity| %>
  <li> <%= image_tag(activity.owner.avatar.url(:thumb), height: "64", width: "64") %>  <%= activity.owner.fullname %>
   <ul>
    <li><strong><%= activity.trackable.title %></strong></li>
    <li><%= activity.trackable.content %></li>
     <ul>
       <li><%= render 'comments/form' %></li>
     </ul>
   </ul>
 </li>
<% end %>
</ul>
评论\u controller.rb

def index
 @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: current_user.friend_ids, owner_type: "User")
 @comment = Comment.new
end
class CommentsController < ApplicationController
 before_filter :load_post

def create
 @comment = @post.comments.build(params[:comment])
 @comment.user = current_user
 if @comment.save
   @comment.create_activity :create, owner: current_user
   redirect_to @post, notice: "Comment was created."
 else
   render :new
 end
end
....
 def load_post
  @post = Post.find(params[:post_id])
 end
end
class CommentsController

因此,我的问题是如何修复它,以便存储post\u id并让它找到它?

从我所看到的评论表单似乎对嵌套路由(即/posts/:post\u id/comments)执行post请求。然后将从CommentsController中的url检索post_id

当前的“comments/_form.html.erb”部分要求@post变量能够生成正确的操作url(在本例中,似乎没有在任何位置设置@post变量)

要解决这个问题,可以将“post”作为局部变量传递给partial表单。这样,您的表单部分将能够创建正确的url,并且您的控制器将能够访问post_id

另见

newsfeeds/index.html.erb:

<ul>
 <% @activities.each do |activity| %>
 <li> <%= image_tag(activity.owner.avatar.url(:thumb), height: "64", width: "64") %>  <%= activity.owner.fullname %>
   <ul>
    <li><strong><%= activity.trackable.title %></strong></li>
    <li><%= activity.trackable.content %></li>
     <ul>
       <li><%= render 'comments/form', post: activity.trackable %></li>
     </ul>
   </ul>
 </li>
<% end %>
</ul>
<%= form_for [post, @comment] do |f| %>
 <%= f.text_area :content %>
 <%= f.submit "Add comment" %>
<% end %>
<%= form_for [post, @comment], namespace: post.id do |f| %>
 <%= f.text_area :content %>
 <%= f.submit "Add comment" %>
<% end %>
附言: 在同一页面上多次呈现同一表单时,所有表单及其输入字段的DOM id都是相同的。为了避免这种情况,您可能应该为call设置形式为_的名称空间选项(请参见以下问题:)

注释/_form.html.erb:

<ul>
 <% @activities.each do |activity| %>
 <li> <%= image_tag(activity.owner.avatar.url(:thumb), height: "64", width: "64") %>  <%= activity.owner.fullname %>
   <ul>
    <li><strong><%= activity.trackable.title %></strong></li>
    <li><%= activity.trackable.content %></li>
     <ul>
       <li><%= render 'comments/form', post: activity.trackable %></li>
     </ul>
   </ul>
 </li>
<% end %>
</ul>
<%= form_for [post, @comment] do |f| %>
 <%= f.text_area :content %>
 <%= f.submit "Add comment" %>
<% end %>
<%= form_for [post, @comment], namespace: post.id do |f| %>
 <%= f.text_area :content %>
 <%= f.submit "Add comment" %>
<% end %>


您尚未向我们展示
PublicActivity::Activity
帖子的关系。从feed上看,你可能连一个Post模型都没有?在这种情况下,您需要能够从您拥有的对象派生出它,并在
表单中使用它,或者类似的东西。这是来自PublicActivity gem的。我有一个模特。它工作并显示数据。在post.rb模型中,我有:class post“profiles#index“get”profile/:id“=>”profiles#show“get”feed“=>”newsfeeds#index“get”notifications”=>“notifications”=>“notifications”#index“get”comment“=>“comments”#新的“get”posts/:id“=>”posts#显示“尝试更改”资源帖子”为“resources:posts做资源:评论,仅::创建end”嗯,似乎进展顺利。以前的错误将我重定向到/comments,现在转到posts/:id/comments。{“utf8”=>“✓", "真实性令牌“=>”lNOQ3TjDsJWYupcRn9lB4ca07WX8cpPpWib/qhjGwNI=“,“评论”=>{“24”=>{“内容”=>“确定”}>,“提交”=>“添加评论”,“操作”=>“创建”,“控制器”=>“评论”,“发布id”=>“24”},现在我们似乎能够获得帖子id了。。但我仍然得到:没有ID就找不到帖子