2017-10-02 6 views
0

浅いネストされたルートを使用したのはこれが初めてで、編集アクションを実行するのに苦労しています(さらに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

このエラーが発生した場合、 – krishnar

+0

編集しようとしています – olliekav

+0

エラーを受けとっている場所を指定していません。エラーメッセージ全体を追加します。 –

答えて

0

ので、私は別の形でフォームをラップしてしまいましたform_for私は編集のためのURLオプションを使用することができました。これは現在動作しています。

<%= form_for(@group_action, url: action_path) do |f| %> 
    <%= render 'form', f: f %> 
<% end %> 
関連する問題