<やりたいこと>
アコーディオンの編集フォームで誤ったユーザー情報で更新した際にエラーメッセージを表示させたい
<困っていること>
正しい情報での更新はできるのですが誤った情報だとエラーメッセージは表示されずエラーになる
<質問>
フォームの書き方が悪いのかコントローラが悪いのかわからないのですが参考サイトやアドバイスいただけないでしょうか?
users_contoller.rb
def index if params[:q] && params[:q].reject { |key, value| value.blank? }.present? @q = User.ransack(search_params, activated_true: true) @title = "検索結果" else @q = User.ransack(activated_true: true) @title = "ユーザー一覧" end @users = @q.result.paginate(page: params[:page]) end 〜 def update_basic_info @user = User.find(params[:id]) if @user.update_attributes(user_params) flash[:success] = "基本情報を更新しました。" redirect_to users_path else @users = User.paginate(page: params[:page]) flash[:danger] = "#{@user.name}さんの基本情報の更新に失敗しました。" render 'index' end end
index.html.erb
<% provide(:title, @title) %> <% provide(:button_text, '更新') %> <h1><%= @title %></h1> <div class="row"> <div class="search_form"> <%= render 'users/search_form' %> </div> </div> <%= will_paginate %> <% unless @users.empty? %> <ul class="users"> <li> <% @users.each do |user| %> <%= user.name %> <% if current_user.admin? %><br> <% if !current_user?(user) %> <%= link_to "削除", user, method: :delete, data: { confirm: "削除してよろしいですか?" },class: "btn btn-primary" %><br> <% end %> <div class="userslist"> <a class="btn btn-primary" data-toggle="collapse" href="#collapse<%= user.id %>">編集</a> <div class="collapse" id="collapse<%= user.id %>"> <div class="panel-body"> <%= render "user",user: user %> </div> </div> </div> <% end %> <% end %> </li> </ul> <% else %> <p class="search"> ユーザーが見つかりませんでした。<br> 探しているユーザー名を確認してください。 </p> <% end %> <%= will_paginate %>
_user.html.erb
<%= form_with model: user, url: update_basic_info_path(params: {id: user.id}), local: true, data: {remote: 'true'} do |f| %> <% if user.errors.any? %> <div class="alert alert-danger"> <ul> <% user.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <%= f.label :name %> <%= f.text_field :name, class: 'form-control' %> <%= f.label :email %> <%= f.email_field :email, class: 'form-control' %> <%= f.label :department %> <%= f.text_field :department, class: 'form-control' %> <%= f.label :uid %> <%= f.text_field :uid, class: 'form-control' %> <%= f.label :employee_number %> <%= f.text_field :employee_number, class: 'form-control' %> <%= f.label :basic_time %> <%= f.time_field :basic_time, class: 'form-control' %> <%= f.label :work_time %> <%= f.time_field :work_time, class: 'form-control' %> <%= f.label :work_end_time %> <%= f.time_field :work_end_time, class: 'form-control' %> <%= f.label :password %> <%= f.password_field :password, class: 'form-control' %> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation, class: 'form-control' %> <%= f.submit yield(:button_text), class: "btn btn-primary" %> <% end %>
routes.rb
Rails.application.routes.draw do root 'static_pages#home' get '/signup', to: 'users#new' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' # get '/edit-basic-info/:id', to: 'users#edit_basic_info', as: :basic_info #patch 'update-basic-info', to: 'users#update_basic_info' get 'users/:id/attendances/:date/edit', to: 'attendances#edit', as: :edit_attendances patch 'users/:id/attendances/:date/update', to: 'attendances#update', as: :update_attendances resources :users do member do patch 'update_basic_info' end resources :attendances, only: :create end end
schema.rb
create_table "users", force: :cascade do |t| t.string "name" t.string "email" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "password_digest" t.boolean "admin", default: false t.string "department" t.datetime "basic_time", default: "2019-02-19 22:30:00" t.datetime "work_time", default: "2019-02-19 23:00:00" t.string "remember_digest" t.datetime "work_end_time", default: "2019-02-20 08:00:00" t.string "employee_number" t.string "uid" t.boolean "superior", default: false t.index ["email"], name: "index_users_on_email", unique: true end
回答3件
あなたの回答
tips
プレビュー