form_for重定向显示而不是创建

时间:2014-05-16 20:43:04

标签: ruby-on-rails-4

我有一个表格,我的行动是新的和更新。

一切都可以更新,但对于我的新手,他试图将我重定向到show而不是create。

我有这个错误:

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

2 个答案:

答案 0 :(得分:1)

问题出在这一行

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

根据路线编写url:club_team_path时,它与show动作匹配。

将其更改为此

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

有关详情,请参阅 API

答案 1 :(得分:0)

问题出在我的路线文件中。

我有

 # 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

我改名为:团队到:团队,一切都好了!

由于