Ruby on rails Rails:注释模型-连接到Micropost\u id和用户id
编辑 由于carlosramireziii帮助将表单更改为,路由错误已消失Ruby on rails Rails:注释模型-连接到Micropost\u id和用户id,ruby-on-rails,ruby,ruby-on-rails-3,routing,comments,Ruby On Rails,Ruby,Ruby On Rails 3,Routing,Comments,编辑 由于carlosramireziii帮助将表单更改为,路由错误已消失 <%= form_for (@micropost) do |f| %> <%= fields_for :comments do |ff| %> <%= ff.text_area :content %> <% end %> <div class="CommentButtonContainer"> <%= f.submit "Comment" %> &l
<%= form_for (@micropost) do |f| %>
<%= fields_for :comments do |ff| %>
<%= ff.text_area :content %>
<% end %>
<div class="CommentButtonContainer">
<%= f.submit "Comment" %>
</div>
<% end %>
这是我的路线。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 %>
<%= form_for(@micropost) do |f| %>
<%= f.fields_for :comments do |ff %>
<%= ff.text_area :content %>
<% end %>
<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 %>
匿名的
最后,这就是微柱中的内容
<div id='CommentContainer' class='Condensed2'>
<div class='Comment'>
<%= render "comments/form" %>
</div>
<div id='comments'>
<%= render @micropost.comments %>
</div>
</div>
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments
accepts_nested_attributes_for :comments
validates :user_id, presence: true
end
所有其他与我在下面发布的评论模型和控制器相关的内容,我已经考虑了很长时间,真的需要帮助,谢谢
评论模型
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注释
微成本模式
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 Micropost
用户模型
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用户:microposts,:source=>:comments
结束
评论控制器
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
class CommentsController
用户控制器
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
class UsersController
我认为问题在于,您试图在尚未保存的微柱上保存新的注释。由于您的注释
路径嵌套在microspost
路径下,因此在创建新注释之前需要存在microspost
如果要以相同的形式创建两个对象,则需要使用嵌套的模型属性
Micropost
<div id='CommentContainer' class='Condensed2'>
<div class='Comment'>
<%= render "comments/form" %>
</div>
<div id='comments'>
<%= render @micropost.comments %>
</div>
</div>
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments
accepts_nested_attributes_for :comments
validates :user_id, presence: true
end
class Micropost
表格
<%= form_for([@micropost, @micropost.comments.new]) do |f| %>
<%= f.text_area :content %>
<div class="CommentButtonContainer">
<%= f.submit "Comment" %>
</div>
<% 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 %>
谢谢你的建议,但是当添加
时,该字段消失,我无法发表评论。我实际上在f.fields\u中取出了f,出现了文本区域,但评论没有保存到microposting上