【问题标题】:Rails: Comment Model - Connected to a Micropost_id and User_idRails:评论模型 - 连接到 Micropost_id 和 User_id
【发布时间】:2012-03-13 11:00:10
【问题描述】:

编辑

由于 carlosramireziii 帮助将表单更改为

,路由错误已消失
<%= form_for (@micropost) do |f| %>
<%= fields_for :comments do |ff| %>
<%= ff.text_area :content %>
<% end %>
<div class="CommentButtonContainer">
<%= f.submit "Comment" %>
</div>
<% end %>

但现在的问题是帖子无法保存,有什么建议吗?


我目前正在制作一个连接到 a: 的 cmets 模型:

micropost_id :integeruser_id :integer

我继续收到的问题是,当我发布一些东西时,我得到了回报:

Routing Error

No route matches [POST] "/microposts/comments"

这是我的 routes.eb

Project::Application.routes.draw do
  resources :microposts do
    resources :comments
  end

这是意见表

<%= form_for([@micropost, @micropost.comments.new]) do |f| %>
<%= f.text_area :content %>
<div class="CommentButtonContainer">
<%= f.submit "Comment" %>
</div>
<% end %>

这是评论模板

<%= div_for comment do %> 
<div class='UserCommentContainer'>
<div class='UserComment'>
<div class='UserName sm'>
Anonymous
</div>
<div class='UserCommentText'>
<%= comment.content %>
</div>
</div>
</div>
<% end %>

最后,这就是 Micropost

里面的内容
<div id='CommentContainer' class='Condensed2'>
<div class='Comment'>
<%= render "comments/form" %>
</div>
<div id='comments'>
<%= render @micropost.comments %>
</div>
</div>

关于我在下面发布的 cmets 模型和控制器的所有其他内容,我已经考虑了很长时间,并且真的可以使用帮助,谢谢!

评论模型

class Comment < ActiveRecord::Base
  attr_accessible :content
  belongs_to :micropost

  validates :content, presence: true, length: { maximum: 140 }
  validates :user_id, presence: true
  validates :micropost_id, presence: true

  default_scope order: 'comments.created_at DESC'
end

微博模型

class Micropost < ActiveRecord::Base
  belongs_to :user
  has_many :comments

  validates :user_id, presence: true
end

用户模型

class User < ActiveRecord::Base
  has_many :microposts
  has_many :replies, :through => :microposts, :source => :comments
end

评论控制器

class CommentsController < ApplicationController 
  def create 
    @comment = @micropost.comments.new(params[:comment]) 
    if @comment.save 
      redirect_to @user
    else 
      redirect_to @user
    end 
  end 
end

用户控制器

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @micropost = Micropost.new
    @comment = @micropost.comments.new
    @microposts = @user.microposts.paginate(page: params[:page])
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 routing comments


    【解决方案1】:

    我认为问题在于您正在尝试在尚未保存的 micropost 上保存新的 comment。由于您的comments 路由嵌套在microposts 路由下方,因此micropost 需要存在才能创建新评论。

    如果要在同一个表单中创建两个对象,则需要使用嵌套模型属性。

    微博

    class Micropost < ActiveRecord::Base
      belongs_to :user
      has_many :comments
      accepts_nested_attributes_for :comments
    
      validates :user_id, presence: true
    end
    

    表格

    <%= form_for(@micropost) do |f| %>
       <%= f.fields_for :comments do |ff %>
          <%= ff.text_area :content %>
       <% end %>          
       <div class="CommentButtonContainer">
          <%= f.submit "Comment" %>
       </div>
    <% end %>
    

    【讨论】:

    • 感谢您的建议,但是当添加 &lt;%= f.fields_for :comments do |ff %&gt; &lt;%= ff.text_area :content %&gt; &lt;% end %&gt; 时,该字段消失,我无法发表评论
    • 我居然把f.fields_for里面的f去掉了,出现了textarea但是评论没有保存到微博上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多