我有一個用於我的操作的新表單和更新。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
刪除URL部分,並使用[@club,@team]在的form_for – phoet