dimanche 26 février 2017

Creating form that changes multiple models

So I have an assignment where I have to build an app with Ruby on Rials and i'm having some trouble with using forms.. The app basically is a video site that lets users upload a video URL and comment on these videos.

This is the database:

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

Here are the models:

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

The user controller:

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

The video controller:

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

And the comment controller:

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

Now I'm trying to make a form where a user can give a username (without any form of authentication) and a comment. I've managed to make a form that lets the user upload comments, without any username, but I've been having a lot of trouble with adding a username. This is what I have in my view so far:

<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 %>

I've googled this problem and some people have had the same problem, but I can't seem to find a solution that works for me.

Aucun commentaire:

Enregistrer un commentaire