html - Create action is called by every button (not only "submit" one) -
i have form transaction, text , select fields. there text-field date, use jquery.ui.datepicker. i've hidden text-field , use buttons instead ("today", "yesterday", , "calendar-icon" buttons). "yesterday" , "today" buttons change value of hidden field, call submit action , create transaction if other fields filled or cause internal server error status 500. need call create action click on submit button.
this create action of transaction_controller.rb
def create if @transaction.save respond_to |format| format.html { redirect_to root_url } format.js end else render 'pages/index' end end this transaction_form new transaction action.
<%= form_for @transaction, remote: true |f| %> <%= render 'shared/error_messages', object: f.object %> <div class="form-row"> <%= f.text_field :name, class: 'input-medium', 'placeholder' => 'name' %> <%= f.text_field :amount, class: 'input-small', 'placeholder' => '$' %> <%= f.submit 'add', class: 'btn btn-primary' %> </div> <%= f.collection_select :category_id, current_user.categories.all, :id, :name, {}, class: 'input-medium' %> <%= f.collection_select :account_id, current_user.accounts.all, :id, :name, {}, class: 'input-small' %> <%= f.text_field :date, 'value' => date.today %> <div class="btn-group" id="transaction_date_group"> <button class="btn active" id="transaction_today">today</button> <button class="btn" id="transaction_yesterday">yesterday</button> <button class="btn dropdown-toggle" data-toggle="dropdown" id="transaction_date_dropdown"> <i class="icon-calendar"></i> </button> </div> <% end %> this create.js.erb
$('#transactions_table').prepend('<%= j render(@transaction) %>'); $('#new_transaction').each(function () { this.reset(); }); <% if @transaction.category_id == current_user.categories.first.id && current_user.categories.first.balance == @transaction.amount %> $('#categories_table').append('<%= j render(@category) %>'); <% end %> $('#edit_category_<%= @transaction.category_id %>').replacewith('<%= j render(@category) %>'); $('#edit_category_<%= @transaction.category_id %>').hide().fadein('slow'); $('#edit_account_<%= @transaction.account_id %>').replacewith('<%= j render(@account) %>'); $('#edit_account_<%= @transaction.account_id %>').hide().fadein('slow'); $('#accounts_total').replacewith('<td id="accounts_total"><%= current_user.accounts.sum(:balance) %></td>'); $('#accounts_total').hide().fadein('slow'); and transaction.js.coffee, used datepicker showing , changing classes of buttons, while clicking.
jquery -> $("#transaction_date").datepicker dateformat: "yy-mm-dd" $("#transaction_date_dropdown").click -> if $("#transaction_date_group").hasclass 'open' $("#transaction_date").datepicker 'hide' else $("#transaction_date").datepicker 'show' $("#transaction_today").removeclass 'active' $("#transaction_yesterday").removeclass 'active' $("#transaction_yesterday").click -> $("#transaction_today").removeclass 'active' $("#transaction_yesterday").addclass 'active' yesterday = moment().add('days', -1).format 'yyyy-mm-dd' $("#transaction_date").val yesterday $("#transaction_today").click -> $("#transaction_yesterday").removeclass 'active' $("#transaction_today").addclass 'active' today = moment().format 'yyyy-mm-dd' $("#transaction_date").val today thanks!
from fine specification:
a
buttonelement notypeattribute specified represents same thingbuttonelementtypeattribute set"submit".
so this:
<button class="btn active" id="transaction_today">today</button> is, in fact, submit button , submit buttons submit forms. if don't want <button>s submitting form, using type="button" button:
<button type="button" class="btn active" id="transaction_today">today</button> some older ies default type="button" despite (or in spite) of specification says should always specify type attribute when use <button>.
Comments
Post a Comment