0

这个bug让我很困惑。

我正在显示一个带有最后 5 个数据点的小部件和一个新数据点的表单。当我尝试渲染表单和显示部分时,它会因路由错误而失败。如果我注释掉任何一个部分,它就可以正常工作。因此,只有部分形式它可以工作,或者只有显示部分它可以与其他部分一起工作。

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

当两者render :partial => ...都未注释时,看起来好像传递给第二个部分的 data_point 与传递给第一个部分的 data_point 相同。只有一个 data_set_id 全部为零。我尝试将本地从 :data_point 重命名为 :dp 以防发生名称冲突。

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

当您尝试在视图中渲染多个部分时,将 :locals 传递给部分是否有限制?

查看: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.
4

1 回答 1

0

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

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

于 2012-08-03T01:56:10.777 回答