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

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

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

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

Q&A

解決済

2回答

470閲覧

createされない

yukireonsousi

総合スコア3

Ruby on Rails

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

0グッド

0クリップ

投稿2020/10/26 07:52

編集2020/10/26 23:35

以前はcreateできていたのに、今回入力したら、元の画面に戻され、ターミナルに下記のようなエラーが出ます。
変化と言えば、ルーティングで、**resources :users, only: [:show]**を追加それらに関するcontroller,viewを追加したことです

ActionView::MissingTemplate (Missing template baggages/new, application/new with {:locale=>[:en], :formats=>[:js, :html, :text, :js, :css, :ics, :csv, :vcf, :vtt, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :mp3, :ogg, :m4a, :webm, :mp4, :otf, :ttf, :woff, :woff2, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder, :haml]}. Searched in: * "/Users/arimotoyuuki/projects/fusion/app/views" * "/Users/arimotoyuuki/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/kaminari-core-1.2.0/app/views" * "/Users/arimotoyuuki/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise-4.7.2/app/views" ): app/controllers/baggages_controller.rb:33:in `create'

コントローラー

class BaggagesController < ApplicationController before_action :if_not_admin before_action :set_baggage, only: [:edit, :update, :show, :destroy] # before_action :move_to_index,only: [:show] def index if current_user.admin? # @baggages = Baggage.search(params[:search]) @search = User.ransack(params[:q]) #:qは入力したクエリのq @users = @search.result#検索した"結果"ユーザーのparams丸ごと # @baggages = Baggage.find(params[:user_id]) end end def new if current_user.admin? @baggage = Baggage.new(baggage_params) end end def create # @baggage = current_user.baggages.build(baggage_params) # @baggage = Baggage.new.(user_id: @user.id) # binding.pry @baggage = Baggage.new(baggage_params) if @baggage.save redirect_to pages_show_path(@baggage) else render :new end end def edit # if current_user.admin? # end end def update if @baggage.update(baggage_params) redirect_to pages_show_path(@baggage) else render :edit end end def show # binding.pry # @baggage = Baggage.find_by(id: params[:id]) # @user = User.find_by(id: @baggage.user_id) end def destroy end private def if_not_admin redirect_to root_path unless current_user.admin?#管理者ユーザー以外が特定のアクションを実行しようとした場合トップページにリダイレクトされる end def set_baggage @baggage = Baggage.find(params[:id])#edit, show, destroy などのアクションで使用する変数をセットします。 end def baggage_params params.permit( :kind,:storage_period, :code, :user_id )#.merge(user_id: current_user.id) end # def move_to_index # redirect_to action: :index unless user_signed_in? && current_user.id == @baggage.user_id # end end

routes.rb

Rails.application.routes.draw do # get 'mypages/index' root 'pages#index' get 'pages/show' devise_for :users resources :baggages resources :users, only: [:show] namespace :admin do resources :baggages do end end end

リダイレクト先をroot_pathにしてみましたが変化ありません

エラーで無いと言われていたファイルです
**app/views/admin/baggages/new.html.haml

.wrapper .disply = form_with model: @baggage do |f| .baggage .baggage__box %span 荷物の種類 = f.select :kind, [["なまもの", "なまもの"], ["チルド", "チルド"], ["冷凍", "冷凍"], ["その他", "その他"]], include_blank: "選択して下さい" .baggage__box %span 保管期限 = f.text_field :storage_period, class: "baggage_text" , placeholder: '例)7' %span 日 .baggage__box %span 追跡番号 = f.text_field :code, class: "baggage_text", placeholder: '123456789012' -# = f.hidden_field :user_id , :value => "#{user_session}" = f.hidden_field :user_id = f.submit 'SEND', class: "baggage__send"

全く同じ記述の管理者用baggagesコントローラーがあり

class Admin::BaggagesController < ApplicationController before_action :if_not_admin before_action :set_baggage, only: [:edit, :update, :show, :destroy] # before_action :move_to_index,only: [:show] def index if current_user.admin? # @baggages = Baggage.search(params[:search]) @search = User.ransack(params[:q]) #:qは入力したクエリのq @users = @search.result#検索した"結果"ユーザーのparams丸ごと # @baggages = Baggage.find(params[:user_id]) end end def new if current_user.admin? @baggage = Baggage.new(baggage_params) end end def create # @baggage = current_user.baggages.build(baggage_params) # @baggage = Baggage.new.(user_id: @user.id) # binding.pry @baggage = Baggage.new(baggage_params) if @baggage.save redirect_to pages_show_path(@baggage) else # render :new render file: "admin/baggages/new." end end def edit # if current_user.admin? # end end def update if @baggage.update(baggage_params) redirect_to pages_show_path(@baggage) else render :edit end end def show # binding.pry # @baggage = Baggage.find_by(id: params[:id]) # @user = User.find_by(id: @baggage.user_id) end def destroy end private def if_not_admin redirect_to root_path unless current_user.admin?#管理者ユーザー以外が特定のアクションを実行しようとした場合トップページにリダイレクトされる end def set_baggage @baggage = Baggage.find(params[:id])#edit, show, destroy などのアクションで使用する変数をセットします。 end def baggage_params params.permit( :kind,:storage_period, :code, :user_id )#.merge(user_id: current_user.id) end # def move_to_index # redirect_to action: :index unless user_signed_in? && current_user.id == @baggage.user_id # end end

と、修正したら以前のエラーは出なくなったものの、createはされず、下記のターミナルをみても、原因が掴めません

app/controllers/baggages_controller.rb:34:in `create' Started POST "/baggages" for ::1 at 2020-10-27 08:18:42 +0900 Processing by BaggagesController#create as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"r1DJfXsonsAr8fJOdEzLr7uyluXFz1uBTzK/4P++LNRUVBd8jgfp3ZNKbSlAe/I4ausFGKubcmtXmkuVhoHNfw==", "baggage"=>{"kind"=>"チルド", "storage_period"=>"7", "code"=>"111111111111", "user_id"=>"1"}, "commit"=>"SEND"} Unpermitted parameters: :utf8, :authenticity_token, :baggage, :commit (0.2ms) BEGIN ↳ app/controllers/baggages_controller.rb:29 (0.2ms) ROLLBACK ↳ app/controllers/baggages_controller.rb:29 Rendering baggages/new.html.haml within layouts/application Rendered baggages/new.html.haml within layouts/application (8.1ms) User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/views/layouts/application.html.haml:12 Completed 200 OK in 219ms (Views: 173.9ms | ActiveRecord: 21.2ms)

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

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

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

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

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

guest

回答2

0

自己解決

結論
class Admin::BaggagesControllerのストロングパラメーターとclass BaggagesControllerの記述を変える

class Admin::BaggagesController < ApplicationController before_action :if_not_admin before_action :set_baggage, only: [:edit, :update, :show, :destroy] # before_action :move_to_index,only: [:show] def index if current_user.admin? # @baggages = Baggage.search(params[:search]) @search = User.ransack(params[:q]) #:qは入力したクエリのq @users = @search.result#検索した"結果"ユーザーのparams丸ごと # @baggages = Baggage.find(params[:user_id]) end end def new if current_user.admin? @baggage = Baggage.new(baggage_params) end end def create # @baggage = current_user.baggages.build(baggage_params) # @baggage = Baggage.new.(user_id: @user.id) # binding.pry @baggage = Baggage.new(baggage_params) # binding.pry if @baggage.save redirect_to pages_show_path(@baggage) else # render :new render file: "pages/show" # render "new" # render "baggages/index" end end def edit # if current_user.admin? # end end def update if @baggage.update(baggage_params) redirect_to pages_show_path(@baggage) else render :edit end end def show # binding.pry # @baggage = Baggage.find_by(id: params[:id]) # @user = User.find_by(id: @baggage.user_id) end def destroy end private def if_not_admin redirect_to root_path unless current_user.admin?#管理者ユーザー以外が特定のアクションを実行しようとした場合トップページにリダイレクトされる end def set_baggage @baggage = Baggage.find(params[:id])#edit, show, destroy などのアクションで使用する変数をセットします。 end def baggage_params params.permit( :kind,:storage_period, :code, :user_id )#.merge(user_id: current_user.id) end # def move_to_index # redirect_to action: :index unless user_signed_in? && current_user.id == @baggage.user_id # end end
class BaggagesController < ApplicationController before_action :if_not_admin before_action :set_baggage, only: [:edit, :update, :show, :destroy] # before_action :move_to_index,only: [:show] def index if current_user.admin? # @baggages = Baggage.search(params[:search]) @search = User.ransack(params[:q]) #:qは入力したクエリのq @users = @search.result#検索した"結果"ユーザーのparams丸ごと # @baggages = Baggage.find(params[:user_id]) end end def new if current_user.admin? @baggage = Baggage.new(baggage_params) end end def create # @baggage = current_user.baggages.build(baggage_params) # @baggage = Baggage.new.(user_id: @user.id) # binding.pry @baggage = Baggage.new(baggage_params) # binding.pry if @baggage.save redirect_to pages_show_path(@baggage) else # render :new # render "new" render file: "pages/show" # render "baggages/index" end end def edit # if current_user.admin? # end end def update if @baggage.update(baggage_params) redirect_to pages_show_path(@baggage) else render :edit end end def show # binding.pry # @baggage = Baggage.find_by(id: params[:id]) # @user = User.find_by(id: @baggage.user_id) end def destroy end private def if_not_admin redirect_to root_path unless current_user.admin?#管理者ユーザー以外が特定のアクションを実行しようとした場合トップページにリダイレクトされる end def set_baggage @baggage = Baggage.find(params[:id])#edit, show, destroy などのアクションで使用する変数をセットします。 end def baggage_params params.require(:baggage).permit( :kind,:storage_period, :code, :user_id )#.merge(user_id: current_user.id) end # def move_to_index # redirect_to action: :index unless user_signed_in? && current_user.id == @baggage.user_id # end end

投稿2020/10/27 09:50

yukireonsousi

総合スコア3

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

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

0

app/views/baggages/new.html.* が無いと言ってます。
ありますか?

追記
app/views/admin/baggages/new.html.haml は
app/views/baggages/new.html ではないですね。

app/views/admin/baggages/new.html.haml で表示させたいなら
render :new がおかしいです。
render file: "admin/baggages/new." でしょうか。
しかし、controllerが BaggagesController で Admin::BaggagesController ではないですからちょっとおかしいです。

で、???なのは def new からの 画面はエラー無かったのですね?

投稿2020/10/26 08:07

編集2020/10/26 11:15
winterboum

総合スコア23360

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

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

yukireonsousi

2020/10/26 10:21

app/views/admin/baggages/new.html.hamlのviewがあります ファイルの中身は以前の状態から、いじって無いです 念のため、ファイルを追記しておきます
yukireonsousi

2020/10/26 23:44

質問文を追記しました。 荷物の情報を管理者のみが行える実装を行っており BaggagesControllerとAdmin::BaggagesControllerが二つあり、中身の内容は同じです。 ファイルが二つ無いとなぜか、エラーが出ます。 Admin::BaggagesControllerの方の中身を先ほどご指摘いただいた通りに修正しましたら、エラーは消えたものの、createできない状態です。 ターミナルの解読知識が足りず難航しております。
yukireonsousi

2020/10/27 09:26

ストロングパラメーターのbaggage_paramsメソッドに require(:baggage)を加えるとnewページからcreateはできますが、 indexページから、newページに行こうとするには無い状態でないと param is missing or the value is emptyのエラーがおきるループ状態です
yukireonsousi

2020/10/28 01:24

winterboumさんありがとうございました。 renderの遷移先の指定は、場合によっては、明確に指定しなきゃいけないんですね
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問