导轨上的短片用户评论 [英] Rails User Comments on Movies
问题描述
我有三个支架
用户 注释和电影
在我的应用程序,我想的用户到注释的关于电影
,和不同的用户可以的评论的一个电影
页
In my app, I want Users to Comment on Movies
, and different users can Comment on a Movie
page.
我将如何打造联想code,让用户在电影添加注释,然后电影页面显示所有评论?请你也告诉我的code来算的意见,所以显示有多少意见有在整数显示这些
How would i create the associations code that lets users add comments on movies and then on the movie page display all comments? COuld you please also tell me the code to count the comments, so show how many comments there are and display them in integers
我已经得到了迄今
注释表主题和正文
电影表
用户表
Comments table with Subject and Body Movies table Users table
user.rb
has_many: comments
movies.rb
movies.rb
has_many: comments
comments.rb
comments.rb
belongs_to :users
belongs_to :movies
谢谢!
推荐答案
您需要的关联是告诉他们属于什么。所以你需要做的模型如下:
The associations you would need is to tell what they belong to. so you would need to do the following in the models:
注释型号:
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :movie
end
用户模型:
class User < ActiveRecord::Base
has_many :comments
end
电影型号:
class Movie < ActiveRecord::Base
has_many :comments
end
您需要生成迁移外键列添加到注释表。一旦你的,你需要做的是通过自己的ID附加注释电影和用户。然后把他们的意见显示,在视图中:
You would need to generate migrations to add the foreign key columns to the comment table. Once you have that, all you need to do is attach the comments to the movies and users through their id. Then to have them the comments display, in the view:
<% @movie.comments.each do |comment| %>
<%= comment.text %>
<% end %>
编辑:要创建注释,你需要一个链接添加一个新的注释。在视图:
To create a comment, you would need a link to add a new comment. in the view:
<%= link_to 'New Comment', new_movie_comment_path(@movie) %>
这应该带你到新的注释视图,并为它的形式。在表单中,您可以通过设置用户评论相关联的隐藏字段用户评论关联。在评论表单视图:
That should take you to the new comment view and the form for it. In the form, you can associate the comment to the user by setting a hidden field that associates the user to the comment. In the comment form view:
<%= form_for(@comment) do |f| %>
<%= f.label :user %>
<%= f.hidden_field :comment, :user_id, current_user_id %>
<% end %>
最后一部分假定您有活动的会话。
The last part assumes you have a session active.
编辑2:
在路线,你要嵌套的电影资源中的注释资源:
In the routes, you would nest the comments resource inside the movies resource:
resources :movies do
resources :comments
end
编辑3:
在您的意见控制器,你必须来点动作电影。在控制器
In your comments controller, you'll have to point the actions to the movie. In the controller
class CommentsController < ApplicationController
before_filter :load_movie
private
def load_movie
@movie = Movie.find(params[:movie_id])
end
的专用段需要在控制器的底部。与完成后,更新使用@movie的操作。
The private section needs to be at the bottom of the controller. With that done, update the actions to use the @movie.
def index
@comments = @movie.comments.all
end
做那显示,在控制器中新等行动。在创建操作和更新操作,你需要更新HTML重定向。
Do that for show, new, etc actions in the controller. In the create action and update action, you'll need to update the html redirect.
format.html { redirect_to (@movie, @comment), notice: 'Comment was successfully created.' }
和
format.html { redirect_to (@movie, @comment), notice: 'Comment was successfully Updated.' }
这篇关于导轨上的短片用户评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!