如何使评论表单在我的主页上正常显示和工作?
How do I make a comment form appear and work correctly in my homepage?
我有一个 Post 模型和一个属于 Post 的评论模型。我能够在与家庭控制器和家庭操作以及 User/show 视图相对应的家庭视图中显示 Post。因此,在主页和用户视图中,post 是按创建时间顺序列出的。我还能够在用户和主页视图中都有一个 post 表单。
当我尝试在主视图和用户视图中每个显示 Post 下方显示评论表单时出现问题。 home controller/home action 已经有一个用于显示新 post 表单的变量 @post ,这使得很难为要创建的评论启动另一个 post 实例变量。评论表单应位于相应文章下方,从而为该文章创建评论。
如何在主页视图中实现它?如何启动处理评论表单所需的 Post 和评论变量?
这是我的主视图:app/views/home/home。html.erb:
<% if logged_in? %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<hr/>
<br/>
<section class="stats">
<%= render 'shared/stats' %>
</section>
<section class="post_form">
<%= render 'shared/post_form' %>
</section>
</aside>
<div class="col-md-8">
<h3>Post Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center jumbotron">
<h1>Welcome to the Unstarv website</h1>
<h2>
Please sign up now to use this site
<%= link_to "Sign Up", signup_path =%>
now.
</h2>
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
<%= link_to image_tag("rails.png", alt: "unstarv logo"),
'#' %>
<% end %>
这是我的家庭控制器:
class HomeController < ApplicationController
def home
if logged_in?
@post = current_user.posts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
def about
end
def privacy
end
def terms
end
end
这是我的 Post 模型,相关部分:
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
end
我的用户模型的相关部分:
class User < ActiveRecord::Base
attr_accessor :remember_token
before_save { self.email = email.downcase }
has_many :posts, dependent: :destroy
has_many :comments
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
validates :username, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }, allow_blank: true
def feed
following_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
Post.where("user_id IN (#{following_ids})
OR user_id = :user_id", user_id: id)
end
end
这是我的评论模型:
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
default_scope -> { order(created_at: :desc) }
end
这里是 post 控制器:
class PostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
@comment = Comment.new
@comment.post_id = @post.id
@comments = @post.comments.all
end
def new
@post = Post.new
end
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created!"
redirect_to root_url
else
@feed_items = []
render 'home/home'
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
@post.update(post_params)
flash.notice = "Post '#{@post.title}' Updated!"
render 'home/home '
end
def update
@post = Post.find(params[:id])
@post.update(post_params)
flash.notice = "Post '#{@post.title}' Updated!"
redirect_to root_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body, :picture)
end
end
这是我的 app/views/post/_post.html.erb 文件,
<li id="post-<%= post.id %>">
<span class="user"><%= link_to post.user.username, post.user %></span>
<span class="content">
<%= post.title %>
<%= post.body %>
<%= image_tag post.picture.url if post.picture? %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(post.created_at) %> ago.
<% if current_user?(post.user) %>
<%= link_to "delete", post, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</span>
<section>
<h2>Your Comments here</h2>
<h3>Post a Comment</h3>
<h3>Post a Comment</h3>
<%= render 'shared/comment_form' %>
<% post.comments.all.each do |comment| %>
<h4><small>Comment by</small> <%= comment.post.user.username %></h4>
<p class="comment"><%= comment.body %></p>
<p><small>Posted <%= distance_of_time_in_words(Time.now, comment.created_at) %> ago</small></p>
<br/>
<%end%>
</li>
这是我的 app/views/shared/comment_form_html.erb ,这似乎是问题的一部分,因为实例变量未正确初始化:
<%= form_for [ @post1, @comment] do |f| %>
<p>
<%= f.label :body, "Your Comment" %><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit 'Submit' . method="post", class: 'btn btn-primary' %>
</p>
<% end %>
最后是我的足迹:
actionview (4.1.8) lib/action_view/helpers/form_helper.rb:423:in `form_for'
app/views/shared/_comment_form.html.erb:1:in `_app_views_shared__comment_form_html_erb__972919349_34226064'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8)lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:278:in `render'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.1.8) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/posts/_post.html.erb:22:in `_app_views_posts__post_html_erb__365860364_28803540'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:399:in `block in collection_with_template'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:395:in `map'
非常感谢您的帮助!!!!
在 _post.html.erb
中,将 <%= render 'shared/comment_form' %>
更改为 render 'shared/comment_form', post: post
然后在 _comment_form_html.erb
中,将 [@post1, @comment]
更改为 [post, @comment]
编辑
您还需要将以下行添加到 HomeController#home
@comment = Comment.new
编辑
issue/question 是如何生成一个实例以在每个评论表单中使用的,对吗?
由于您正在迭代每个 post <%= render 'shared/feed' %>
,您已经有一个 post 实例 ,您正在 [=] 中访问该实例19=],实例名称为post
.
当您尝试呈现 comment_form
时,您可以将参数传递给 render
方法,您可以发送一个名为 locals
的参数,或者只发送参数 name=value
] 这是一条捷径。
如果您呈现 comment_form
发送当前的 post
那么您可以根据 post.
生成表单
因为现在你可以访问 post
到 shared/_comment_form.html.erb
,当前的问题是如何生成一个 comment
实例,一种方法是在你想要的每个控制器操作中实例注释显示提要(如果在多个位置使用它是一个糟糕的选择)并且是我在评论中所说的(我的错误)。我认为最好的选择是将 [@post1, @comment]
(您的原始解决方案)更改为 [post, post.comments.build]
它将如何运作?由于您已经可以访问 post
,因此您只需在视图
中为该 post
创建一个空评论
我有一个 Post 模型和一个属于 Post 的评论模型。我能够在与家庭控制器和家庭操作以及 User/show 视图相对应的家庭视图中显示 Post。因此,在主页和用户视图中,post 是按创建时间顺序列出的。我还能够在用户和主页视图中都有一个 post 表单。
当我尝试在主视图和用户视图中每个显示 Post 下方显示评论表单时出现问题。 home controller/home action 已经有一个用于显示新 post 表单的变量 @post ,这使得很难为要创建的评论启动另一个 post 实例变量。评论表单应位于相应文章下方,从而为该文章创建评论。
如何在主页视图中实现它?如何启动处理评论表单所需的 Post 和评论变量?
这是我的主视图:app/views/home/home。html.erb:
<% if logged_in? %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<%= render 'shared/user_info' %>
</section>
<hr/>
<br/>
<section class="stats">
<%= render 'shared/stats' %>
</section>
<section class="post_form">
<%= render 'shared/post_form' %>
</section>
</aside>
<div class="col-md-8">
<h3>Post Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
<% else %>
<div class="center jumbotron">
<h1>Welcome to the Unstarv website</h1>
<h2>
Please sign up now to use this site
<%= link_to "Sign Up", signup_path =%>
now.
</h2>
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
<%= link_to image_tag("rails.png", alt: "unstarv logo"),
'#' %>
<% end %>
这是我的家庭控制器:
class HomeController < ApplicationController
def home
if logged_in?
@post = current_user.posts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
def about
end
def privacy
end
def terms
end
end
这是我的 Post 模型,相关部分:
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
end
我的用户模型的相关部分:
class User < ActiveRecord::Base
attr_accessor :remember_token
before_save { self.email = email.downcase }
has_many :posts, dependent: :destroy
has_many :comments
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
validates :username, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }, allow_blank: true
def feed
following_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
Post.where("user_id IN (#{following_ids})
OR user_id = :user_id", user_id: id)
end
end
这是我的评论模型:
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
default_scope -> { order(created_at: :desc) }
end
这里是 post 控制器:
class PostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
def index
@posts = Post.all
end
def show
@post = Post.find(params[:id])
@comment = Comment.new
@comment.post_id = @post.id
@comments = @post.comments.all
end
def new
@post = Post.new
end
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = "Post created!"
redirect_to root_url
else
@feed_items = []
render 'home/home'
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
@post.update(post_params)
flash.notice = "Post '#{@post.title}' Updated!"
render 'home/home '
end
def update
@post = Post.find(params[:id])
@post.update(post_params)
flash.notice = "Post '#{@post.title}' Updated!"
redirect_to root_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body, :picture)
end
end
这是我的 app/views/post/_post.html.erb 文件,
<li id="post-<%= post.id %>">
<span class="user"><%= link_to post.user.username, post.user %></span>
<span class="content">
<%= post.title %>
<%= post.body %>
<%= image_tag post.picture.url if post.picture? %>
</span>
<span class="timestamp">
Posted <%= time_ago_in_words(post.created_at) %> ago.
<% if current_user?(post.user) %>
<%= link_to "delete", post, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</span>
<section>
<h2>Your Comments here</h2>
<h3>Post a Comment</h3>
<h3>Post a Comment</h3>
<%= render 'shared/comment_form' %>
<% post.comments.all.each do |comment| %>
<h4><small>Comment by</small> <%= comment.post.user.username %></h4>
<p class="comment"><%= comment.body %></p>
<p><small>Posted <%= distance_of_time_in_words(Time.now, comment.created_at) %> ago</small></p>
<br/>
<%end%>
</li>
这是我的 app/views/shared/comment_form_html.erb ,这似乎是问题的一部分,因为实例变量未正确初始化:
<%= form_for [ @post1, @comment] do |f| %>
<p>
<%= f.label :body, "Your Comment" %><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit 'Submit' . method="post", class: 'btn btn-primary' %>
</p>
<% end %>
最后是我的足迹:
actionview (4.1.8) lib/action_view/helpers/form_helper.rb:423:in `form_for'
app/views/shared/_comment_form.html.erb:1:in `_app_views_shared__comment_form_html_erb__972919349_34226064'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8)lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:278:in `render'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.1.8) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/posts/_post.html.erb:22:in `_app_views_posts__post_html_erb__365860364_28803540'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:399:in `block in collection_with_template'
actionview (4.1.8) lib/action_view/renderer/partial_renderer.rb:395:in `map'
非常感谢您的帮助!!!!
在 _post.html.erb
中,将 <%= render 'shared/comment_form' %>
更改为 render 'shared/comment_form', post: post
然后在 _comment_form_html.erb
中,将 [@post1, @comment]
更改为 [post, @comment]
编辑
您还需要将以下行添加到 HomeController#home
@comment = Comment.new
编辑
issue/question 是如何生成一个实例以在每个评论表单中使用的,对吗?
由于您正在迭代每个 post <%= render 'shared/feed' %>
,您已经有一个 post 实例 ,您正在 [=] 中访问该实例19=],实例名称为post
.
当您尝试呈现 comment_form
时,您可以将参数传递给 render
方法,您可以发送一个名为 locals
的参数,或者只发送参数 name=value
] 这是一条捷径。
如果您呈现 comment_form
发送当前的 post
那么您可以根据 post.
因为现在你可以访问 post
到 shared/_comment_form.html.erb
,当前的问题是如何生成一个 comment
实例,一种方法是在你想要的每个控制器操作中实例注释显示提要(如果在多个位置使用它是一个糟糕的选择)并且是我在评论中所说的(我的错误)。我认为最好的选择是将 [@post1, @comment]
(您的原始解决方案)更改为 [post, post.comments.build]
它将如何运作?由于您已经可以访问 post
,因此您只需在视图
post
创建一个空评论