So habe ich eine Aufgabe, wo ich eine App mit Ruby on Rials erstellen muss und ich habe einige Probleme mit der Verwendung von Formularen .. Die App ist im Grunde eine Video-Website, die Benutzer ein Video hochladen können URL und kommentiere diese Videos.Erstellen von Formular, das mehrere Modelle ändert
Dies ist die Datenbank:
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
Hier sind die Modelle:
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
Der Benutzer Controller:
class UsersController < ApplicationController
def new
@user = User.new
end
end
Der Videocontroller:
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
Und der Kommentar 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
Nun Ich versuche, eine Form zu machen, wo ein Benutzer (ohne irgendeine Form der Authentifizierung) und einen Kommentar einen Benutzernamen geben kann. Es ist mir gelungen, ein Formular zu erstellen, mit dem der Nutzer Kommentare ohne Benutzernamen hochladen kann, aber ich habe große Probleme mit dem Hinzufügen eines Benutzernamens. Dies ist, was ich in meiner Sicht haben bisher:
<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 %>
Ich habe dieses Problem gegoogelt und einige Leute haben das gleiche Problem hatte, aber ich kann nicht eine Lösung zu finden scheinen, die für mich funktioniert.