从同一控制器渲染多个部分时未设置本地

时间:2012-08-01 01:37:59

标签: ruby-on-rails ruby-on-rails-3.2

这个错误使我感到困惑。

我正在显示一个包含最后5个数据点的Widget和一个新data_point的表单。当我尝试渲染表单和显示部分时,它失败并出现路由错误。如果我评论 部分它可以正常工作。因此,只有部分表单可以正常工作,或者仅使用显示部分它可以正常工作。

三个相关模型/控制器:Widget,DataSet和DataPoint。 DataPoints是STI,因此我有不同的部分来显示与类名匹配的不同类型。

当两个render :partial => ...都被取消注释时,看起来传递给第二个partial的data_point与传递给第一个partial的data_point相同。只有一个data_set_id,它都是零。我尝试将本地从:data_point重命名为:dp,以防它是名称冲突。

查看,底部显示部分和路由错误。

当您尝试在视图中渲染多个部分时,是否存在传递局部变量的限制?

查看:show.html.haml(控制器上的show动作为空)

- title "#{@widget.type.underscore.humanize} for #{@widget.data_set.name.to_s.downcase}"

.row

  .span6
    = render :partial => 'data_points/form', :locals => { :data_set => @widget.data_set, :data_point => @widget.data_set.data_points.build }
    %br
    - last_5 = @widget.data_set.data_points.last(5)
    - if last_5.count > 0
      %h3= "Last #{pluralize( last_5.count, 'Entry' )}"
      - last_5.each do |dp|
        %pre
          = dp.inspect
        = render :partial => "data_points/#{@widget.data_set.data_type.underscore}", :locals => { :data_set => @widget.data_set, :data_point => dp }
    - else
      %h3 No data yet!

    = form_tag( push_widget_path @widget, :method => 'POST' ) do
      = submit_tag( "Push Data", :class=>"btn btn-primary" )
      = link_to "Edit your data", data_set_path( @widget.data_set ), :class=>'btn'

  .span6
    = image_tag "widgets/#{@widget.type.underscore}.png"
    %p
      Using the 
      %b= link_to @widget.data_set.name, @widget.data_set
      data set.

      = render :partial => "widgets/#{@widget[:type].underscore}", :locals => { :widget => @widget } rescue nil
    %p
      = link_to 'Edit', edit_widget_path(@widget), :class=>'btn'

表格部分(data_points / _form.html.haml)

%pre
  = data_set.inspect
%pre
  = data_point.inspect
= form_for [data_set,data_point], :html=>{:class => 'well form-horizontal'} do |f|
  -if data_point.errors.any?
    #error_explanation
      %h2= "#{pluralize(data_point.errors.count, "error")} prohibited this data_point from being saved:"
      %ul
        - data_point.errors.full_messages.each do |msg|
          %li= msg
  = render :partial => "data_points/#{data_set.data_type.underscore}_form", :locals => { :f => f, :data_point => data_point, :data_set => data_set }
  = f.text_field :created_at, :type=>'datetime'
  .form-actions
    = f.submit 'Save'

显示部分(data_points / _multi_value_data_point.html.haml)

.data-point{ :id=>"data-point-#{data_point.id}"}
  %span.value 
    - data_point.data.map do |k,v|
      - label = data_set.properties[:data_series][k][:name]
      %span.key= "#{label}: "
      = v
  .meta
    = data_point.created_at 
    %span.edit
      = link_to 'Edit', edit_data_set_data_point_path( data_set, data_point )
    %span.delete
      = link_to 'Destroy', [data_set, data_point], :method => :delete, :data => {:confirm => 'Are you sure?'}
    =# link_to 'Edit', '#', id: "edit-data-point-#{data_point.id}"
    :javascript
      $("#edit-data-point-#{data_point.id}").click( function(e) {
        $("#data-point-#{data_point.id}").html( "#{escape_javascript( render :partial => "data_points/multi_value_data_point_form", :locals => { data_set: data_set, data_point: data_point } )}");
        return false;
      });

路由错误

Routing Error

No route matches {:action=>"edit", :controller=>"data_points", :data_set_id=>#<MultiValueDataSet id: 4, ... properties removed ... type: "MultiValueDataSet">, :id=>#<DataPoint id: nil, data: {}, data_set_id: 4, created_at: nil, updated_at: nil>}
Try running rake routes for more information on available routes.

1 个答案:

答案 0 :(得分:0)

问题源于这样一个事实:表单正在构建一个未保存的新元素,最后一个是返回未保存的元素。

简单的解决方案,用.find(:all,order:'id desc',limit:5)替换.last(5)