我正在使用远程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