ruby on rails6
form_with使ってデータを作成したいのですが、以下のようなエラーが発生します。
Routing Error
No route matches [POST] "/admin_users/new"
上記のエラー通りにnewにポストを付与してあげた。
post 'admin_users/new' => 'admin_users#new'
を追加したのですが、createされてないようなので何が原因なのでしょうか。
view
1<%= form_with(model: @admin_user, local: true) do |form| %> 2 <%= render 'shared/error_messages', model: form.object %> 3 <fieldset class="inputs"> 4 <ol> 5 <li class="string input optional stringish" id="item_name_input"> 6 <%= form.label :email %> 7 <%= form.email_field :email,class: 'form_field', autofocus: true, autocomplete: "email" %> 8 </li> 9 <li class="string input optional stringish" id="item_price_input"> 10 <%= form.label :password %> 11 <%= form.password_field :password,class: 'form_field', autocomplete: "current-password" %> 12 </li> 13 <li class="number input optional numeric stringish" id="item_stock_input"> 14 <%= form.label :membername %> 15 <%= form.text_field :membername %> 16 </li> 17 </ol> 18 </fieldset> 19 <fieldset class="actions"> 20 <ol> 21 <li class="action input_action " id="item_submit_action"> 22 <%= form.submit "メンバーを追加" %> 23 </li> 24 <li class="cancel"> 25 <%= link_to '取り消す', admin_users_path %> 26 </li> 27 </ol> 28 </fieldset> 29 <% end %>
controller
1 # GET /admin_users/new 2 def new 3 @admin_user = AdminUser.new 4 end 5 6 # GET /admin_users/1/edit 7 def edit 8 end 9 10 # POST /admin_users 11 # POST /admin_users.json 12 def create 13 @admin_user = AdminUser.new(admin_user_params) 14 15 respond_to do |format| 16 if @admin_user.save 17 format.html { redirect_to admin_users_path, notice: 'Admin user was successfully created.' } 18 format.json { render :show, status: :created, location: @admin_user } 19 else 20 format.html { render new_admin_user_path } 21 format.json { render json: @admin_user.errors, status: :unprocessable_entity } 22 end 23 end 24 end 25 26 # PATCH/PUT /admin_users/1 27 # PATCH/PUT /admin_users/1.json 28 def update 29 respond_to do |format| 30 if @admin_user.update(admin_user_params) 31 format.html { redirect_to @admin_user, notice: 'Admin user was successfully updated.' } 32 format.json { render :show, status: :ok, location: @admin_user } 33 else 34 format.html { render :edit } 35 format.json { render json: @admin_user.errors, status: :unprocessable_entity } 36 end 37 end 38 end 39 40 private 41 # Use callbacks to share common setup or constraints between actions. 42 43 # Only allow a list of trusted parameters through. 44 def admin_user_params 45 params.require(:admin_user).permit(:membername, :password, :email) 46 end 47
route
1Rails.application.routes.draw do 2 resources :customers, only: [:new, :create, :show, :index, :update, :edit, :destroy] do 3 resources :exchange_histories 4 resources :point_histories 5 end 6 7 root to: 'admin_users#index' 8 9 devise_for :admin_users 10 resources :admin_users 11 post 'admin_users/new' => 'admin_users#new' 12end 13
あなたの回答
tips
プレビュー