Button with a value - Ruby on Rails

Asked

Viewed 914 times

2

I have 2 buttons and each one will have to send a template value and will also be the submit

button 1 and button 2 - depending on the button I click it will send the value to the @rating.type field. Similar to a like and dislike. The possible code I imagine should be on view in the helper form_for

[button 1]:

<%= form_for(@avaliacao) do |f| %>
    <%= render 'compartilhado/mensagens_erro', object: f.object %>
    <%= f.hidden_field :item_id %>
    <div class="field">
      <%= f.text_area :avaliacao, placeholder: "Escreva seu Comentario..." %>
    </div>
<!-- Seria isso no f.submit?? -->
    <div class="col-md-4.5 col-md-offset-8">
      <%= f.submit "Avaliar!", class: "btn btn-large btn-primary" %
    </div>
<% end %>

1 answer

0


Try the following (I have not tested in practice):

Create the field @avaliação.tipo of the kind hidden:

<%= f.hidden :tipo, id: 'hidden_tipo' %>

Make the Submit invisible:

<%= f.submit "Avaliar!", class: "..." style='display: none' id='submit' %>

Create both buttons and use jQuery to change the Hidden field and submit the form:

$('#botao1').click(function() {
  $('#hidden_tipo').val('like');
  $('#submit').click();
});
  • Thanks buddy, it worked here! I thought they would have two buttons from Submit! Good to know that you can do things like this!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.