2017-02-26 113 views
1

所以我有一个任务,我必须在Rials上构建一个Ruby应用程序,并且在使用表单时遇到了一些麻烦。该应用程序基本上是一个视频网站,可让用户上传视频网址和评论这些视频。创建更改多个模型的表格

这是数据库:

ActiveRecord::Schema.define(version: 20170226161051) do 

    create_table "comments", force: true do |t| 
    t.text  "body" 
    t.integer "video_id" 
    t.integer "user_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "comments", ["user_id"], name: "index_comments_on_user_id" 
    add_index "comments", ["video_id"], name: "index_comments_on_video_id" 

    create_table "users", force: true do |t| 
    t.string "name" 
    t.string "mailaddress" 
    t.string "birthday" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "videos", force: true do |t| 
    t.string "title" 
    t.string "url" 
    t.text  "description" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

下面是型号:

class Comment < ActiveRecord::Base 
    belongs_to :video 
    belongs_to :user 
end 

class User < ActiveRecord::Base 
    has_many :comments 
end 

class Video < ActiveRecord::Base 
    has_many :comments 
end 

用户控制器:

class UsersController < ApplicationController 
    def new 
    @user = User.new 
    end 
end 

视频控制器:

class VideosController < ApplicationController 

    def new 
    @video = Video.new 
    end 

    def create 
    @video = Video.new(video_params) 

    if @video.save 
     redirect_to @video 
    else 
     render 'new' 
    end 
    end 

    def show 
    @video = Video.find(params[:id]) 
    end 

    def index 
    @video = Video.all 
    end 

    def edit 
    @video = Video.find(params[:id]) 
    end 

    def update 
    @video = Video.find(params[:id]) 
    if @video.update(params[:video].permit(:title, :url, :description)) 
     redirect_to @video 
    end 
    end 

    def destroy 
    @video = Video.find(params[:id]) 
    @video.destroy 

    redirect_to videos_path 
    end 

    private 
    def video_params 
     params.require(:video).permit(:title, :url, :description) 
    end 

end 

和注释控制器:

class CommentsController < ApplicationController 
    def create 
    @video = Video.find(params[:video_id]) 
    @comment = @video.comments.create(params[:comment].permit(:body)) 
    redirect_to video_path(@video) 
    end 
end 

现在,我试图让一个表单,用户可以得到一个用户名(没有任何形式的认证)和注释。我设法制作了一个表单,允许用户上传评论,但没有任何用户名,但是我添加用户名时遇到了很多麻烦。这是我在我看来,到目前为止:

<h1> 
    <strong>Title:</strong> 
    <%= @video.title %> 
</h1> 

<p> 
    <%= video_tag @video.url, controls: true, autobuffer: true %> 
</p> 

<p> 
    <strong>Description</strong> 
    <%= @video.description%> 
</p> 

<h2>Comments</h2> 
<% @video.comments.each do |comment|%> 
    <p> 
     <%= comment.body %> 
    </p> 
<%end%> 

<h2> Add a comment</h2> 
<%= form_for([@video, @video.comments.build]) do |f|%> 

    <p> 
     <%= f.label :body %><br /> 
     <%= f.text_area :body %> 
    </p> 

    <p> 
     <%= f.submit %> 
    </p> 
<% end %> 

<%= link_to 'Back to list of videos', videos_path %> 

我GOOGLE了这个问题,有人有过同样的问题,但我似乎无法找到适合我的解决方案。

回答

0

这是错误的设计。

form_for @comment ... 

不要忘了与@comment = Comment.new

定义在控制器中的@comment你eather,通过表单的动作视频ID槽路线(或与形式隐藏输入。

编辑:重新阅读你的问题,你已经覆盖了视频通过,你所需要做的就是指出正确的控制器,它应该工作