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 %>
Related
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 %>
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
<%= #questions.each do |question| %>
<%= select_tag "campaign_#{question['id']}",options_from_collection_for_select(current_advertiser.campaigns.all, "id", "title", selected_value || 0) %>
<% end %>
Now how I get the selected value in controller. Meanz they are dynamic then how I get params[?]
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.