まずActiveModel::ForbiddenAttributesErrorが出ており、permitが無いとエラーが出るという投稿を見たため以下のようなコードを追加しました
private def post_params params.permit(:post) end
すると今度はNoMethodError in Posts#showが出てきて、それまでは正常に作動していた部分がエラーであると示されるようになり、何が問題であるのか分からず投稿させていただきました。
他のコードを以下に貼り付けておきます。宜しくお願いします。
posts_controller.rb
class PostsController < ApplicationController def index @posts = Post.all end def show @post = Post.find_by(id: params[:id]) end def create @post= Post.new(post: params[:post][:post]) if @post.save redirect_to("/posts") else render'/posts/new' end end def new @post=Post.new end def edit @post=Post.find_by(id: params[:id]) end def update @post = Post.find_by(id: params[:id]) @post.assign_attributes(post_params) @post.save redirect_to("/posts/index") end private def post_params params.permit(:post) end end
edit.html.erb
<h1>this is Posts edit.html.erb</h1> <div class ="container"> <div class ="row justify-content-center"> <div class="col-md-6"> <%= form_for @post do |f| %> <%= f.label "投稿内容" %> <div class ="text_field"> <%= f.text_field :post, :size=>"50x500" %> </div> <%= f.submit "編集完了!", class: "btn btn-primary" %> <% end %> </div> </div> </div>
new.html.erb
<div class="container"> <h1>投稿ページ</h1> <div class ="row justify-content-center"> <div class="col-md-6"> <p>ここに入力して下さい↓↓↓</p> <%= form_for @post do |f| %> <%= f.label "投稿内容" %> <%= f.text_field :post %> <%= f.submit "投稿する!", class: "btn btn-primary" %> <% end %> </div> </div> </div>
show.html.erb
<div class="container"> <h1>this is Posts show.html.erb</h1> <div class="row justify-content-center"> <div class="col-md-6"> <div class="each-posts"> <%= @post.post %> <p><%= @post.created_at %></p> <p><%= link_to "編集する", "/posts/#{@post.id}/edit" %></p> </div> </div> </div> </div>
Gemfile
source 'https://rubygems.org' gem 'rails', '5.1.6' gem 'bootstrap' gem 'flexbox-rails' gem 'puma', '3.9.1' gem 'sass-rails', '5.0.6' gem 'uglifier', '3.2.0' gem 'coffee-rails', '4.2.2' gem 'jquery-rails', '4.3.1' gem 'turbolinks', '5.0.1' gem 'jbuilder', '2.6.4' group :development, :test do gem 'sqlite3', '1.3.13' gem 'byebug', '9.0.6', platform: :mri end group :development do gem 'web-console', '3.5.1' gem 'listen', '3.1.5' gem 'spring', '2.0.2' gem 'spring-watcher-listen', '2.0.1' end # Windows環境ではtzinfo-dataというgemを含める必要があります gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
routes.rb
Rails.application.routes.draw do root 'static_pages#home' get 'static_pages/home' get 'static_pages/about' resources :users resources :posts # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
ちなみにログは以下のようになっています。
@post.postがおかしいと指摘されています。
Started PATCH "/posts/3" for 182.169.182.77 at 2020-02-05 04:05:19 +0000 Cannot render console from 182.169.182.77! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"59L+eb7ZGGwKKsflK5wR21LNIDGsVXlknVjDRvZN/TOQpjRT+2q9YknU1QdAlois8SxZarj2iFlST4xtyFTLyg==", "post"=>{"post"=>"3つ目の投稿だよ!!!!!!!!!!!!!!!!!"}, "commit"=>"編集完了!", "id"=>"3"} Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] Unpermitted parameters: :utf8, :_method, :authenticity_token, :post, :commit, :id (0.1ms) begin transaction (0.0ms) commit transaction Redirected to https://ae33995f099d4ca0a2f9f7e5fe584a1c.vfs.cloud9.us-east-2.amazonaws.com/posts/index Completed 302 Found in 2ms (ActiveRecord: 0.2ms) Started GET "/posts/index" for 182.169.182.77 at 2020-02-05 04:05:19 +0000 Cannot render console from 182.169.182.77! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#show as HTML Parameters: {"id"=>"index"} Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]] Rendering posts/show.html.erb within layouts/application Rendered posts/show.html.erb within layouts/application (2.2ms) Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.1ms) ActionView::Template::Error (undefined method `post' for nil:NilClass): 5: <div class="col-md-6"> 6: <div class="each-posts"> 7: 8: <%= @post.post %> 9: <p><%= @post.created_at %></p> 10: <p><%= link_to "編集する", "/posts/#{@post.id}/edit" %></p> 11: </div> app/views/posts/show.html.erb:8:in `_app_views_posts_show_html_erb__4156350255315023459_70020181469260'
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/02/05 09:49