这是我第一次使用浅层嵌套路由,我正在努力让我的编辑操作起作用(我也在学习更多Rails的东西)。
因此,一个组可以分配多个组操作,因此我的路由设置为
resources :groups do
get 'actions', to: 'group_actions#index', as: :actions
get 'actions/new', to: 'group_actions#new', as: :new_action
post 'actions', to: 'group_actions#create'
resources :group_actions, path: '', as: :actions, except: [:index, :new, :create], shallow: true, shallow_path: 'actions/:group_id'
end
空路径的原因是我们可以通过在其之前附加group_id轻松管理冲突的组操作名称,因此操作URL将是......
/actions/17/action-name
。这当然会导致嵌套路由中的索引/新操作出现问题,因此我手动设置这些路由如上所述。这一切似乎都很好,可以创建,删除,查看等操作,但我无法编辑它们。
这给了我以下路线......
group_actions GET /groups/:group_id/actions(.:format) group_actions#index
group_new_action GET /groups/:group_id/actions/new(.:format)
group_actions#new
POST /groups/:group_id/actions(.:format) group_actions#create
edit_action GET /actions/:group_id/:id/edit(.:format) group_actions#edit
action GET /actions/:group_id/:id(.:format) group_actions#show
PATCH /actions/:group_id/:id(.:format)group_actions#update
PUT /actions/:group_id/:id(.:format) group_actions#update
DELETE /actions/:group_id/:id(.:format)
我的GroupAction控制器就是这样......
class GroupActionsController < ApplicationController
before_action :authenticate_user!, :except => [:show, :index]
before_action :set_group, only: [:index, :show, :new, :create, :edit, :update, :destroy]
def index
@group_actions = GroupAction.all
end
def show
@group_action = GroupAction.find(params[:id])
end
def new
@group_action = @group.group_actions.build
end
def edit
@group_action = GroupAction.find(params[:id])
end
def create
@group_action = GroupAction.new(group_action_params)
respond_to do |format|
if @group_action.save
format.html { redirect_to group_actions_path(@group.slug), notice: 'Group Action successfully created.' }
format.json { render :show, status: :created, group: @group }
else
format.html { render :new }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @group_action.update(group_params)
format.html { redirect_to @group_action, notice: 'Action was successfully updated.' }
format.json { render :show, status: :ok, location: @group_action }
GaTrack.event(:group, :updated, "#{@group_action.name}")
else
format.html { render :edit }
format.json { render json: @group_action.errors, status: :unprocessable_entity }
end
end
end
def destroy
@group_action = GroupAction.find(params[:id])
@group_action.destroy
respond_to do |format|
format.html { redirect_to group_actions_path(@group.slug), notice: 'Action was successfully deleted.' }
format.json { head :no_content }
end
end
private
def set_group
@group = Group.find(params[:group_id])
end
def group_action_params
params.require(:group_action).permit(:name, :description, :location, :start_date, :end_date, :how_to_participate, :intro_email, :update_email, :thank_you_email, :action_link, :is_public, :group_id)
end
end
我正在尝试分享我的新/编辑表单,这是我不理解这个问题的地方。
new.html.erb
<h1>New Group Action</h1>
<%= render 'form', group_action: @group_action %>
edit.html.erb
<h1>New Group Action</h1>
<%= render 'form', group_action: @group_action %>
尝试编辑动作时,我收到以下错误...
undefined method `group_action_path' for #<#
<Class:0x007f855e30b2c8>:0x007f855e3163a8>
Did you mean? group_actions_path
group_locations_path
group_new_action_path
group_actions_url
答案 0 :(得分:0)
所以我最终将表单包装在单独的form_for
中,因此我可以使用url选项进行编辑,这在目前是有效的。
<%= form_for(@group_action, url: action_path) do |f| %>
<%= render 'form', f: f %>
<% end %>