创建操作的路由

时间:2014-08-05 18:44:20

标签: ruby-on-rails routes

我有我的user_controller和动作创建。但是,我确实要为创建操作设置自定义路由,因此当用户点击register时,创建操作会将网址设置为user/thank-you。我已将以下match添加到我的routes文件中,但在按下注册按钮后,网址保持不变。这是我的路线文件的一部分,直到谢谢你的行。

get '/:locale' => 'pages#home'
root :to => 'pages#home'


scope "(:locale)", locale: /en|es/ do

get "javascripts/dynamic_cities"
get "javascripts/dynamic_cities_to_stadia"
get "javascripts/dynamic_stadia"
get "javascripts/dynamic_modality_to_max_players"

get "tournaments/edit_info"
get "tournaments/seasons"
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"

resources :admin_tasks, :only => [:index]
resources :sports
resources :countries
resources :cities
resources :stadia
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :teams
resources :tournaments
resources :leagues
resources :brackets, :only => [:new, :create, :edit, :update, :show]
resources :labs, :only => [:show, :edit]
resources :seasons, :only => [:edit, :show, :destroy]
resources :matches, :only => [:show, :edit, :update]
resources :player_participations
resources :highlights

resources :users do
  resources :requests, :name_prefix => "user_"
end

# match 'league/:id/subscription' => 'league_rosters#new', :as => :subscription
match '/contact',  :to => 'pages#contact'
match '/terms_and_conditions', :to => 'pages#terms_and_conditions'
match '/about_us', :to => 'pages#about_us'
match '/cookie_excluder', :to => 'pages#cookie_excluder'
match '/vacancies', :to => 'pages#vacancies'
match '/signup',   :to => 'users#new'
match '/thank-you',   :to => 'users#create' 

以下是users_controller.rb

中的创建方法
def create
# Note: This function is repeated in request controller in the invitations part. So any change should be added to request controller aswell
@user = User.new(email: params[:user][:email].downcase,
                 name: params[:user][:name].capitalize,
                 password: params[:user][:password],
                 password_confirmation: params[:user][:password_confirmation],
                 language: params[:user][:language])
#for user_menu
@title = t("user.new.title")

if @user.save
  confirmation_code = "#{@user.id}#{random_string}"
  if @user.update_attributes(confirmation_code: confirmation_code)
    UserMailer.welcome_email(@user).deliver
    vars = Hash.new
    vars[:cc] = "#{confirmation_code}"
    confirmation_url = url_maker(params[:request_protocol], params[:request_host_with_port], params[:request_locale], "#{email_confirmation_path}", vars)
    UserMailer.confirm_email(@user, confirmation_url).deliver
    sign_in @user
    @user_tournaments = current_user.tournaments.all(:order => "name ASC")
    @user_teams = current_user.teams.all(:order => "name ASC")
  else
    render 'new'
  end
else
  render 'new'
end
end

这是否有可能以我接近它的方式工作,或者我必须进行重定向和thank-you页面,我可以通过我的页面控制器轻松管理它?

1 个答案:

答案 0 :(得分:1)

将您的thank-you路线移到资源区块上方,如下所示:

...
get "league_rosters/cancel_new"
get "tournament_administrations/cancel_new"

# Move route here
match '/thank-you',   :to => 'users#create', via: [:post]

resources :admin_tasks, :only => [:index]
resources :sports
...

我还在路由中添加via: [:post]

  

将GET和POST请求路由到单个操作具有安全隐患。通常,除非有充分的理由,否则应避免将所有动词都路由到动作。

[http://guides.rubyonrails.org/routing.html]