将数据属性添加到rails button_to时出错

时间:2015-02-26 22:15:48

标签: ruby-on-rails ruby ruby-on-rails-4 view-helpers

我正在尝试在rails应用程序中为按钮添加数据属性。当我查看应用程序时,我收到以下错误。

错误

  

/app/views/versions/_full.html.erb:60:语法错误,意外',',   期待=> ... GI :: escape version.plist_url}“,class:”download-btn   btn-prim ... ... ^ /app/views/versions/_full.html.erb:62:语法错误,   意外的',',期待=> ... ild_number},version.plist_url,类:   'download-btn btn-pri ...... ^ /app/views/versions/_full.html.erb:62:   语法错误,意外',',期待')'...- primary btn-small   pull-right',method :: get); @ output_buffe ...... ... ^

视图

<li class="download cols-lg-4 pull-right">
  <% if (version.app[:app_type] == 'ios') %>
    <%= button_to 'Download', data: {build: version.build_number}, "itms-services://?action=download-manifest&url=#{CGI::escape version.plist_url}", class:"download-btn btn-primary btn-small pull-right" %>
  <% else %>
    <%= button_to 'Download', data: {build: version.build_number}, version.plist_url, class: 'download-btn btn-primary btn-small pull-right', method: :get %>
  <% end %>
</li>

2 个答案:

答案 0 :(得分:1)

切换button_to调用中的第二个和第三个参数:

<li class="download cols-lg-4 pull-right">
  <% if (version.app[:app_type] == 'ios') %>
    <%= button_to 'Download', "itms-services://?action=download-manifest&url=#{CGI::escape version.plist_url}", data: {build: version.build_number}, class:"download-btn btn-primary btn-small pull-right" %>
  <% else %>
    <%= button_to 'Download', version.plist_url, data: {build: version.build_number}, class: 'download-btn btn-primary btn-small pull-right', method: :get %>
  <% end %>
</li>

答案 1 :(得分:1)

In the docs您会看到:data:html_options的一部分,这是第三个参数。第二个参数通常是按钮的目标,通常是RESTful路径或包含操作目标的哈希,有时是方法。如果没有阻塞,:html_options就是第二个参数之后的所有内容。尝试类似:

<%= button_to 'Download', 
      version.plist_url, 
      data: {build: version.build_number}, 
      class: 'download-btn btn-primary btn-small pull-right', 
      method: :get %>