2016-02-29 36 views
1

Rails使用句點而不是問號生成帳號激活URL。我在郵件程序預覽和rails日誌中看到了這種情況。例如鏈路:從routes.rb中爲什麼Rails會在URL的查詢字符串之前加上句點(點),而不是問號?

http://localhost:3000/account_activations/Cm4OyFOwosBcGZ67qg49nQ/[email protected]

resources :account_activations, only: [:edit] 

從users_controller.rb:

def create 
    @user = User.new(user_params) 
    if @user.save 
     UserMailer.account_activation(@user).deliver_now 
     flash[:info] = "Please check your email to activate your account." 
     redirect_to root_url 
    else 
     render 'new' 
    end 
    end 

從account_activation.html.erb:

<%= link_to "Activate", edit_account_activation_url(@user.activation_token, @user.email) %> 

從user.rb(方法來創建並分配摘要):

def create_activation_digest 
    self.activation_token = User.new_token 
    self.activation_digest = User.digest(activation_token) 
end 

從user_mailer_preview.rb:

def account_activation 
    user = User.first 
    user.activation_token = User.new_token 
    UserMailer.account_activation(user) 
    end 
+0

[email protected]看起來像一個電子郵件地址。 –

回答

4

url_route只需要一個PARAM:id。你想要做的是什麼:

edit_account_activation_url(@user.activation_token, email: @user.email)

這會給你params[:id]和​​在你的控制器使用。

2

其原因是,每一個資源豐富url_helper實際預計n或n + 1個參數(其中n是一個數在路由命名則params的),與最後一個參數是所述路徑的格式:

user_path(@user, :json) #=> /users/1.json 

(實際上,簽名只是url_helper(*args),錯誤的元數異常是從助手的內部拋出)

如果你想添加額外的GET參數,你需要傳遞一個額外的哈希值由nzfinab已經指出:

user_path(@user, hello: :there) #=> /users/1?hello=there 
相關問題