rails 4 passing a value to the controller - ruby-on-rails

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

Related

Rails: conditional image link

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 %>

Multiple saves with one submit

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

How to get dynamic select_tag params in controller

<%= #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[?]

Setting up pagination with will_paginate properly

I have been trying to setup pagination properly but I am having trouble following any tutorial and getting it to work properly.
What I basically want setup is that when a user chooses the number of companies they want to appear on a page, the following loop with paginate the given number of companies on each page. If the user doesn't choose a specific number of companies/page, there would be a default value that would be set.
If anyone could maybe give me a push in the right direction, that would be much appreciated.
View
<ul class="list-group">
<%= #comps.find_each do |comp| %>
<li class="list-group-item">
<div class="row">
<div class="span12">
<p style="font-size: 1.5em"><strong><%= link_to comp.name, comp %></strong></p>
<hr class="divider">
<div class="row">
<div class="span6">
<p><strong>Primary Field:</strong> <%= comp.primary_field %></p>
<p><strong>Description:</strong> <%= truncate(comp.description, length: 150) { link_to "Read More", comp, :target => "_blank"}%>
</p>
</div>
<div class="span5">
<% if signed_in? %>
<p><% if !(current_user.favorites.include? comp) %>
<%= link_to 'Favorite',
favorite_company_path(comp, type: "favorite"),
class: "btn btn-primary",
method: :put%>
<% else %>
<%= link_to 'Unfavorite',
favorite_company_path(comp, type: "unfavorite"),
class: "btn btn-danger",
method: :put%>
<% end %></p>
<% end %>
<p><strong>Website:</strong> <%= auto_link(comp.website, :html => { :target => '_blank' })%></p>
<p><strong>Location:</strong> <%= comp.address %></p>
</div>
</div>
</div>
</div>
</li>
<% end %>
<%= will_paginate #comps%>
</ul>
Controller (Where the list page is handled)
def filter
if ((params[:field] != "") and (params[:field] != "All Fields") and (params[:field] != nil))
# This will apply the filter
#comps = Company.where(primary_field: params[:field])
else
# This will return all records
#comps = Company.all
end
end
def list
#fields = ##primary_field
filter
#comps = #comps.paginate(:page => params[:page], :per_page => params[:number_of_companies] ||= 5)
end
Model for the list page
class DynamicPages < ActiveRecord::Base
self.per_page = 5
end
Since you are using will_paginate. You could do in this way.
Post.paginate(:page => params[:page], :per_page => params[:number_of_companies] ||= 10)
Here params[:number_of_companies] is the number of companies a user has selected and 10 is the
default page value.
class Post
self.per_page = 10
end
Also, your code snippet <%= #comps.find_each do |comp| %>, should be this <% %> instead of <%= %>

block method output is generating twice

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.

Resources