以前は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)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。