データベースに保管する場合の記述をしているのですが、
リンク内容の様なエラーがでます
admin/baggages/controller.rb
class Admin::BaggagesController < ApplicationController before_action :if_not_admin before_action :set_baggage, only: [:show, :edit, :destroy] def index if current_user.admin? # @baggages = Baggage.search(params[:search]) @search = User.ransack(params[:q]) #:qは入力したクエリのq @users = @search.result end end def new # binding.pry if current_user.admin? @baggage = Baggage.new(user_id: @users) 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 end def destroy end def show 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: user_session) end end
admin/baggages/index.html.haml
= search_form_for @search, url: admin_baggages_url do |f| = f.label :name_cont, "名前を入力してください" = f.search_field :name_cont = f.submit "検索" .table .thead %tr %th= sort_link(@search, :name)#resultさせてる部分とリンク %th= sort_link(@search, :address) .tbody - @users.each do |user|#controllerの「@users = @search.result」検索したものを"返す" %tr -# %td= link_to user.name, new_admin_baggage_url(@q, :'q[user_id_eq]' => "#{user.id}") %td= user.name %td= user.address %td= link_to "New", new_admin_baggage_url(user_id: user.id)
admin/baggages/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"
エラー文
NoMethodError in Admin::Baggages#new Showing /Users/arimotoyuuki/projects/fusion/app/views/admin/baggages/new.html.haml where line #4 raised: undefined method `baggages_path' for #<#<Class:0x00007fb0d88e2f48>:0x00007fb0ef5a1390> Did you mean? baggage_path Extracted source (around line #4): 2 3 4 5 6 7 .disply = form_with model: @baggage do |f| .baggage .baggage__box Rails.root: /Users/arimotoyuuki/projects/fusion Application Trace | Framework Trace | Full Trace app/views/admin/baggages/new.html.haml:4:in `_app_views_admin_baggages_new_html_haml___1924533726690858305_70198953314620' Request Parameters: {"user_id"=>"1"} Toggle session dump Toggle env dump Response Headers: None x >> params => <ActionController::Parameters {"user_id"=>"1", "controller"=>"admin/baggages", "action"=>"new"} permitted: false> >>
routes.rb
Rails.application.routes.draw do root 'pages#index' get 'pages/show' devise_for :users resources :baggages, only: [:new,:create, :show] namespace :admin do resources :baggages do collection do get 'search' end end end end
get 'search'
は使ってないから、削除した方がいいかもしれないですね