Ruby on Rails初心者です。
editページの登録ボタンクリックでupdateアクションを呼びたいのですが、
No route matchesのエラーが出ます。
単純なミスだと思うのですが、どうしても分からずにいます。。。
分かる方よろしくお願いします。
#routes.rb
ruby
1 get '/emp_groups', to: 'emp_groups#index' 2 get '/emp_groups/edit', to: 'emp_groups#edit' 3 patch '/emp_groups', to: 'emp_groups#update'
#edit.html.erb
ruby
1 2 <% if @employee.present? %> 3 <%= form_for @employee, url: emp_groups_edit_path(employee_id: @employee.id) do |f| %> 4 <% if @has_error %> 5 <p class="alert alert-danger">入力内容に不備があります</p> 6 <% end %> 7 <div class="table-responsive"> 8 <table id="empgroup_input" class="table"> 9 <thead> 10 <tr> 11 <th>部</th> 12 <th>課</th> 13 <th>グループ</th> 14 <th>開始年月日</th> 15 <th>行削除</th> 16 </tr> 17 </thead> 18 <tbody> 19 <%= f.fields_for :emp_groups do |eg_f| %> 20 <tr> 21 <td> 22 <%= eg_f.collection_select :department_id, Department.all, :id, :dep_name, {include_blank: true }, { class: 'department_select' } %> 23 </td> 24 <td> 25 <%= eg_f.collection_select :section_id, Section.where(department_id: eg_f.object.department_id).all, :id, :sec_name, {include_blank: true }, { class: 'section_select' } %> 26 </td> 27 <td> 28 <%= eg_f.collection_select :group_id, Group.where(section_id: eg_f.object.section_id).all, :id, :grp_name, {include_blank: true }, { class: 'group_select' } %> 29 </td> 30 <td> 31 <%= eg_f.text_field :start_date, class: 'date-picker' %> 32 </td> 33 <td> 34 <button class="btn btn-danger empgroup_destroy">削除</button> 35 <%= eg_f.hidden_field :_destroy %> 36 </td> 37 </tr> 38 <% end %> 39 </tbody> 40 </table> 41 <button id="empgroup_add_row" class="btn btn-primary">行追加</button> 42 </div> 43 <p><%= f.submit "更新", class: "btn btn-primary" %></p> 44 <% end %> 45 <% end %> 46 </div> 47 </div> 48 </div>
#Controller
ruby
1 2class EmpGroupsController < ApplicationController 3 4 5 def edit 6 set_employee 7 @employee.emp_groups.build 8 end 9 10 def update 11 12 if @employee.update(employee_params) 13 redirect_to emp_groups_edit_path(employee_id: @employee.id) 14 else 15 @employee.emp_groups.build 16 @has_error = true 17 render :edit 18 end 19 end 20 21 private 22 23 def set_employee 24 @employee = Employee.find(params[:id]) 25 end 26 27 def employee_params 28 params.require(:employee).permit(emp_groups_attributes: [:id, :department_id, :section_id, :group_id, :start_date, :_destroy]) 29 end 30end 31 32 33 34
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/02 07:14
2018/01/02 08:50