2012-10-15 27 views
0

我遇到了一個問題,我懷疑這個問題不難解決,而且我的經驗缺乏使問題變得困難。在URL中傳遞參數,然後在未通過驗證的情況下使用'new'操作呈現錯誤

我有我的路線

match '(/:client_id)/signup', to: 'users#new' 

現在,如果客戶端發送鏈接誰www.myappurl.com/123/signup用戶將註冊和他的用戶模型將收到的client_id作爲參數(這是我需要在後面上)。

控制器:

def new 
    @user = User.new 
    if signed_in? 
    redirect_to root_path 
    end 
end 

def create 
    @user = User.new(params[:user]) 

    if @user.save 
    sign_in @user 
    flash[:success] = t("users.new.welcome") 
    redirect_to @user 
    else 
    render 'new' #and this is the problem 
    end 
end 

而問題是,當用戶提交自己的應用程序,他提供的信息沒有通過驗證,然後「新」的動作渲染,但我失去的:CLIENT_ID參數因此他和客戶之間的關聯。

我已經嘗試了redirect_to並傳遞參數。這可以工作,但不會顯示錯誤消息。

任何想法如何解決它?

編輯

提交無效信息之前我調試:

controller: users 
action: new 
locale: en 
client_id: '2' 

後:

!binary "dXRmOA==": ✓ 
!binary "YXV0aGVudGljaXR5X3Rva2Vu": ldtdjuWAWHB052qY3ASoH1Mv3XYtrdRcL56DgSaYC0Q= 
!binary "dXNlcg==": !ruby/hash:ActiveSupport::HashWithIndifferentAccess 
!binary "Zmlyc3RfbmFtZQ==": '' 
!binary "bGFzdF9uYW1l": '' 
!binary "ZW1haWw=": '' 
!binary "Y2xpZW50X2lk": '2' 
!binary "cGFzc3dvcmQ=": '' 
!binary "cGFzc3dvcmRfY29uZmlybWF0aW9u": '' 
!binary "Y29tbWl0": Create my account 
action: create 
controller: users 
locale: en 

服務器記錄

Started POST "/en/users" for 127.0.0.1 at 2012-10-16 07:10:31 +0800 
Processing by UsersController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ldtdjuWAWHB052qY3ASoXYtrdRcL56DgSaYC0Q=", "user"=>{"first_name"=>"", "last_name"=>"", "email"=>"", "client_id"=>"2", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account", "locale"=>"en"} 
(0.4ms) BEGIN 
User Exists (2.5ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('') LIMIT 1 
(0.1ms) ROLLBACK 
Rendered shared/_error_messages.html.erb (18.6ms) 
Rendered users/_fields_for_registration_no_verification.html.erb (36.9ms) 
Rendered users/_password_fields.html.erb (3.6ms) 
Rendered users/new.html.erb within layouts/application (77.5ms) 
Rendered layouts/_shim.html.erb (0.0ms) 
Rendered layouts/_google_tracking_code (0.0ms) 
User Load (2.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" IS NULL LIMIT 1 
Rendered layouts/_header.html.erb (8.7ms) 
Rendered layouts/_footer.html.erb (1.5ms) 
Completed 200 OK in 437ms (Views: 213.4ms | ActiveRecord: 14.9ms) 

編輯

我發現問題的真正根源(與下面的答案不同)。問題是,當我用我的形式隱藏字段我用下面的代碼:

<%= f.hidden_field :client_id, { :value => params[:client_id]} %> 

原來,有一次我刪除了{ :value => params[:client_id]}部分一切都開始註冊不成功後按預期運行。永遠不要感謝你的幫助。

+0

和我在的form_for通過CLIENT_ID在隱藏字段,如果有人在問 –

回答

2

這是因爲驗證失敗並呈現新內容時,:cliend_id將從@user對象中丟失。

def new 
    if params[:client_id] 
    @user = User.new(:client_id => params[:client_id]) 
    else 
    @user = User.new 
    end 
    if signed_in? 
    redirect_to root_path 
    end 
end 

而在你users/new.html.haml視圖請確保您有隱藏字段爲:client_id

=form_for @user do |u| 
    =f.hidden_field :client_id #This will prevent your client_id from getting lost when validation fails. 

更清潔的方法是將before_filter用於新動作。希望你明白這個主意。

+0

感謝您的答覆,不幸CLIENT_ID仍然丟失,如果我嘗試您的解決方案 –

+0

我已經更新了問題與一些附加信息 –

+0

可以粘貼您的服務器日誌當新的命令第一次用':client_id'命中時? – Deepak

相關問題