create
その部分的なフォームのURLを設定して、update
アクションに使用できるようにするにはどうすればよいですか。
今、私は2つのURLを設定する必要があります:アクションadmin_faculty_path
用update
とアクションadmin_faculties_path
用create
です。
# admin/faculties/_form.html.haml
= simple_form_for @faculty, html: { multipart: true, class: 'form-horizontal' } do |f|
= f.input :department_id, as: :hidden, input_html: { value: params[:department_id] } if params[:department_id]
= f.input :name
= f.input :name_en
= f.submit
ルート:
admin_faculties POST /admin/faculties(.:format) admin/faculties#create
new_admin_faculty GET /admin/faculties/new(.:format) admin/faculties#new
edit_admin_faculty GET /admin/faculties/:id/edit(.:format) admin/faculties#edit
admin_faculty GET /admin/faculties/:id(.:format) admin/faculties#show
PUT /admin/faculties/:id(.:format) admin/faculties#update
コントローラ:
# faculties_controller.rb
def new
@faculty = Faculty.new
end
def create
@faculty = Faculty.new(params[:faculty])
if @faculty.save
redirect_to admin_departments_path,
notice: t('activerecord.successful_save_data')
else
flash[:error] = t('activerecord.save_data_error_html')
render 'new'
end
end
def edit
@faculty = Faculty.find(params[:id])
end
def update
@faculty = Faculty.find(params[:id])
if @faculty.update_attributes(params[:faculty])
redirect_to admin_departments_path,
notice: t('activerecord.successful_save_data')
else
flash[:error] = t('activerecord.save_data_error_html')
render 'edit'
end
end