Related
So i have a search filter working perfectly in my index view. The code in the controller is as follows
def index
#tutor = Tutor.where(:admin => false)
#tutor_array = []
#tutor_array << #tutor.fees_search(params[:fees_search]) if params[:fees_search].present?
#tutor_array << #tutor.subject_search(params[:subject_search]) if params[:subject_search].present?
#tutor_array << #tutor.lssubject_search(params[:lssubject_search]) if params[:lssubject_search].present?
#tutor_array << #tutor.ussubject_search(params[:ussubject_search]) if params[:ussubject_search].present?
#tutor_array << #tutor.jcsubject_search(params[:jcsubject_search]) if params[:jcsubject_search].present?
#tutor_array.each do |tutor|
ids = #tutor.merge(tutor).map(&:id)
#tutor = Tutor.where(id: ids)
end
#tutor = #tutor.sort_by { |tutor| tutor.rating.rating }.reverse
#tutor = #tutor.paginate(:page => params[:page], :per_page => 2)
end
And in my view the form that passes in the search filters for me is
<form class='form-inline'>
<%= form_tag(tutors_path, method: :get) do %>
<div class='row', id='filter-form'>
<div class='form-group'>
<%= label_tag 'subject_search', 'Primary Subject' %>
<% subject_array = Subject.all.map { |subject| [subject.name] } %>
<%= select_tag 'subject_search', options_for_select(subject_array, :selected => params[:subject_search]), :include_blank => true, class:'form-control' %>
<%= label_tag 'lssubject_search', 'Lower Sec Subject' %>
<% lssubject_array = Lssubject.all.map { |lssubject| [lssubject.name] } %>
<%= select_tag 'lssubject_search', options_for_select(lssubject_array, :selected => params[:lssubject_search]), :include_blank => true, class:'form-control' %>
<%= label_tag 'ussubject_search', 'Upper Sec Subject' %>
<% ussubject_array = Ussubject.all.map { |ussubject| [ussubject.name] } %>
<%= select_tag 'ussubject_search', options_for_select(ussubject_array, :selected => params[:ussubject_search]), :include_blank => true, class:'form-control' %>
</div>
</div>
<div class='row', id='filter-form2'>
<div class='form-group'>
<%= label_tag 'jcsubject_search', 'JC Subject' %>
<% jcsubject_array = Jcsubject.all.map { |jcsubject| [jcsubject.name] } %>
<%= select_tag 'jcsubject_search', options_for_select(jcsubject_array, :selected => params[:jcsubject_search]), :include_blank => true, class:'form-control' %>
<%= label_tag 'fees_search', 'Max Fees' %>
<%= select_tag 'fees_search', options_for_select((10..150).step(10), :selected => params[:fees_search]), :include_blank => true, class:'form-control' %>
<%= submit_tag 'Filter', class: 'btn btn-primary btn-xs' %>
</div>
</div>
<% end %>
<div id='filter-reset'>
<%= link_to 'Reset Filters', tutors_path, class: 'btn btn-primary btn-xs' %>
</div>
</form>
And when a filter is passed in an example or the URL attained is as follows
/tutors?utf8=✓&subject_search=Science&lssubject_search=&ussubject_search=&jcsubject_search=&fees_search=&commit=Filter
What i would like to ask is, how do i pass in the filters through a button?
So if i were to click on "Math" or "English" its equivalent to passing in the filter through the form? I understand that it'll most likely be link_to tutors_path(???) but what should go into the parenthesis to pass the correct filters in?
I tried <%= link_to subs.name, tutors_path(:subject_search => subs.name %> and it seems like it works. To provide some context here's the view and how subs.name comes about.
<% tutor.subjects.each do |subs| %>
<span class='badge'id='tutor-listing-badge'>
<%= link_to subs.name, tutors_path(:subject_search => subs.name) %>
</span>
<% end %>
I have the following form which includes a hidden field tag, but when I look at the posted params, that hidden field param is not posting. Does anyone have any ideas on what could be wrong with the hidden field in this form?
<%=form_for '/schedule_path' do |f|%>
<% #total_hrs = 0 %>
<table>
<table class = "responstable" id ="form-table">
<tr>
<th>Employee</th>
<th>Job 1</th>
<th>Hrs</th>
<th>Job 2</th>
<th>Hrs</th>
<th>Job 3</th>
<th>Hrs</th>
<th>Total</th>
<th>Planned</th>
</tr>
<% #collectionsearch = "select jobs.id as id, jobs.name as name from jobs join (Select* from schedule_plans where schedule_plans." + #col + " >0 and schedule_plans.user_id =" + #userid.to_s + ") sp on sp.job_id = jobs.id order by name asc"
#collection = Hash[Job.connection.select_all(#collectionsearch).rows] %>
<% #schedule.each do |schedule| %>
<%
#hrs1 = schedule.hrs1.to_f
#hrs2 = schedule.hrs2.to_f
#hrs3 = schedule.hrs3.to_f
#search = "Select "+#col+" FROM employees where id= "+schedule.employee.id.to_s+" and user_id = "+current_user.id.to_s
#planned_hrs = Employee.connection.select_value(#search).to_f
#total_hrs = #total_hrs + #planned_hrs
%>
<% if schedule.employee.status && #planned_hrs > 0%>
<tr>
<%= f.fields_for :schedules, index: schedule.id do |sf| %>
<% #check = false %>
<td class = "large-col" > <%= schedule.employee.name %> </td>
<td> <%= if (schedule.job1.nil?)
sf.collection_select :job1, #collection, :first, :last, include_blank: 'Select a job'
else
sf.collection_select :job1, #collection, :first, :last, selected: schedule.job1
end
%>
</td>
<td class= "small-col"> <%= if (schedule.hrs1.nil?)
sf.number_field :hrs1, placeholder: "Hrs" , :step => 'any'
else
sf.number_field :hrs1, value: #hrs1 , :step => 'any'
end
%>
</td>
<td > <%= if (schedule.job2.nil?)
sf.collection_select :job2, #collection, :first, :last, include_blank: 'Select a job'
else
sf.collection_select :job2, #collection, :first, :last, selected: schedule.job2
end
%>
</td>
<td class= "small-col"> <%= if (schedule.hrs2.nil?)
sf.number_field :hrs2, placeholder: "Hrs" , :step => 'any'
else
sf.number_field :hrs2, value: #hrs2 , :step => 'any'
end
%>
</td>
<td > <%= if (schedule.job3.nil?)
sf.collection_select :job3, #collection, :first, :last, include_blank: 'Select a job'
else
sf.collection_select :job3, #collection, :first, :last, selected: schedule.job3
end
%>
</td>
<td class= "small-col"> <%= if (schedule.hrs3.nil?)
sf.number_field :hrs3, placeholder: "Hrs" , :step => 'any'
else
sf.number_field :hrs3, value: #hrs3 , :step => 'any'
end
%>
</td>
<td class = "small-col" > <%= #hrs1+ #hrs2 + #hrs3 %> </td>
<td class = "small-col" > <%= #planned_hrs %> </td>
<% end %>
</tr>
<% end %>
<%end %>
<% if #total_hrs == 0 %>
<p class = "alert-danger"> Alert -You have not defined employee availability. Please go to Labor Management > Employees and define working hours for each employee. </p>
<%end%>
</table>
<%= f.hidden_field_tag :date, #date %>
<%= f.submit "Add", class: "btn btn-primary" %>
<% end %>
Parameters:
Processing by SchedulesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OjqNrvs3JWbtbs01XA3qnYk4Mv1+OstxcWCu3xDKo7NrN9GeHTsfK+VC1CGAC2S909NiPLofOXgEO5ES/kvB1Q==", "/schedule_path"=>{"schedules"=>{"391"=>{"job1"=>"1", "hrs1"=>"7.0", "job2"=>"4", "hrs2"=>"3.0", "job3"=>"", "hrs3"=>""}, "392"=>{"job1"=>"1", "hrs1"=>"3.0", "job2"=>"1", "hrs2"=>"4", "job3"=>"", "hrs3"=>""}}}, "commit"=>"Add"}
I am using the same hidden_tag field in this other form, and it works fine:
<%= form_for(#user, url: password_reset_path(params[:id])) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= hidden_field_tag :username, #user.username %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-primary" %>
<% end %>
I thought I had tried this before, but right after submitting the question I gave it one last chance:
I changed
<%= f.hidden_field_tag :date, #date %>
<%= hidden_field_tag :date, #date %>
amd it started working.
I have this nasty if/else statement in a rails view:
<% if question.field_type == "text_area" %>
<%= f.text_area :content, :class=>"form-control question-field", :data => {:question => question.id, :filter=> #filter}, :value=> question.answer(#assessment).try(:content) %>
<% elsif question.field_type == "date" %>
<%= f.date_select :content, { :order => [:year, :month, :day], :prompt => { :day => 'day', :month => 'month', :year=> "year" }, :end_year=> Date.today.year, :start_year => Date.today.year - 2 }, {:class => "question-field", :data => {:question => question.id, :filter=> #filter}, :value=> question.answer(#assessment).try(:content)} %>
<% elsif question.field_type == "text_field" %>
<%= f.text_field :content, :class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter} %>
<% elsif question.field_type == "dropdown" %>
<%= f.select :content, options_for_select(question.options), { :prompt => "Choose One..." }, :class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter} %>
<% elsif question.field_type == "number" %>
<%= f.select :content, options_for_select(1..10), {:include_blank=> true}, :class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter} %>
<% elsif question.field_type == "percentage" %>
<h2>100%</h2>
<%= f.range_field :content, :value=> get_percentage(question), :class=> "question-field percentage", :data => {:question => question.id, :filter=> #filter}, :step => 25, :in => 0..100 %>
<% end %>
Is there a good way to refactor this to make it nicer? This piece of code is in every field:
:class=>"form-control question-field", :value=> question.answer(#assessment).try(:content), :data => {:question => question.id, :filter=> #filter}
Do I refactor into a helper method or a partial?
Sometimes templates are just messy and you can only clean up detail. Refactoring into a parameterized partial will help. For goodness sake, use a case. And consider switching to HAML. It eliminates a lot of the visual clutter.
<%= render 'question_field', f: f, type: question.field_type %>
Then in _question_field.erb,
<%= case type %>
<% when 'text_area' %>
<% f.text_area :content, class: 'form-control question-field', %>
<% data: { question: question.id, filter: #filter }, %>
<% value: question.answer(#assessment).try(:content) %>
<% when ... %>
<% end %>
Note common industrial practice is to pick a max line length and stick to it: 100 and 120 are pretty common. Also, use the new symbol key notation for hashes. The old hook-and-arrow is too noisy.
In HAML:
= case type
- when 'text_area'
- f.text_area :content, class: 'form-control question-field',
data: { question: question.id, filter: #filter },
value: question.answer(#assessment).try(:content)
- when ...
I would get rid of if's and when's altogether by creating seperate partial for every possibility, then you just end up with:
<%= render question.field_type, locals: {question: question} %>
Or to make it even cleaner for view make helper method and call only
<%= question_field(question) %>
and this method would look little bit like
def question_field(question)
return render question.field_type, locals: {question: question}
# raise when no partial found, or do something elese
end
I have a checkbox below in a view page:
<table>
<tr>
<td>
<% #user_profession.each do |p| %>
<% if !p.Designation.blank? and p.Private? == "1" %>
<%= check_box(:ChkBx_Profession, {:id => "ChkBx_Profession",:value => "Profession"},true) %>
<%= label(:lb_Profession, "Profession") %>
<% else %>
<%= check_box(:ChkBx_Profession,{:id => "ChkBx_Profession",:value => "Profession"}) %>
<%= label(:lb_Profession, "Profession") %>
<% end %>
<% end %>
</td>
</tr>
</table>
Below is my controller page
if params[:ChkBx_Profession] == "1"
#blah = params[:ChkBx_Profession]
#publicprofession = params[:ChkBx_Profession]
Profession.delete_all(:UserID => current_user.id)
if !params[:tf_designation].blank?
#desig = params[:tf_designation]
#comp = params[:tf_company]
#fromdesigcom = params[:tf_fromdesignation]
#todesigcom = params[:tf_todesignation]
#public_profession = #publicprofession
#Profession = Profession.new( :UserID => current_user.id, :Designation => #desig, :Company => #comp, :Job_From => #fromdesigcom, :Job_To => #todesigcom, :Private? => #publicprofession )
#Profession.save
end
end
and I want to get the checkbox value means if checkbox is checked it gets me 1 and if checkbox is unchecked it gets me 0 but its get like this {"{:id=>\"ChkBx_Profession\", :value=>\"Profession\"}"=>"1"}. Kindly help me waiting for your reply.
Thanks
The second attribute on an check_box is the :value attribute. Try putting empty double quotes between the chec_box name AND the options:
<%= check_box(:YourCheckName, "",{Your Options}) %>
I am trying to implement the ability for a customer to create a new card.
I have created a form in which the customer creates a new card:
<table class="table">
<%= simple_form_for(#user, :url => billing_path, html: {class: 'form-horizontal' }) do |f| %>
<tr>
<td>Country</td>
<td><%= f.text_field :saddress_country, value: card1.address_country %> </td>
</tr>
<tr>
<td>Billing Address</td>
<td><%= f.text_field :sbilling_address1, value: card1.address_line1 %> </td>
<td><%= f.text_field :sbilling_address2, value: card1.address_line2 %> </td>
</tr>
<tr>
<td>City / State / Zip</td>
<td><%= f.text_field :saddress_city, value: card1.address_city %> </td>
<td><%= f.text_field :saddress_state, value: card1.address_state %> </td>
<td><%= f.text_field :saddress_zip, value: card1.address_zip %> </td>
</tr>
<tr>
<td>Credit Card Number </td>
<td><%= f.text_field :scardnumber, placeholder: card1.last4, value: cardnumberempty %></td>
<td><%= f.text_field :scvc, placeholder: "CVC", value: cvcempty %></td>
</tr>
<tr>
<td>Card's Info</td>
<td><%= f.text_field :sname, value: card1.name %> </td>
<td><%= f.text_field :sexp_month, value: card1.exp_month %> </td>
<td><%= f.text_field :sexp_year, value: card1.exp_year %> </td>
</tr>
<tr><td><%= f.submit %></td></tr>
<% end %>
</table>
And then in the controller I grab the information and create the new card:
def create_card_stripe
#user = current_user
customer = Stripe::Customer.retrieve(current_user.stripe_customer_token)
customer.cards.create({
:card => current_user.stripe_card_token,
:number => params[:user][:scardnumber],
:exp_month => params[:user][:sexp_month],
:exp_year => params[:user][:sexp_year],
:cvc => params[:user][:scvc],
:name => params[:user][:sname],
:address_line1 => params[:user][:saddress_line1],
:address_line2 => params[:user][:saddress_line2],
:address_city => params[:user][:saddress_city],
:address_zip => params[:user][:saddress_zip],
:address_state => params[:user][:saddress_state],
:address_country => params[:user][:saddress_country]
})
#user.update_attribute(:stripe_card_id, customer.active_card.id)
if customer.save
flash.alert = "Billing information updated successfully!"
redirect_to edit_user_registration_path
else
flash.alert = "Stripe error"
redirect_to edit_user_registration_path
end
end
When I submit the form I get the following error that comes from Stripe:
"Received unknown parameters: number, exp_month, exp_year, cvc, name, address_city, address_zip, address_state, address_country".
So what it says is that I am sending wrong parameters. But if you check here those parameters are there (Child parameters).
Any clue on what I am doing wrong? Any guidance at all on how to create a new card using the Stripe API will be really helpful. Thank you.
There is two ways of adding a card using Stripe.
1) Using the card token. If you have a token, like you do, you don't need anything else, so you can just do
customer.cards.create({
:card => current_user.stripe_card_token
})
2) You could also do this:
customer.cards.create({
:card => {
:number => params[:user][:scardnumber],
:exp_month => params[:user][:sexp_month],
:exp_year => params[:user][:sexp_year],
:cvc => params[:user][:scvc],
:name => params[:user][:sname],
:address_line1 => params[:user][:saddress_line1],
:address_line2 => params[:user][:saddress_line2],
:address_city => params[:user][:saddress_city],
:address_zip => params[:user][:saddress_zip],
:address_state => params[:user][:saddress_state],
:address_country => params[:user][:saddress_country]
}
})
So as you can see here, the important thing is that the details are nested into the card attribute.
Evidently, you've done a mix of the two, you are best off choosing one (token is better) and sticking with it. Is that clear?