我正在使用遠程form_for我的show動作來檢索基於此窗體傳遞的參數的內容。Rails 3:控制器參數默認值
= form_tag modelname_path(@modelname), :id=>"select_content_form", :remote => true, :method => 'get' do
= text_field_tag :content_type, params[:content_type], :id=>"select_content_type"
= submit_tag "submit", :name => nil, :id=>"select_content_submit"
我改變控制器中的內容如下:
# Default params to "type1" for initial load
if params[:content_type]
@content_type = params[:content_type];
else
@content_type = "type1"
end
case @content_type
when "type1"
# get the content
@model_content = ...
when "type1"
# get the content
@model_content = ...
我的問題是,上述方法是否是唯一的,我們可以爲PARAMS設置默認值或者我們可以做一個更好的方式。這工作,但我想知道這是否是正確的方法。
UPDATE 基礎上的建議之下,我用下面的上了車defaults.merge行錯誤:
defaults = {:content_type=>"type1"}
params = defaults.merge(params)
@content_type = params[:content_type]
嗯,有趣。但是,它給了我以下錯誤: *不能將零轉換爲散列* – rgoraya 2012-01-31 08:16:34
它是否會拋出defaults.merge? – 2012-01-31 17:14:15
是的,它確實,修改了帖子以包含代碼。 – rgoraya 2012-01-31 20:12:46