Add radio_button to each image rails - ruby-on-rails

When iterating through a collection of records, how can i add a radio_button to each image. here is what i have so far
<% #default_images.each do |d| %>
<%= image_tag(d.image_url(:campaign_form), 'data-image-id' => d.id)) %>
<% end %>
Im not sure on the syntax to generate a radio button with an associated image here.
I have previously done it within a form_for
f.collection_radio_buttons(:default_image_id, DefaultImage.all, :id, :image) do |b|
b.label { b.radio_button + image_tag(b.object.image) }
end
but ive already got the collection stored within #default_images in this scenario
any help appreciated
Thanks

Ended up doing this which seems to work
<% #default_images.each do |d| %>
<%= radio_button_tag :default_images, d.id %>
<%= image_tag(d.image_url(:campaign_form)) %>
<% end %>

Related

Stuck on looping through a form.select in Rails (using Elastic Search/Searchkick aggs)

I'm totally stuck on looping through a form.select in Rails (using Elastic Search/Searchkick aggs).
I can access the 'bucket' array (and thus "key" and "doc_count") when I don't pre-pend the form.select helper, but it just doesn't loop through when it's there. Not sure how I would get the options dynamically otherwise!
Does anyone know what might be going wrong (ignore the link_to stuff, I'll be changing that once I can get the actual "key")? Perhaps I'm using the form.select helper incorrectly? Thanks!
<% form.select #jobs.aggs["location"]["buckets"].each do |bucket| %>
<% if params[:location] == bucket["key"].to_s %>
<strong><%= link_to bucket["key"], request.params.except(:location)%></strong>
<% else %>
<%= link_to bucket["key"], request.params.merge(location: bucket["key"])%>
<% end %>
<% end %>
I figured it out! It was a syntax error on my part, as I forgot to use :location in the first part of the form.select, and additionally didn't wrap the block after this in [] brackets (presumably the options have to be in an array or hash with this helper).
Here is the code (simplified) which solved this for me:
<% form.select :location, [#jobs.aggs["location"]["buckets"].each do |bucket| %>
<%= bucket["key"] %>
<% end ] %>

Using a loop to create 10 similar fields in Rails

I'm a beginner on my first project. If I have 10 similar fields, is there a way to create them dry so they would still work with validations? I would also like to put the loop number in the attribute name, because the attributes are named field_name_1 through to field_name_10, for example.
<% 10.times do |asdf| %>
<%= f.input :field_name_(asdf + 1), etc %>
<% end %>
This may work !!!
<% (1..10).each do |n| %>
<%= f.input ("field_name_"+n.to_s).to_sym, etc %>
<% end %>

checkboxes for all elements in a model

I want to display a checkbox for all elements in a model called MyModel. Here is what I wrote:
<%= MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
It does show the checkboxes as expected but at the end, I also get a list of the model content as shown in this screenshot.
Actually, it seems to be related to <%= MyModel.all.each do |c| %> because just printing out simple text still prints the whole model table content at the end:
<%= MyModel.all.each do |c| %>
toto<br>
<% end %>
shows this screenshot
Any idea how to get rid of this list at the end?
Thanks!
<%- MyModel.all.each do |c| %>
<%= check_box_tag(:id) %>
<%= label_tag(:name, c[:name]) %><br>
<% end %>
- - evaluates code.
= - evaluates code and outputs.

Ruby on Rails: How to print contents of variable in view and make checkbox?

I am trying to create a checklist in rails using form_for. This checklist is taken from a table which I gained in the create action of my sign_ins controller:
#content = OrientationContent.where(site_id: session[:site_id])
In my view I want to use the form_for helper to iterate through the list in #content:
<%= form_for(:sign_ups ) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>
However this is not working and it produces two square brackets on the page: [].
How do I go through the list and print the name while creating a check box on the left of it? The check box does not have any meaning or data, I just need it present for reference.
Solved:
In the controller, need to pluck an individual field:
#content = OrientationContent.where(site_id: 1).pluck(:content)
In the view, structure as so:
<%= form_for(:sign_ups) do |f| %>
<% #content.each do |c| %>
<%= f.check_box nil %> <%= c %> <br>
<% end %>
<% end %>

Rails: Update only when checkbox is checked

I want to update a model - only lines where the checkox is clicked and insert a remark
View:
<%= form_tag update_fb_instruction_users_path, :method => :put do %>
<% #user_wishes.each do |u| %>
<%= u.user.name %>
<%= fields_for "instruction[]", u do |f| %>
<%= f.text_field :remark_tl %>
<% end %>
<%= check_box_tag "instruction_user_ids[]", u.id %>
<% end %>
Controller:
def update_fb
params[:instruction_user_ids].each do
#check = InstructionUser.update(params[:instruction].keys, params[:instruction].values).reject { |p| p.errors.empty? }
end
The issue there is that they all have the same name. So whatever value the last one is, that's what it will be in the request params.
It's a bit old, but you might want to check out the railscast here: http://railscasts.com/episodes/73-complex-forms-part-1. The basic idea is to use fields_for on top of each user object. I haven't done it myself before, otherwise i'd write a full solution :).

Resources