How to save different values for the checkbox field labels, in ruby on rails? - ruby

I have a collection checkboxes in my form, like below.
<% ["cricket" ,"tennis", "not there in list"].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>
Currently the label of the checkbox, and its value that is getting saved in the db, both are same. i want to save different values to the db for the labels. i tries like below. but its not working. can anyone help me with this.
<% [["cricket","cri"] ,["tennis","ten"], ["not there in list","na"]].each do |c| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(c), class: "reason-for-the-test"}, c, "" %> <%= c.capitalize.tr("_"," ") %></p>
</div>
<% end %>

# ⇓⇓⇓⇓⇓⇓⇓⇓⇓
<% [["cricket","cri"], ["tennis","ten"], ...].each do |name, val| %>
<div class="col-md-4" >
<p><%= f.check_box :game, {:multiple => true, checked: #training.game&.include?(val)}, name, "" %> <%= name %></p>
</div>
<% end %>

Related

How would I get all people, that are following my in Ruby

I am using the act_as_follower gem, and I get all the users, that I follow, but now I want all the users, that are following me, how would I do that. I checked and I think something like this
current_user.followers, but I cant get it working. My code for people that I am following is this:
<% #users = User.all %>
<% #users.each do |user| %>
<% unless user == current_user %>
<div class="container people-lists list-following">
<%= image_tag("/assets/" + user.avatar + ".png", alt: "Logo", class: "people-image") %>
<%= user.username %>
<% if user_signed_in? %>
<% unless user == current_user %>
<% if current_user.following?(user) %>
<%= button_to("Un-Follow", user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, class: 'btn btn-primary button') %>
<% else %>
<%= button_to("Follow", user_follows_path(user.to_param), :remote => true, class: 'btn btn-primary button') %>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
I found I the User.all returns all of them, and I can use the all_following, and follows like this and it works.
<% #users = current_user.all_following %>
And like this
<% #users = current_user.followers %>

Do I need an Enumerator for this?

I want to do this:
<div class="menu">
<%- render_menu do |title,path,children| %>
<%= link_to title, path %>
<div class="submenu">
<%= render_menu(children) do |title,path,children| %>
<%= link_to title, path %>
<%= children %>
<%- end %>
</div>
<% end %>
</div>
The method render_menu would look something like this:
def render_menu(children=nil)
children = Paths.roots if children.nil?
children.collect do |child|
[ child.title, child.path, child.children ]
end
end
I'm not sure what the render_menu needs to return to get the three params..
The render_menu will grab the default menu items if no argument is given..
You have to use yield and replace each for collect inside render_menu:
def render_menu(children=nil)
children = Paths.roots if children.nil?
children.each do |child|
yield([child.title, child.path, child.children])
end
end
You should also modify your template to not display the value returned by render_menu:
<div class="submenu">
<% render_menu(children) do |title,path,children| %>
<%= link_to title, path %>
<%= children %>
<% end %>
</div>

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.

add form (non-nested) dynamically in rails

pls can someone explain how I can dynamically add another copy of a form in rails?...been working on this for almost 2hrs. I messed around with .clone() and .appendTo in jquery, but it didnt work. Also, most of the materials I found online (like on railscast #196 and stackoverflow) focused heavily on nested forms. My form is nested, but I actually just want to add the parent form again. The photos which are nested use the html multiple attributes so I'm guessing that will handle multiple files upload for each parent form (btw I'm using paperclip).
If I just need to modify the railscast code please let me know.
Thanks.
<%= form_for(#user_book, html: { multipart: true }) do |f| %>
<% if #user_book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user_book.errors.count, "error") %> prohibited this user_book from been saved:</h2>
<ul>
<% #user_book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<p>hello</p>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :title, placeholder: "enter title...", id: "book_title" %>
<%= f.text_field :category, placeholder: "enter category..." %>
<%= file_field_tag 'user_book[user_book_photos_attributes][][photo]', :multiple => true do |p| %>
<%= p.file_field :photo %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Resources