小编rgo*_*aya的帖子

Rails 3:控制器参数默认值

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

parameters model-view-controller ruby-on-rails-3

8
推荐指数
2
解决办法
2万
查看次数

Rails 3嵌套模型未知属性错误

但是,我遇到错误"未知属性:关系"

我在这里做错了吗?我确实看到关系属性在日志中传递给服务器,但是这个错误不会让创建成功.

我对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)

ruby-on-rails nested-forms railscasts ruby-on-rails-3

5
推荐指数
1
解决办法
9105
查看次数

Rails 3根据非键属性查找记录

  • 我有一个名为"Notes"的模型.
  • 属性是"id","url","content".
  • 在我的notes_controller中想要检索特定音符的"id",其中"url"与我的查询url匹配.

我使用以下内容:

    @noteid = Note.find(:url => "blah").id
Run Code Online (Sandbox Code Playgroud)

但是,这会返回错误:Unknown key(s):url

有没有办法根据非关键属性找到记录?

ruby-on-rails ruby-on-rails-3

0
推荐指数
1
解决办法
1537
查看次数