I am trying to implement Ajax search. When the user enters the query into the text field, the data should display below it and if there are not results, a new entry should be created in the database.
I have a button next to the text field. When the user presses the button, the data in the text field should be posted to the create method which creates a new record in the database. How can I do that?
Now i am using this:
<%= form_tag song_requests_path, :method => 'get', :id => "requests_search", :class => 'well form-horizontal' do %>
<p>
<h2> I Love To Sing ...</h2>
<%= text_field_tag :search1, params[:search1], :class => 'text_field span4',:placeholder=>"Song Title", :maxlength => "250" %>
<%= text_field_tag :search2, params[:search2], :class => 'text_field span3', :placeholder=>"Album", :maxlength => "250" %>
</p>
<% end %>
<%=form_for( :song_request, :remote => true) do |f| %>
<center>
<%= f.hidden_field :title, :value => "#{params[:search1]}" %>
<%= f.hidden_field :album, :value => "#{params[:search2]}" %>
<%= f.submit " Request ", :class => 'btn btn-primary'%>
</center>
<%end%>
Functionally its working perfect, but i can't place the request button besides the text field...Thank u....
Related
When I submit a remote form in rails, the form POST duplicate data, even though if I manually serialize the form using jQuery shows that there are no duplicates.
What could be causing this?
*Normally this wouldn't be a problem, but one of the form data is an array, as a result there are duplicate value in the submitted array.
Result from chrome's network inspector
Result from jQuery
UPDATE
Here is the gist of my form (removing the html markups):
<% #order.available_payment_methods.each do |method| %>
<%= radio_button_tag "order[payments_attributes][][payment_method_id]", method.id, method == #order.available_payment_methods.first %>
<%= Spree.t(method.name, :scope => :payment_methods, :default => method.name) %>
<%= render :partial => "spree/checkout/payment/#{method.method_type}", :locals => { :payment_method => method } %>
<% end %>
# partial
<% param_prefix = "payment_source[#{payment_method.id}]" %>
<%= label_tag "name_on_card_#{payment_method.id}", Spree.t(:name_on_card) %><span class="required">
<%= text_field_tag "#{param_prefix}[name]", "#{#order.billing_firstname} #{#order.billing_lastname}", { id: "name_on_card_#{payment_method.id}", class:'form-control'} %>
<% options_hash = Rails.env.production? ? {:autocomplete => 'off'} : {} %>
<%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(:id => 'card_number', :class => 'required cardNumber form-control', :size => 19, :maxlength => 19, :autocomplete => "off") %>
<%= text_field_tag "#{param_prefix}[expiry]", '', :id => 'card_expiry', :class => "required cardExpiry form-control", :placeholder => "MM / YY" %>
<%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(:id => 'card_code', :class => 'required cardCode form-control', :size => 5) %>
<% if #order.bill_address %>
<%= fields_for "#{param_prefix}[address_attributes]", #order.bill_address do |f| %>
<%= render :partial => 'spree/address/form_hidden', :locals => { :form => f } %>
<% end %>
<% end %>
<%= hidden_field_tag "#{param_prefix}[cc_type]", '', :id => "cc_type", :class => 'ccType' %>
UPDATE Submitting the form without remote: true fixes the problem (BUT I NEED IT!)
Add onsubmit="return false();" on your form it will make sure that form is not submited through default form submission. It will be submitted through ajax as you are adding remote: true
i'm creating a form where a user can choose a product and a quantity. I need to pass the id value from the object #event to the controller, but i dont know whats the right way to do it. As it is now, it the params[:event_id] field is always nil in the controller.
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
Add hidden filed in your form and set its value equal to #event.id
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= hidden_field_tag :event_id, value: #event.id%> #add this
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
<% end %>
You can use now event id in your controller as params[:event_id]
<%= hidden_field_tag :event_id, #event.id %>
So this will be available in params[:event_id].
Simply pass an hidden field in form as followings
<%= hidden_field_tag :event_id, value: #event.id%>
It will available in controller as
params[:event_id]
I am building message app in rails where user can send message from templates. I have a database of templates, and the user can select templates as per category.
In my message model, I want to render the template dynamically based on category selected. I looked for examples on google, but was not able to find a relevant solution.
This is my message form:
<%= form_for #message, :html => {:multipart => true} do |m| %>
<%= m.select :biz_case, options_for_select(Message::Bcase), :prompt => "Select business case" %>
<%= m.text_field :subject, :class => "message-text", :placeholder => "Subject" %>
<div class="message-body">
<%= m.text_area :message, :class => "message-body", :class => "redactor", :placeholder => "Your content" %>
</div>
<%= m.select :user_type, options_for_select(Customer::CType), :prompt => "Customer segment" %>
<%= m.submit %>
<% end %>
In the above form, I am looking to display the subject and body based on the selected business case. Something like:
if biz_case == "promote"
subject = #template.subject where ("biz_case = ?", "promote")
message = #template.content where ("biz_case = ?", "promote")
end
The subject and message would be displayed in input text fields.
Can anyone tell me how to do this?
in your method:
#subject = #template.subject where ("biz_case = ?", "promote")
in view:
<%= m.text_field :subject, :value => #subject, :class => "message-text", :placeholder => "Subject" %>
I have been trying to pass what round the users intends to go to by which button they click through a hidden field that passes in the round number.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
With this code I constantly get 2 passed through as the round in my pick_page. It is obviously skipping over the first hidden field. How can I get it so 'f.submit' submits the round number that is associated with its class.
We just had the same problem! We fixed it creating 2 diferent forms right next to each other.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
Change it to
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<% end %>
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
You can just remove the hidden fields and add a name and value attributes on your submit button.
I'm trying to create a feedback form where each student can list 2 interests.
However, with my code below, it only recognizes interest_1 and interest_2 for the last student and maps the values to all students, instead of a different value for each student. Anyone can shed some light on how to make interest_1 and interest_2 specific for each student?
<%= form_tag feedback_send_path, :method => 'post' do %>
<% #course.students.each do |student| %>
<%= avatar_for(student, :photo_size => :thumb_small, :class => 'notBig', :size => '50x50') %>
<%= link_to student.name, user_path(student.id), :class => "profile-link" %>
<%= text_area_tag 'interest_1', '', :class => 'feedback-form', :placeholder => "Wants to teach..." %>
<%= text_area_tag 'interest_2', '', :class => 'feedback-form', :placeholder => "Wants to learn..." %>
<% end %>
<%= submit_tag "Submit", :class => "send-message-button" %>
<% end %>
`
Instead of
<%= text_area_tag 'interest_1', '', :class => 'feedback-form', :placeholder => "Wants to teach..." %>
<%= text_area_tag 'interest_2', '', :class => 'feedback-form', :placeholder => "Wants to learn..." %>
Use
<%= text_area_tag 'interest_1[]', '', :class => 'feedback-form', :placeholder => "Wants to teach..." %>
<%= text_area_tag 'interest_2[]', '', :class => 'feedback-form', :placeholder => "Wants to learn..." %>
Then have a look at your params Hash!