前提・実現したいこと
form_forで投稿した値がcreateアクションに行かず
newアクションが呼び出されてしまい、createアクションが実行されない。
実現したい事としてはformに入力した内容を正常にcreateアクションに渡す事です。
どう解決していいか不明なため、ご質問させていただきました。
発生している問題・エラーメッセージ
エラーメッセージ createアクションが実行されず、再びnewアクションが呼び出されてしまう。 その際のURLが以下のようになる。 tweets/new?utf8=✓&authenticity_token=5IQwYybmuQj6K1kJJfUkV%2BnEO74h4%2BAQaMIogij%2BmEQ70%2BzvPi9ebMdO4SSCsck6%2BjIgWJ5glLNQOqPm2TvM%2FQ%3D%3D&tweet%5Bname%5D=aa&tweet%5Btext%5D=aaa&commit=投稿 以下debbugerのparamsコマンドを実行した際の値 "controller"=>"tweets", "action"=>"new"} permitted: false>
該当のソースコード
tweets_controller.rb
class TweetsController < ApplicationController def index @tweets = Tweet.order('created_at desc') end def new @tweets = Tweet.new end def create @tweet = Tweet.new(tweet_params) @tweet.save end private def tweet_params params.require(:tweet).permit(:text, :name) end end
new.html.erb
<div class="contents"> <h2>投稿する</h2> <form class="clearfix"> <%= form_for (@tweets) do |form| %> <%= form.text_field :name %> <%= form.text_area :text,rows:"4",cols:"40" %> <%= form.submit "投稿" %> <%end %> </form> </div>
routes.rb
Rails.application.routes.draw do resources :tweets end
rails routes
Prefix Verb URI Pattern Controller#Action tweets GET /tweets(.:format) tweets#index POST /tweets(.:format) tweets#create new_tweet GET /tweets/new(.:format) tweets#new edit_tweet GET /tweets/:id/edit(.:format) tweets#edit tweet GET /tweets/:id(.:format) tweets#show PATCH /tweets/:id(.:format) tweets#update PUT /tweets/:id(.:format) tweets#update DELETE /tweets/:id(.:format) tweets#destroy rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
試したこと
ここに問題に対して試したことを記載してください。
newアクションにdebuggerを仕込んでparamsを表示。
(byebug) params
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"5A2TPiLu9YN7IlKoPVzp8ERevGjvQc2V9aM+uCSonCk7Wk+yOicS50ZH6oWaGASdV6injlDCuTbNW7Xc1W3IkA==", "tweet"=>{"name"=>"aaaa", "text"=>"aaaaaaa"}, "commit"=>"投稿", "controller"=>"tweets", "action"=>"new"} permitted: false>
補足情報(FW/ツールのバージョンなど)
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
Rails 5.2.1.1

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/03 11:12
退会済みユーザー
2018/12/03 11:56
退会済みユーザー
2018/12/03 11:58
2018/12/03 12:40