Antarr Byrd
Antarr Byrd

Reputation: 26151

Error when adding data attributes to rails button_to

I'm trying to add a data- attribute to button in my rails application. When I view the app I get the following error.

error

/app/views/versions/_full.html.erb:60: syntax error, unexpected ',', expecting => ...GI::escape version.plist_url}", class:"download-btn btn-prim... ... ^ /app/views/versions/_full.html.erb:62: syntax error, unexpected ',', expecting => ...ild_number}, version.plist_url, class: 'download-btn btn-pri... ... ^ /app/views/versions/_full.html.erb:62: syntax error, unexpected ',', expecting ')' ...-primary btn-small pull-right', method: :get );@output_buffe... ... ^

view

<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>

Upvotes: 1

Views: 2886

Answers (2)

Eric
Eric

Reputation: 2549

In the docs you'll see that :data is part of :html_options, which is the third argument. The second argument is usually the target of the button, usually a RESTful path or a hash containing an action target and sometimes a method. If there is no block, :html_options is everything after the second argument. Try something like:

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

Upvotes: 2

alexcavalli
alexcavalli

Reputation: 536

Switch the second and third parameters in your button_to calls:

<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>

Upvotes: 2

Related Questions