我正在使用远程form_for进行show动作,以根据此表单传递的参数检索内容.
= 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"
Run Code Online (Sandbox Code Playgroud)
我改变控制器中的内容如下:
# 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 = ...
Run Code Online (Sandbox Code Playgroud)
我的问题是,上述方法是否是我们唯一可以为params设置默认值的方法,还是我们能够以更好的方式进行.这有效,但我想知道这是否是正确的方法.
更新 根据下面的建议,我使用以下内容并在defaults.merge行上出错:
defaults = {:content_type=>"type1"}
params = defaults.merge(params)
@content_type = params[:content_type]
Run Code Online (Sandbox Code Playgroud) 在issue.rb我提到过:
has_many :relationships, :dependent => :destroy
accepts_nested_attributes_for :relationships, :allow_destroy => true
Run Code Online (Sandbox Code Playgroud)在relationship.rb我提到过:
belongs_to :issue
Run Code Online (Sandbox Code Playgroud)关注Ryan Bates Railcast#196我在issues_controller中有以下内容:
relationship = @issue.relationships.build
Run Code Online (Sandbox Code Playgroud)但是,我遇到错误"未知属性:关系"
我在这里做错了吗?我确实看到关系属性在日志中传递给服务器,但是这个错误不会让创建成功.
我对rails的专业知识是初学者的水平,如果我问一个可能被视为微不足道的问题,请原谅我.
谢谢您的帮助.
编辑:相关的控制器代码:
@relationship = @issue.relationships.build
#@relationship = Relationship.new(params[:relationship])
if @relationship.issue_id = ''
@relationship.issue_id = @issueid
end
if @relationship.cause_id = ''
@relationship.cause_id = @issueid
end
@relationship.save
redirect_to(:back, :notice => 'New Relationship was created')
Run Code Online (Sandbox Code Playgroud)
我在跟踪上看到的内容:
ActiveRecord::UnknownAttributeError in IssuesController#create
unknown attribute: relationship
Run Code Online (Sandbox Code Playgroud)
在Issue参数中,我看到关系参数按预期传递:
"relationship"=>{"issue_id"=>"100",
"cause_id"=>""}
Run Code Online (Sandbox Code Playgroud)
另一个更新 发布form_for代码:
- form_for Issue.new do |f|
.field
= f.text_field :description, :class=>"formfield", :id=>"frm_descr" …Run Code Online (Sandbox Code Playgroud) 我使用以下内容:
@noteid = Note.find(:url => "blah").id
Run Code Online (Sandbox Code Playgroud)
但是,这会返回错误:Unknown key(s):url
有没有办法根据非关键属性找到记录?