passing value from select_tag - ruby

I want to pass selected :ammount value to my controller as :quantity. What i am doing wrong?
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= button_to 'Add to cart', line_items_path(:product_id => product.id, :quantity => :ammount) %>

You should use form here to pass the data to controller`s action:
<%= form_tag line_items_path(:product_id => product.id) do %>
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= submit_tag 'Add to cart' %>
<% end %>
All that you did in your sources is just passing symbol :ammount to a controller`s action as a quantity param.

Related

pass a string to a hidden field in rails simple form

I am trying to build a simple message relay system from scratch. My form looks like this:
<%= simple_form_for #message do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Your message" %>
<%= f.input :target_user_id, label: "Who are you sending your message to?" %>
<%= hidden_field_tag 'user_id', current_user.id %>
<%= hidden_field_tag 'sender_email', current_user.email %>
<%= f.button :submit %>
<% end %>
I am trying to pass "current_user.email" to the message model, where "Message.sender_email" is a string, but I am getting "nil" when I look at the created Message in the console. All the other fields do get passed though.
Just add this line to your controller:
#message.sender_email = #user.email
in the message create method.

cached votes not updating with validates_presence_of in model

I have acts_as_taggable installed and working for my products with cached votes.
After I tried to finish my validations inside the Product model with validates_presence_of the cached votes are not getting updated anymore.
Anyone had the same problem?
Any hint is appreciated.
Basic validation inside the product model. If this line is not there, the cached_votes work:
validates_presence_of :original_url, :format => URI::regexp(%w(http https))
How I trigger the votes:
<% if user_signed_in? %>
<% if current_user.liked? likeable %>
<%= form_tag unlike_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), method: :post, remote: true do %>
<% button_tag class: 'btn btn-block liked' do %>
<%= fa_icon 'heart-o' %> unlike
<% end %>
<% end %>
<% else %>
<%= form_tag like_path(likeable_type: likeable.class.to_s, likeable_id: likeable.id), remote: true do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>
<% else %>
<%= link_to new_user_registration_path do %>
<% button_tag class: 'btn btn-block' do %>
<%= fa_icon 'heart' %> like
<% end %>
<% end %>
<% end %>

Get value of a hidden field tag Ruby on Rails

I am lost, I do not know what I'm doing wrong! I have 4 radio buttons and a hidden field (value = "1"). When you click on the second radiobutton, the value of the hidden field changes to 2 and so on. This works fine with a js function.
Different divs will be showed when a different radiobutton is selected. Now, when I'm trying to get the value of the hidden field in my controller it always returns nil.
Here's the code:
view:
(radiobuttons, hiddenfield and one div)
<div>
<%= form_tag patients_path do %>
<%= radio_button_tag 'searchRBN', 'patient', true, :onchange => "checkRadioButton()" %>
<%= label_tag :byPatient_patient, "Patient" %>
<%= radio_button_tag 'searchRBN', 'staff', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byStaff_staff, "Staff" %>
<%= radio_button_tag 'searchRBN', 'ocmw', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byOcmw_ocmw, "OCMW" %>
<%= radio_button_tag 'searchRBN', 'mutuality', false, :onchange => "checkRadioButton()" %>
<%= label_tag :byMutuality_mutuality, "Mutuality" %>
<%= hidden_field_tag :hidden_one, "1" %>
<% end %>
</div>
<div id="searchByPatient">
<%= form_tag patients_path, :method => 'get' do %>
<p>
<%= text_field_tag :search1, params[:search1] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</div>
controller:
def index
#staff_all = Staff.all
#ocmw_all = Ocmw.all
#mutuality_all = Mutuality.all
debugger
if params[:hidden_one] == '1'
#patients = Patient.searchByName(params[:search1])
elsif params[:hidden_one] == '2'
#patients = Patient.searchByStaff(params[:search2])
else
#patients = Patient.all
end
end
It's because you have two forms. When you submit the second form it won't send the fields of the first form. If you put everything in one form it will work as expected.
Use only single form:
Also as a workaround use two submit tag in a single form:
differentiate both the action with params[:action]
For Example:
<%= form_for :attachment_metadata, :url=>{:action=>'delete_files'}, :html=>{:onsubmit=> "return confirm('Are you sure, you want to delete selected files?');",:multipart => true} do |f| %>
<table>
..........Some stuff here..........
</table>
<%= submit_tag 'Reprocess', :class =>'button' %>
<%= submit_tag 'Remove', :class =>'button' %>
<% end %>
params[:commit] can differentiate the actions of two submit tags.
#action = params[:commit]
it gives #action value as "Reprocess" if your click the Reprocess button and gives "Remove" value if you click the Remove button,
Then you will get your values.

Rails submit remote form onclick of radio button

I have a form that submits when you chose a radio button (below). Currently the page refreshes when it submits and I'd like to use Jquery and remote submission so it doesn't refresh. Any idea where to start with this?
<%= simple_form_for(#order) do |f| %>
<%= f.input :orderstatus_id, :as => :radio, :label => false do %>
<% current_user.account.orderstatuses.order(:status_order).each do |os| %>
<p id="invoice-color-select">
<%= f.radio_button :orderstatus_id, os.id, :class => 'selector', :onclick => "this.form.submit();" %>
<%= f.label "#{os.name}", :style => "background: ##{os.color}", :class => 'status' %>
</p>
<% end %>
<% end %>
<div style="margin-top:20px">
<% if !current_user.account.has_plan? || current_user.account.plan_key== "ultra" %>
<a class="fancybox" href="#editstatus">Edit</a>
<% end %>
</div>
<% end %>
I added this into my view:
<%= f.radio_button :orderstatus_id, os.id, :class => 'selector', :onclick => "this.form.submit();" %>
You can see the :onclick portion here.

semantic_form_for with an alert upon completion

I am using semantic_form_for and I wonder if there is such attribute as :complete then upon successful creation, the form would alert something so that the user would have to click on "Okay" to proceed?
Do you mean confirmation before submit?
you could use something like the following:
<%= semantic_form_for #user do |f| %>
...
<%= f.buttons do %>
<%= f.commit_button :button_html => {:confirm => 'Are you sure?'} %>
<% end %>
<% end %>
My solution is like the following and it works well:
<%= semantic_form_for #post do |f| %>
...
<%= f.action :submit, :button_html => { 'data-confirm'.to_sym => 'Are you sure?' } %>
<% end %>

Resources