I have here four images, what I want if one of those images are empty to not showing it as missing. here's an image to explain more
<% [#car.image.url, #car.image2.url, #car.image3.url, #car.image4.url].each do |image| %>
<%= link_to "javascript:void(0)", data: { image: image, zoom_image: image } do %>
<%= image_tag image%>
<% end %>
<% end %>
Well, a simple if will make it work
<% [#car.image.url, #car.image2.url, #car.image3.url, #car.image4.url].each do |image| %>
<%= link_to "javascript:void(0)", data: { image: image, zoom_image: image } do %>
<%= image_tag image%>
<% end if image.present? %>
<% end %>
or, you can compact the array, then iterate. Like this:
<% [#car.image.url, #car.image2.url, #car.image3.url, #car.image4.url].select(&:present?).each do |image| %>
<%= link_to "javascript:void(0)", data: { image: image, zoom_image: image } do %>
<%= image_tag image%>
<% end%>
<% end %>
Related
In the code below, #carousel is a collection of CarouselImages. If the first CarouselImage has a populated "link_to" field, a picture link is displayed, otherwise the picture is displayed without a link.
<% if #carousel.first.link_to && #carousel.first.link_to <> "" %>
<%= link_to image_tag(#carousel.first.image, class: "main-image",
data: {image_id: #carousel.first.id,
slide_no: 0, active: "true"}), #carousel.first.link_to %>
<% else %>
<%= image_tag(#carousel.first.image, class: "main-image",
data: {image_id: #carousel.first.id,
slide_no: 0, active: "true"}) %>
<% end %>
Is it possible to DRY this out and, if so, how?
<% img_tag = image_tag(#carousel.first.image, class: "main-image",
data: {image_id: #carousel.first.id,
slide_no: 0, active: "true"}) %>
<% if #carousel.first.link_to && #carousel.first.link_to != "" %>
<%= link_to img_tag, #carousel.first.link_to %>
<% else %>
<%= img_tag %>
<% end %>
I have a collection_radio_buttons on a simple_form form on Ruby on Rails.
Code:
<%= f.collection_radio_buttons :player_out, #lineup.lineup_players.starting_players, :id, :name, label: "Player to get out?",
collection_wrapper_tag: :fieldset, collection_wrapper_class: "form-group radio_buttons optional substitution_suggestion_player_out",
item_wrapper_tag: :div, item_wrapper_class: "form-check" do |player| %>
<%= player.radio_button %>
<%= player.label { tag.div(player.text) } %>
<% end %>
I have two question about it:
1 - My label ("label: 'Player to get out?'") is not working. What I'm doing wrong?
2 - How do I call other methods of the collection? For example to get the player's avatar. :
<%= player.label { tag.div(image_tag(player.avatar) player.text) } %>
Thanks in advance.
David
If you want to display an image with radio button you can do something like,
<%= f.collection_radio_buttons :player_out, #lineup.lineup_players.starting_players, :id, :name do |player| %>
<%= player.radio_button %>
<%= player.label do %>
<%= player.text %>
<%= image_tag player.avatar, height: '100px', width: '100px' %>
<% end %>
<% end %>
In addition to this, by separately creating the element will allow you to apply a class to them easily.
To push a html code to the label block of the collection_radio_buttons I end up creating a model method that return the html that I wanted to use, and use that method as the text method for the collection.
In my model:
def html_to_radio_button
html = ""
html << "<a class='avatar rounded-circle mr-3' style='background:none !important;'>"
html << "<img alt='Image placeholder' src='#{ApplicationController.helpers.url_for_player_avatar(self.player)}'>"
html << "</a>"
html << "<div class='media-body'>"
html << "<span class='mb-0 text-sm'>#{self.name}</span><br>"
html << "<small class='text-muted'>#{self.player.position}</small>"
html << "</div>"
html.html_safe
end
On the form:
<%= f.collection_radio_buttons :player_out,lineup.lineup_players.starting_players, :id, :html_to_radio_button, collection_wrapper_tag: :fieldset, collection_wrapper_class: "form-group radio_buttons optional substitution_suggestion_player_out", item_wrapper_tag: :div, item_wrapper_class: "form-check" , label: 'Quem Sai?' do |player| %>
<%= player.radio_button(class: "form-check-input radio_buttons optional") %>
<%= player.label(class: "collection_radio_buttons", style: "cursor: pointer;") {content_tag(:div, player.text, class: "media align-items-center")} %>
<% end %>
I need a form that includes 1 radio button, 1 submit button and a checkbox for each listed item.
Upon submit, the form should save a separate record for each checked item. Each saved item should include the value of the radio button along with other hidden values.
The form renders but crashes upon submit. The error message is:
undefined method `permit' for #<Array:0x007fb4b3b1a520>
My code is:
<%= form_tag(controller: "handoffs", action: "create", method: "post") %>
<%= radio_button_tag(:attend, "arrive") %>
<%= label(:handoff_arrive, "drop-off") %>
<%= radio_button_tag(:attend, "depart") %>
<%= label(:handoff_depart, "pick-up") %>
<% #parent.children.each do |child| %>
<%= check_box_tag "handoff[][check]" %>
<strong>
<%= child.fname %>
<%= child.mname %>
<%= child.lname %>
</strong><br>
<% group = Group.find(child.group_id) %>
<%= hidden_field_tag "handoff[][attend]" %>
<%= hidden_field_tag "handoff[][group_name]", :value => group.name %>
<%= hidden_field_tag "handoff[][child_id]", :value => child.id %>
<%= hidden_field_tag "handoff[][center_id]", :value => #center.id %>
<%= hidden_field_tag "handoff[][escort_fname]", :value => #parent.fname %>
<%= hidden_field_tag "handoff[][escort_lname]", :value => #parent.lname %>
<%= hidden_field_tag "handoff[][child_fname]", :value => child.fname %>
<%= hidden_field_tag "handoff[][child_mname]", :value => child.mname %>
<%= hidden_field_tag "handoff[][child_lname]", :value => child.lname %>
<% end %>
<%= button_to :submit, :class => 'f_submit' %>
<% end %>
Controller actions:
def new
#handoff = Handoff.new
#parent = current_parent
#center = Center.find(#parent.center_id)
end
def create
params["handoff"].each do |handoff|
if params[:handoff["check"]] != ""
#handoff = Handoff.new(handoff_params)
#handoff.save
end
end
end
def handoff_params
params.require(:handoff).permit(:attend, :group_name, :child_id, :center_id, :escort_fname, :escort_lname, :child_fname, :child_mname, :child_lname)
end
Request parameters (in error report)
{"utf8"=>"✓",
"authenticity_token"=>"snqrS130bXNV4bmMHOMlXeyhX2rWFVpmY/PYIv0jn97MBOLSWWw2jBbeYGPyjt7O9l5pRVNuFiu1qOwkGpELTA==",
"attend"=>"depart", "handoff"=>[{"check"=>"1", "attend"=>"",
"group_name"=>"{:value=>\"Gifted\"}", "child_id"=>"{:value=>60}",
"center_id"=>"{:value=>4}", "escort_fname"=>"{:value=>\"Richard\"}",
"escort_lname"=>"{:value=>\"Messing\"}",
"child_fname"=>"{:value=>\"Aaron\"}",
"child_mname"=>"{:value=>\"Lawrence\"}",
"child_lname"=>"{:value=>\"Schwartz\"}"}, {"check"=>"1", "attend"=>"",
"group_name"=>"{:value=>\"Arts & Crafts\"}",
"child_id"=>"{:value=>61}", "center_id"=>"{:value=>4}",
"escort_fname"=>"{:value=>\"Richard\"}",
"escort_lname"=>"{:value=>\"Messing\"}",
"child_fname"=>"{:value=>\"Joseph\"}",
"child_mname"=>"{:value=>\"Michael\"}",
"child_lname"=>"{:value=>\"Messing\"}"}], "method"=>"post",
"controller"=>"handoffs", "action"=>"create"}
I had similar case. First you need to create array with all values that you would like to export I used helper to do that. I'll show you my example:
def get_table_of_keywords(keywords)
exact_keywords = []
keyword_array.each do |key|
if !key.blank?
exact_keywords << {keyword: key, created_at: DateTime.now.in_time_zone, updated_at: DateTime.now.in_time_zone }
end
end
exact_keywords
end
And the you are using create:
#inserted_keywords = #campaign.keywords.create(get_table_of_keywords(params[:keyword][:keyword])) # add new keywords to the list
I have some radio buttons in show.erb.html and i want to pass the value of the selected one to the controller using a submit button or link_to
here is show.html.erb
<ol>
<% for poll_answer in #poll_question.poll_answers %>
<li>
<%= radio_button_tag "poll", poll_answer.content ,true%>
<%= h poll_answer.content %>
</li>
<% end %>
and here is the controller poll_questions_controller.rb
def calc
p = PollAnswer.find_by_content params[:poll]
m= p.counter
if m.nil == true
p.update_attributes counter: 1
else
m = 1+m
p.update_attributes counter: m
end
end
i tried link_to
<% link_to "click here ", controller: :poll_questions, action: :calc, :poll_questions => { :poll=> calc} %>
but it didn't work
Try form_tag helper and set your path
<%= form_tag calc_path do %>
<ol>
<% for poll_answer in #poll_question.poll_answers %>
<li>
<%= radio_button_tag "poll", poll_answer.content ,true%>
<%= h poll_answer.content %>
</li>
<% end %>
</ol>
<%= submit_tag "Submit" %>
<% end %>
In your route.rb file add the following
post 'calc' => 'poll_questions#calc', as: :calc
I write a block method to print a list
def column (&block)
if block_given?
content_tag(:li, capture(self, &block))
else
content_tag(:li, "")
end
end
and using it as
<%= data_list_for #leads, [" :10", "Age:30", "Contact:140", "Phone:140", "Email:180", "Company:100", ""] do |l| %>
<%= l.column { |c| link_to " ".html_safe, "leads/details/#{c.object.id}", :class=>:plus, :remote=>true } %>
<%= l.column { |c| c.object.age } %>
<%= l.column { |c| c.object.contact.complete_name } %>
<%= l.column { |c| c.object.contact.phones.blank? ? "-" : c.object.contact.phones.first.phone_number } %>
<%= l.column { |c| c.object.contact.emails.blank? ? "-" : c.object.contact.emails.first.email } %>
<%= l.column { |c| c.object.company.title } %>
<%= l.column do |c| %>
<%= options_menu do |m| %>
<%= m.item link_to 'Show', lead_path(c.object) %>
<%= m.item link_to 'Edit', edit_lead_path(c.object) %>
<%= m.item link_to 'New Note', "leads/#{c.object.id}/notes/new", :class=>"display-newxdoc", :id=>c.object.id %>
<%= m.item link_to 'Create Opportunity', new_lead_opportunity_path(c.object) %>
<% end %>
<% end %>
<% end %>
Every thing is working fine. But the only problem is that options_menu is generating twice. Means two option_menus are there. I traced out one menu is from l.column command because it has proper formating of column, other is generating by its on block in the view, How can i stop it for doing twice?
Remove = from the line
<%= options_menu do |m| %>
I wouldn't use capture if the block is direct (not a "view block"):
content_tag(:li, block.call(self))
The solution is now posted in the discussion.