#解決したいこと
下記のエラーを解消して、youtubeの動画を投稿できるようにしたいです。カラムにyoutube_urlは定義してあるのでなぜエラーになるのかわかりません!
Started PUT "/__web_console/repl_sessions/db9f799215cc6c438ac454364e2c021f" for ::1 at 2019-12-19 10:38:03 +0900 Started PUT "/__web_console/repl_sessions/db9f799215cc6c438ac454364e2c021f" for ::1 at 2019-12-19 10:38:06 +0900 Started GET "/admin/articles/new" for ::1 at 2019-12-19 10:51:08 +0900 (0.2ms) SELECT sqlite_version(*) Processing by Admin::ArticlesController#new as HTML Member Load (1.7ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/application_controller.rb:4:in `current_member' Rendering admin/articles/new.html.erb within layouts/application Rendered shared/_errors.html.erb (Duration: 2.1ms | Allocations: 178) Rendered admin/articles/_form.html.erb (Duration: 10.4ms | Allocations: 2231) Rendered admin/articles/new.html.erb within layouts/application (Duration: 14.7ms | Allocations: 2694) Completed 500 Internal Server Error in 33ms (ActiveRecord: 1.7ms | Allocations: 5065) ActionView::Template::Error (undefined method `youtube_url' for #<Article:0x00007fa285c73768>): 7: </tr> 8: <tr> 9: <th><%= form.label :youtube_url %></th> 10: <td><%= form.url_field :youtube_url, size: 20 %></td> 11: </tr> 12: <tr> 13: <th><%= form.label :body %></th> app/views/admin/articles/_form.html.erb:10 app/views/admin/articles/new.html.erb:5 app/views/admin/articles/new.html.erb:4
form関連
erb
1<%= form_for [:admin,@article] do |form|%> 2 <%= render "form", form: form %> 3 <div><%= form.submit %></div> 4<%end%>
erb
1<tr> 2 <th><%= form.label :youtube_url %></th> 3 <td><%= form.url_field :youtube_url, size: 20 %></td> 4 </tr>
コントローラー
rb
1class Admin::ArticlesController < Admin::Base 2 3 4 def index 5 @articles = Article.order(released_at: :desc).page(params[:page]).per(5) 6 end 7 8 def show 9 @article = Article.find(params[:id]) 10 end 11 12 def new 13 @article = Article.new 14 end 15 16 def edit 17 @article = Article.find(params[:id]) 18 end 19 20 def create 21 @article = Article.new(article_params) 22 23 url = params[:article][:youtube_url] 24 url = url.last(11) 25 @article.youtube_url = url 26 27 if @article.save 28 redirect_to [:admin, @article], notice: "ニュースを登録しました" 29 else 30 render "new" 31 end 32 end 33 34 def update 35 @article = Article.find(params[:id]) 36 @article.assign_attributes(article_params) 37 if @article.save 38 redirect_to [:admin, @article], notice: "ニュースを更新しました。" 39 else 40 render "edit" 41 end 42 end 43 44 def destroy 45 @article = Article.find(params[:id]) 46 @article.destroy 47 redirect_to :admin_articles 48 end 49 50 private def article_params 51 params.required(:article).permit( 52 :title, 53 :body, 54 :released_at, 55 :no_expiration, 56 :expired_at, 57 :member_only 58 ) 59 end 60end
db
rb
1create_table "articles", force: :cascade do |t| 2 t.string "title", null: false 3 t.text "body", null: false 4 t.datetime "released_at", null: false 5 t.datetime "expired_at" 6 t.boolean "member_only", default: false, null: false 7 t.datetime "created_at", precision: 6, null: false 8 t.datetime "updated_at", precision: 6, null: false 9 t.string "youtube_url" 10 end
回答1件
あなたの回答
tips
プレビュー