2014-05-16 46 views
0

我有一個用於我的操作的新表單和更新。form_for重定向以顯示而不是創建

所有更新都可以,但對於我的新版本,他試圖重定向我以顯示而不是創建。

我有這樣的錯誤:

No route matches {:action=>"show", :controller=>"club/team", :club_id=>"1"} missing required keys: [:id] 

這裏我的表格:

<%= form_for @team, :html => { :class => 'sky-form', :multipart => true }, url: club_team_path do |f| %> 

... 

<% end %> 

在這裏,我的路線:

club_team_index POST  /clubs/:club_id/team(.:format)    club/team#create 
new_club_team GET  /clubs/:club_id/team/new(.:format)   club/team#new 
edit_club_team GET  /clubs/:club_id/team/:id/edit(.:format)  club/team#edit 
club_team  GET  /clubs/:club_id/team/:id(.:format)   club/team#show 
       PATCH /clubs/:club_id/team/:id(.:format)   club/team#update 
       PUT  /clubs/:club_id/team/:id(.:format)   club/team#update 
       DELETE /clubs/:club_id/team/:id(.:format)   club/team#destroy 

我的控制器代碼:

class Club::TeamController < ApplicationController 

    def index 
    @teams = Team.find_all_by_club_id(params[:club_id]) 
    end 

    def new 
    @categories = TeamCategory.all 
    @levels = TeamLevel.all 
    @club = Club.find(params[:club_id]) 
    end 

    def create 
    if !user_signed_in? 
     flash[:errors] = [] 
     flash[:errors] << {:message => 'Vous devez vous connecter pour créer une Equipe', :strong => 'Accès Refusé :'} 
     redirect_to new_club_path 
    else 
     @team = Team.new(team_params) 
     @team.club_id = params[:club_id] 

     if @team.save 
     redirect_to club_team_path(@team.club_id, @team.id) 
     else 
     flash[:errors] = [] 
     flash[:errors] << {:message => 'Des champs sont mal remplis', :strong => "Erreur :"} 

     redirect_to new_club_team_path 
     end 
    end 
    end 

    def show 
    @team = Team.find_by_id(params[:id]) 
    end 

    def edit 
    @team = Team.find(params[:id]) 
    @categories = TeamCategory.all 
    @levels = TeamLevel.all 
    @club = Club.find(params[:club_id]) 
    end 

    def update 
    @team = Team.find(params[:id]) 

    if @team.update(team_params) 
     flash[:success] = [] 
     flash[:success] << {:message => 'Equipe mise à jour avec succès.', :strong => 'Edition :'} 
     redirect_to club_team_path(@team.club_id, @team.id) 
    else 
     flash[:errors] = [] 
     flash[:errors] << {:message => 'Certain champs ne remplissent pas les conditions requises.', :strong => 'Erreur :'} 
     render 'edit' 
    end 
    end 

    private 
    def team_params 
     params.require(:team).permit(:name, :category_id, :level_id, :avatar) 
    end 
end 
+0

刪除URL部分,並使用[@club,@team]在的form_for – phoet

回答

1

的問題是在這裏在這一行

<%= form_for @team, :html => { :class => 'sky-form', :multipart => true }, url: club_team_path do |f| %> 

當你寫url:club_team_path,按照路由,它的表演動作相匹配。

改變它喜歡這個

<%= form_for [@club,@team], :html => { :class => 'sky-form', :multipart => true } do |f| %> 

有關詳細信息,請參閱本API

+0

我有這樣的錯誤:在表格第參數不能包含零或爲空。我設置了我的俱樂部:@club = Club.find(params [:club_id]) –

+0

@ user2847607發佈您的控制器代碼。 – Pavan

+0

我討厭我的控制器波紋管 –

0

OK了probleme在我的路線文件。

# Clubs routes 
    scope module: 'club' do 
    resources :clubs do 
     resources :team, :only => [:show, :new, :create, :update, :destroy, :edit] 
     get '/club_accounting', to: 'club_accounting#index', as: 'club_accounting_index' 
     post '/club_accounting', to: 'club_accounting#create', as: 'club_accounting_create' 
     delete '/club_accounting/:id', to: 'club_accounting#delete', as: 'club_accounting_delete' 
    end 
    end 

我改名:團隊:團隊和現在一切都OK!

由於

相關問題