現在、カテゴリー新規作成機能を実装しています。
form_withを使用してデータを受け取りたいです。
new.html.erb
<%= form_with modle: @category, url: categories_path do |f| %> <%= f.label :category_name, "カテゴリー名", class: "control-label" %> <span class="badge badge-danger">必須</span> <%= f.text_field :category_name, class: "form-control" %> <%= f.submit '登録', class: "btn btn-primary assignMenber" %> <% end %>
routes.rb
Rails.application.routes.draw do get 'tasks/new' => 'tasks#new' devise_for :users root 'homes#index' resources :users resources :tasks, only: [:index] resources :categories, only: [:index, :new, :create, :destroy, :edit, :search] do collection do get 'search' end end end
categories_controller.rb
class CategoriesController < ApplicationController def index @category = Category.all # binding.pry end def new @category = Category.new end def create binding.pry @category = Category.new(category_params) @category.save redirect_to categories_path end def destroy @category = Category.find(params[:id]) @category.destroy redirect_to categories_path end def edit @category = Category.find(params[:id]) binding.pry reirect_to new_category_path end def search @category = Category.find_by('category_name LIKE(?)', "%#{params[:keyword]}%") render json: @category end private def category_params params.require(:categories).permit(:category_name) end end
db
create_table "categories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "category_name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "tasks", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "task_users_id" t.string "task_name" t.string "status" t.string "message" t.datetime "end_date" t.string "temp_memo" t.datetime "start_date" t.string "priority" t.string "cont_memo" t.string "amcestry" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "category" end
rails routes
Prefix Verb URI Pattern Controller#Action tasks_new GET /tasks/new(.:format) tasks#new 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 root GET / homes#index users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy tasks GET /tasks(.:format) tasks#index categories POST /categories(.:format) categories#create search_categories GET /categories/search(.:format) categories#search GET /categories(.:format) categories#index POST /categories(.:format) categories#create new_category GET /categories/new(.:format) categories#new edit_category GET /categories/:id/edit(.:format) categories#edit category GET /categories/:id(.:format) categories#show PATCH /categories/:id(.:format) categories#update PUT /categories/:id(.:format) categories#update DELETE /categories/:id(.:format) categories#destroy 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
どこが悪いのか分からず、悩んでいます。
どなたか原因がわかる方がいらっしゃればご指摘・アドバイスをよろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー