2012-03-01 33 views
0

我想借助button_to方法創建一個按鈕,以導航到可創建新用戶友誼的頁面。爲HTTP GET請求創建按鈕:保持區域設置的問題

<%= button_to t(:add_friend), new_user_friendship_url(current_user), :method => :get, :class => "btn-success" %> 

(我不想用一個鏈接,即不使用的link_to,因爲CSS框架我用的按鈕樣式比鏈接看起來要好得多)。

不幸的是,button_to不會將正確的語言環境附加爲URL參數。有什麼我可以做的嗎?

這是我的application_controller.rb其中我爲每個頁面請求設置語言環境。

class ApplicationController < ActionController::Base 
    before_filter :set_locale 

    def set_locale 
    I18n.locale = params[:locale] || I18n.default_locale 
    end 

    protect_from_forgery 

    def default_url_options(options={}) 
    logger.debug "default_url_options is passed options: #{options.inspect}\n" 
    { :locale => I18n.locale } 
    end 
end 

感謝您的建議。

回答

0

首先,應該很容易按照鏈接樣式相同的方式設置樣式按鈕。

其次,我認爲問題是在你的代碼中的其他地方。如果我把這個在我的應用程序:

= button_to 'blaa', users_path(:locale => 'de') 

我得到這樣一種形式:

<form action="/users?locale=de" class="button_to" method="post"><div><input type="submit" value="blaa" /><input name="authenticity_token" type="hidden" value="k4PtVPS041DnTmTqkfh+xBIyFvVJCauenmg233BoLSw=" /></div></form> 

,這個按鈕帶我到http://localhost:5000/users?locale=de

+0

你是對的,在CSS樣式的作品的鏈接,以及。當我第一次嘗試時,不知道什麼產生了不好的結果。因此,我切換到鏈接而不是按鈕。謝謝! – 2012-04-06 16:09:43