質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1872閲覧

createアクションがcontrollerに記載済みだが、投稿が反映されない

tomotomo_1D

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Webサイト

一つのドメイン上に存在するWebページの集合体をWebサイトと呼びます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2020/05/16 03:16

編集2020/05/16 04:02

前提・実現したいこと

rails アプリケーションでCRUD操作を含んだwebアプリを作成しています。

現在おすすめ映画共有サイトを作成しているのですが、
新規投稿ページで『日本映画』『韓国映画』『海外映画』『海外ドラマ』の4つのジャンルを作り、(radio_button)
作品の紹介をした入力し、投稿ボタンを押すと、それぞれのジャンルで表示できるようにしたい。

発生している問題・エラーメッセージ

新規投稿ページの投稿ボタンを押してもページが飛ばず投稿されない。
エラーメッセージ

### 該当のソースコード 新規投稿ページのボタンを押すとターミナルの方で以下の文字が出てきます。 AbstractController::ActionNotFound (The action 'create' could not be found for PostsController): ```Ruby createアクションはpostcontrollerに記載済みでした。 ソースコード class PostsController < ApplicationController #before_action :authenticate_user! def index #@posts=Post.all.order(created_at: :desc) @post=Post.where(genre: '日本映画・ドラマ') end def indexb @postb=Post.where(genre: '韓国映画・ドラマ') end def indexc @postc=Post.where(genre: '海外ドラマ') end def indexd @postd=Post.where(genre: '海外映画') end def new @post=Post.new end def create @post=Post.new(post_params) if @post.save redirect_to controller: :home, action: :top else redirect_to controller: :posts, action: :new end end private def post_params params.require(:post).permit(:genre,:title,:released_year,:image,:summary,:director,:cast,:casta,:castb) end end **nwe.html.erb** <div class="posts-new"> <div class="form-container"> <h1 class="form-heading">Tell Your Favotite!</h1> <div class="form-content"> <%= form_with model: @post,url: {controller:'posts',action: 'create'} do |form| %> <%= form.radio_button :genre, :"日本映画・ドラマ"%> <%= form.label :genre,"日本映画・ドラマ",{style: "display: inline-block;"}%> <%= form.radio_button :genre, :"韓国映画・ドラマ"%> <%= form.label :genre,"韓国映画・ドラマ",{style: "display: inline-block;"}%> <%= form.radio_button :genre, :"海外ドラマ"%> <%= form.label :genre,"海外ドラマ",{style: "display: inline-block;"}%> <%= form.radio_button :genre, :"海外映画"%> <%= form.label :genre,"海外映画",{style: "display: inline-block;"}%> <br> <br> <%= form.label :title,"~タイトル~ " %><br> <%= form.text_field :title,class:"m-title",size:"40"%> <br> <br> <%= form.label :released_year,"~公開年~"%><br> <%= form.date_select :released_year,{start_year: 2020,end_year:2020-40,discard_month:true,discard_day:true},class:"released-year"%> <br> <br> <%= form.label :image,"~画像~"%><br> <%= form.file_field :image %> <br> <br> <%= form.label :summary,"~あらすじ・レビュー~"%><br> <%= form.text_area :summary,class:"m-summary",size:"40x20"%> <br> <br> <%= form.label :director,"~監督名~"%><br> <%= form.text_field :director,class:"m-director",size: 40%> <br> <br> <%= form.label :cast,"~キャスト名~"%><br> <%= form.text_field :cast,class:"m-cast1",size: 40%><br> <%= form.text_field :casta,class:"m-cast2",size: 40%><br> <%= form.text_field :castb,class:"m-cast3",size: 40%> <br> <br> <div class="m-submit"> <%= form.submit "投稿",class:"submit"%> </div> <%end%> </div> </div> </div> **routes.rb** Rails.application.routes.draw do devise_for :users get "/" => "home#top" resources :posts, only: [:index, :indexb, :indexc, :indexd, :new, :create] end ### 試したこと 投稿時のジャンル分けの方法としては、index,indexb,indexc,indexdのようにアクションを4つ作りそれぞれのviewで表示されるようにしています。 ### 補足情報(FW/ツールのバージョンなど) macbook ver10.13.6 rails 6.0.2.2 初めての質問で拙い文章ですがよろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Yasumichi

2020/05/16 03:26

ソースコードは以下のようにテキストではりつけてください。 ```ruby ここにソースコードを貼り付ける ``` 対応しているMarkdownの記法を知りたい https://teratail.com/help#about-markdown あと $ rails routes の実行結果も提示いただけますか?
tomotomo_1D

2020/05/16 03:50

お教えいただきありがとうございます。 ひとまず以下が$rails routesの結果です。 Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create GET / home#top posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create 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
winterboum

2020/05/16 11:59

Yasumiciさんの指摘のように「ソースコードは以下のようにテキストではりつけてください。」してください。 読みにくいだけでなく、素のcodeと違うものになっている可能性があるので、今回のように「あるのに無いと言われる」場合に原因発見が困難になります。
tomotomo_1D

2020/05/16 12:05

ご指摘ありがとうございます。解答しづらくしてしまい申し訳ございません。 teratail の使い方がよくわかっておらず次回の質問からはしっかり理解してから質問を行うようにいたします。 この問題に関しましては、通っているプログラミング教室のメンターの方と協力して解決することができました。後ほど解答方法を記入しようと思います。 ご協力ありがとうございました!
guest

回答1

0

自己解決

Ruby

1def create 2 @post=Post.new(post_params) 3 if @post.save 4 redirect_to :action => "index" 5 else 6 redirect_to("/posts/new") 7 end 8 end

のようにredirect_to以下をacton=>indexとするとそらぞれのindexに飛べました

投稿2020/05/17 04:21

tomotomo_1D

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問