简体   繁体   中英

Using a bootstrap glyphicon in a Rails button using button_tag or form_for

The seemingly trivial task of using a bootstrap glyphicon in a form submit button is anything but. I can't get button_tag or form_for to work.

button_tag successfully shows the glyphicon, but fails because (looking at the console) it doesn't submit a request to the server. It's dead, nothing happens:

<%= button_tag(type: 'submit', name: nil, class: 'btn btn-default btn-xs', id: 'vote_button', 
               path: postvoterelationships_path, remote: true) do %>
  <span class='glyphicon glyphicon-star-empty' aria-hidden="true"></span>
  <%= hidden_field_tag(:voter_callsign, @character.callsign) %>
  <%= hidden_field_tag(:voted_id, @post.id) %>
<% end %>

form_for is failing because, while it successfully sends a request, I can't get it to display the glyphicon. The following two variations both just create a large grey button with 'Create Postvoterelationshiop' in the middle:

<%= form_for(@character.active_post_vote_relationships.build, remote: true) do |f| %>
  <div><%= hidden_field_tag(:voter_callsign, @character.callsign) %></div>
  <div><%= hidden_field_tag(:voted_id, @post.id) %></div>
  <%= f.submit do %>
    <span class="glyphicon glyphicon-star-empty"></span>
  <% end %>
<% end %>

<%= form_for(@character.active_post_vote_relationships.build, remote: true) do |f| %>
  <div><%= hidden_field_tag(:voter_callsign, @character.callsign) %></div>
  <div><%= hidden_field_tag(:voted_id, @post.id) %></div>
  <%= f.submit class: "glyphicon glyphicon-star-empty" %>
<% end %>

How do I get either button_tag to submit the request, or form_for to display the glyphicon?

You could try button_to

<%= button_to postvoterelationships_path, class: 'btn btn-default btn-xs', params: {voter_callsign: @character.callsign, voted_id: @post.id}, remote: true do %>
  <span class="glyphicon glyphicon-star-empty"></span>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM