I'm currently using Stripe Checkout and I want to add the quantity to my "Pay now" button.
I'm currently doing this
<%= button_to "Pay Now", checkout_create_path, params: {
:article_id => #article.id}, remote: true %>
How can I add a field to my params of the button, filled by the user and add it to the params: field of my button ?
Like this it doesn't work :
<%= number_field :quantity, :class => "form-control" %>
Related
I have a reset password page. When user updates his/her password, I want to display some additional wordings (a custom message). My simple_form_for has this submit button:
<%= f.submit :value => "Update", id: "password_reset",:class => 'btn btn-default fr' %>
How can I display a message with a click of this submit button?
I am developing a rails application using the Ransack gem and below is the code that I have written so far to filter my database which works like a charm. Now what I am trying to do is to add additional button like filter options to my index view (where each button has pre-defined filter value). In other words, once the database is first filtered with a brand name, then I would like users to be able to further filter the database by clicking one of the buttons which has a pre-defined filter value of say 'colour = white', then rails will show all the data with the selected brand name and the colour of white).
Controller
class ProjectsController < ApplicationController
def index
#q = Project.ransack(params[:q])
#projects = #q.result(distinct: true)
#projects_count = #q.result.count
#projects = Kaminari.paginate_array(#projects).page(params[:page]).per(30)
end
Index View
<%= search_form_for #q, remote: true, :builder => SimpleForm::FormBuilder do |f| %>
<%= f.input :brand_id_eq, label: false, collection: Brand.all.map{ |f| [f.name, f.id] }, prompt: "All", input_html: { class: "form-control" } %>
<%= f.button :submit, label: "Search", input_html: { class: "btn btn-primary" } %>
<% end %>
...
<span class="data-sort all"><%= link_to "All",q: {color_cont: 'white'}, :class => 'link-sort', :remote => true, :method => :post %></span>
index.js.erb
$('#projects').html('<%= escape_javascript (render partial: 'index') %>').hide().fadeIn('slow');
The problem that I am facing with this approach using the Ransack gem is that when I click the link_to filter button, it does filter the database with the pre-defined filter value of 'white color' however it resets all the previously selected filter options.
Is my approach correct or any better way to achieve this other than using the link_to option?
SOLUTION
I finally got this working using the rail's scope method and a simple jQuery code as shown in my final code below. One thing that I did initially wrong was that I set the name of the scope same as one of my db column name which caused an error. Once I changed the scope name to 'status1', not 'stock_no', it started to work. Hope this helps.
Defined Scope
class ApplicationRecord < ActiveRecord::Base
scope :status1, -> { where( stock_no = "15251" ) }
def self.ransackable_scopes(auth_object = nil)
[:status1]
end
Index.erb
<%= f.hidden_field :status1 %>
<%= f.submit "Stock", :id => "status1", :onclick => "document.getElementById('q_status1').value = 1;", class: 'btn btn-primary status1' %>
Try this question. It is somewhat what you are trying to do, except instead of a submit button, just make yours a button for filtering. It needs to be inside your search_form_for I'm pretty sure as well. And then write a jquery function to submit when the button is clicked like:
$(document).on("turbolinks:load", function(){
$(".data-sort").on('click', function() {
$("form.your-search-form-classname").trigger('submit.rails');
});
});
UPDATE
Try removing the (boolean = true) attribute from the scope. I tested with a similar app of my own and it worked well.
UPDATE 2
I put the following in my app (where status is a column in by db just like your stock_no) and a got the correct query from my database:
<%= f.hidden_field :stock_no %>
<%= f.submit "Stock", :id => "stock_no", :onclick => "document.getElementById('q_stock_no').value = 1;", class: 'btn btn-primary stock_no' %>
scope :stock_no, -> { where( status: 2 ) }
def self.ransackable_scopes(auth_object = nil)
[:stock_no]
end
Are you sure you are putting the scope in the right model?
Replace
<%= link_to "All",q: {color_cont: 'white'}, :class => 'link-sort', :remote => true, :method => :post %>
with
<%= link_to "All",q: {color_cont: 'white', brand_id_eq: params[:q][:brand_id_eq]}, :class => 'link-sort', :remote => true, :method => :post %>
Here assumption is
Once the database is first filtered with a brand name, then I would like users to be able to further filter the database by clicking one of the buttons which has a pre-defined filter value
I'm trying to build a "two-submit-buttons" edit form that lead to the edit action.
I want to execute two different queries based on the button clicked by the user, but I'm not able to get the submit_tag name in my controller.
In my View
<%= form_tag("/points/update", :remote => true, :id => "edit_point_form", :method => :put) do %>
...
<%= submit_tag "Update all", :name => "submit_all" %>
<%= submit_tag "Update this point", :name => "submit_this" %>
...
In my controller :
def update
logger.debug params.inspect
...
gives the following :
{"utf8"=>"✓", "_method"=>"put",
"authenticity_token"=>"w4RserYUA+NWpnWWamTd9dPy/0DDEDU9LRHQ3g25NFk=",
"word"=>{"4"=>"T531", "3"=>"", "1"=>"Test 531"},
"point"=>{"id"=>"106", "group_id"=>"22"},
"action"=>"update",
"controller"=>"points",
"id"=>"update" }
where all these fields are the fields in the form but I can't get the submit_tag name in it. What am I missing ?
Now I have 2 forms that submit a comment.
Form Type A
<%=form_for(([#community, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Form Type B
<%=form_for(([#user, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Now, I want to have link_to button that functions as same as those forms do if a user clicks it.
When the user click on the link, #comment will be automatically filled just like below.
From Form Type A
#comment = "this is for community"
From Form Type B
#comment = "this is for user"
How can I do that? As far as I understand my situation.
Form is put type, then link_to is get type so it's impossible to re-use transaction of form.
Not sure what you mean by "transaction of form" but if you're asking if you can create/modify data via a single button or link than the answer is Yes, it is possible.
You can actually put with a link_to in rails ({:method => :put} (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
If you want a button to do this you should checkout button_to (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to)
It's better to use button_to.
I'm trying to setup a formtastic form with multiple submit actions, following along with Railscast #38. What's the equivalent of this in Formtastic?
<%= submit_tag 'Create' %>
<%= submit_tag 'Preview', :name => 'preview_button' %>
This post gave me hope, but it looks like commit_button was deprecated in 2.1.0, and I can't seem to figure out the new syntax.
Here's my code for what it's worth. I'd like each submit button to go to the same controller, where I will handle them differently:
# Use prepaid credits to checkout
<%= f.action :submit, :as => :button, :label => "Order Critique (1 credit will be spent)", :button_html => { :class => "btn btn-primary", :disable_with => 'Processing...' } %>
# Use credit card to checkout
<%= f.action :submit, :as => :button, :label => "Order Critique ($10)", :button_html => { :class => "btn btn-primary", :disable_with => 'Processing...' } %>
TL;DR: If you use javascript to submit a form, it won't carry over the submit button's name in the commit params.
My problem ended up being the code used in Railscasts Episode #288. This CoffeeScript function gets fired when you submit the form, after the Stripe token checks out:
handleStripeResponse: (status, response) ->
if status == 200
$("#stripe_card_token").val(response.id)
$("#my_form_id")[0].submit()
else
# other stuff
Since javascript is doing the form submission with $("#my_form_id")[0].submit(), the name parameter won't be carried over in the commit params.
My solution was to add a "clicked" attribute to the button that gets clicked...
$('form_input[type=submit]').click ->
$('input[type=submit]', $(this).parents('form')).removeAttr('clicked')
$(this).attr('clicked', 'true')
..and then grab the id attribute of the clicked button populate a hidden field with it:
submitter = $("input[type=submit].clicked=true").attr("id")
I don't particularly like this solution. It feels like my js knows too much, and I don't like depending on js for this sort of thing. Any criticism or better solutions are certainly welcome :)