保存与状态更新相关的评论对象.build ruby

发布于 2024-10-03 17:51:19 字数 1611 浏览 4 评论 0原文

我有一个状态更新,并评论数据库表。

一个用户有很多状态更新,一个状态更新有很多评论。与facebook类似,当用户朋友进入用户提要页面(显示页面)时,他们应该能够对用户状态更新发表评论。

我在保存用户朋友评论时遇到问题..我的代码如下..我认为它与评论控制器,创建方法,“@comment = @statusupdate.comments.build(params[:comment]) 有关“

非常感谢任何指导!谢谢!

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @statusupdates = @user.statusupdates.paginate(:page => params[:page], :per_page => 25)
    @statusupdate = Statusupdate.new    
    @comment = Comment.new
  end
end

show.html.erb
  <% form_for @statusupdate do |f| %>
  <%= f.error_messages %>
  <div class="field">
    <%= f.text_field :content %>
  </div>

 <% @statusupdates.each do |s| %>

   <%= s.content %><br />

    <% form_for @comment do |f| %>
    <%= f.error_messages %>
      <div class="field">
        <%= f.text_field :comment %>
      </div>
      <div class="field">
        <%= f.hidden_field :user_id, :value => current_user.id %>
      </div>
      <div class="actions">
        <%= f.submit "Submit" %>
      </div>
      <br><br>
    <% end %>
  <% end %>

class CommentsController < ApplicationController
  def create
    @comment = @statusupdate.comments.build(params[:comment])
      if @comment.save
      flash[:success] = "Comment created!"
      redirect_to root_path
    else
      @feed_items = []
      render 'pages/home'
    end
   end
 end

I have a status update, and comment db table.

A user has many status updates, and a status update has many comments. Similar to facebook, When a users friend goes to the users feed page (show page), they should be able to comment on the users status updates.

I'm having issues saving a users friends comment.. my code is below.. I think it has something to do with the Comments Controller, Create method, "@comment = @statusupdate.comments.build(params[:comment])"

any guidance is much appreciated! thanks!

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @statusupdates = @user.statusupdates.paginate(:page => params[:page], :per_page => 25)
    @statusupdate = Statusupdate.new    
    @comment = Comment.new
  end
end

show.html.erb
  <% form_for @statusupdate do |f| %>
  <%= f.error_messages %>
  <div class="field">
    <%= f.text_field :content %>
  </div>

 <% @statusupdates.each do |s| %>

   <%= s.content %><br />

    <% form_for @comment do |f| %>
    <%= f.error_messages %>
      <div class="field">
        <%= f.text_field :comment %>
      </div>
      <div class="field">
        <%= f.hidden_field :user_id, :value => current_user.id %>
      </div>
      <div class="actions">
        <%= f.submit "Submit" %>
      </div>
      <br><br>
    <% end %>
  <% end %>

class CommentsController < ApplicationController
  def create
    @comment = @statusupdate.comments.build(params[:comment])
      if @comment.save
      flash[:success] = "Comment created!"
      redirect_to root_path
    else
      @feed_items = []
      render 'pages/home'
    end
   end
 end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-10-10 17:51:19

检查表单的 html 以确保其正确。另请参阅哪些参数被发送到创建操作。

我看到的主要内容是状态更新和注释的表单是嵌套的,并且都使用块参数 f。这可能会导致事情变得非常奇怪(特别是因为 ruby​​ 1.8 和 1.9 之间块参数的范围不同)。看起来您实际上并不希望嵌套表单。您还应该修复 html 中的缩进。

Check the html of the form to make sure its right. Also see what parameters are getting sent to the create action.

The main thing I see is that the forms for the status update and the comments are nested, and both use the block parameter f. This could cause things to get very strange (especially since the scoping of block parameters differs between ruby 1.8 and 1.9). It also seems like you don't actually want the forms nested. You should also probably fix the indentation in your html.

后知后觉 2024-10-10 17:51:19

show.html.erb-我将状态更新评论表单的第一行更改为:

<% form_for (s, s.comments.build) do |f| %>
...

class CommentsController < ApplicationController
  def create
    @statusupdate = statusupdate.find(params[:statusupdate_id])
    @comment = @statusupdate.comments.create(params[:comment])
    ...

我不完全知道这里发生了什么,但它对我有用〜希望它可以帮助别人〜

show.html.erb- I changed the top line of the status update comment form to:

<% form_for (s, s.comments.build) do |f| %>
...

class CommentsController < ApplicationController
  def create
    @statusupdate = statusupdate.find(params[:statusupdate_id])
    @comment = @statusupdate.comments.create(params[:comment])
    ...

I don't exactly know what's going on here but it worked for me~ hope it helps someone out~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文