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?
Related
Here's a snippet of a code that is throwing an error:
<% count = 1 %>
<% exp = Expensestitle.where("expense_title_type_id = ?", 1) %>
<%= f.fields_for :aktiyary_items do |builder| %>
<tr>
<td style="width:5%;">
<label><%= exp[count].name %></label>
<%= builder.hidden_field :expensestitle_id, :class => 'form-control', :value => exp[count].id %>
</td>
<td style="width:1%">
<%= builder.number_field :initial_amount, :class => 'form-control inputs', :value => 0.0, :autocomplete => 'off', :precision => 2, :step => 'any' %>
</td>
</tr>
<% count += 1 %>
<% end %>
I'm trying to display all the 'names' from a table (expensestitle) whose expense_title_type_id = 1.
What I get is the following error:
undefined method `name' for nil:NilClass
But if I replace the second line of code from above with:
<% exp = Expensestitle.all %>
it displays all the 'name' from expense_title_type_id of 1 and 2. I just want to limit the name to an id of 1.
Any help would be appreciated.
Thanks
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 two models Employee and Overtime Definition The Associations are set like this
Employee
class Employee < ActiveRecord::Base
has_many :overtime_definitions
Overtime Definition
class OvertimeDefinition < ActiveRecord::Base
belongs_to :employee
I created an Overtime definition for an employee and it all looks fine.However I'm having trouble with editing the same for an employee.
overtime_definitions__controller:
def new
#flag = params[:flag]
#employee = Employee.find(params[:id])
#overtime = OvertimeDefinition.new
end
def create
#employee = Employee.find(params[:overtime_definition][:employee_id])
#overtime = OvertimeDefinition.new(params[:overtime_definition])
if (params[:half_day_extra_duty_hours][:hour].to_s !="" || params[:half_day_extra_duty_hours][:minute].to_s !="")
#overtime.half_day_extra_duty_hours = params[:half_day_extra_duty_hours][:hour].to_s + ":" + params[:half_day_extra_duty_hours][:minute].to_s + ":" + "00"
else
#overtime.half_day_extra_duty_hours = nil
end
if (params[:full_day_extra_duty_hours][:hour].to_s !="" || params[:full_day_extra_duty_hours][:minute].to_s !="")
#overtime.full_day_extra_duty_hours = params[:full_day_extra_duty_hours][:hour].to_s + ":" + params[:full_day_extra_duty_hours][:minute].to_s + ":" + "00"
else
#overtime.full_day_extra_duty_hours = nil
end
if #overtime.save
flash[:notice] = "Overtime Successfully Created for #{#employee.name}"
redirect_to :action => 'search_overtime'
end
end
def edit
#flag = params[:flag]
#overtime = OvertimeDefinition.find(params[:id][:employee_id])
#employee = Employee.find(params[:id])
end
def update
#employee = Employee.find(params[:id])
#overtime = OvertimeDefinition.find(params[:id])
if #overtime.update_attributes(params[:overtime_definition])
flash[:notice] = "Overtime Successfully Updated for #{#employee.name}"
redirect_to :action => 'search_overtime'
else
render :action => 'edit',:flag=>params[:flag]
end
end
Tried with these in the edit method
#overtime = OvertimeDefinition.find(params[:id][:employee_id])
#gives me can't convert Symbol into Integer error.
#overtime = OvertimeDefinition.find(params[:id])
#gives me Couldn't find OvertimeDefinition with ID=1353 error.Actually 1353 is the id of that employee.
3.#overtime = OvertimeDefinition.find(params[:employee_id])
#gives me couldn't find OvertimeDefinition without an ID error.
My _search_overtime_employee_list having these links for new and edit actions
<%=link_to "Calculation" ,:action => "new",:id=>employee.id, :flag=>"Calculation" %>
<%= link_to "Re-Calculate",:action => "edit",:id=>employee.id,:flag=>"Re-Calculate" %>
new.rhtml
<%= form_tag :action => 'create' do %>
<%= render :partial =>'form'%>
<center>
<%= submit_tag "Save",:onclick=>"return validate()",:class=>"buttons"%>
</center>
<% end %>
<%= link_to "Back" ,:action => "search_overtime" %>
edit.rhtml
<%= form_tag :action => 'update', :id=>#employee.id,:flag=> params[:flag],:value=>params[:id] %>
<%= render :partial =>'form'%>
<center>
<%= submit_tag "Update",:onclick=>"return validate()",:class=>"buttons"%>
</center>
<%= link_to "Back" ,:action => "search_overtime" %>
_form.rhtml
Employee Details
<table cellspacing="5">
<tr>
<td><b>Employee Code</b></td>
<%= hidden_field 'overtime_definition','employee_id',:value=>params[:id] %>
<td><%= #employee.employeeid %></td>
<td><b>Employee Name</b></td>
<td><%= #employee.personnel.display_full_name %></td>
</tr>
<tr>
<td><b>Department</b></td>
<td><%= #employee.department ? #employee.department.name : "" %></td>
<td><b>Designation</b></td>
<td><%= #employee.designation ? #employee.designation.name : "" %></td>
<td><b>Location</b></td>
<td><%= #employee.location.name%></td>
</tr>
</table>
</br>
<fieldset>
<table cellspacing="5">
<%= form_for :overtime_definition, :builder => LabelFormBuilder do |od| %>
<tr>
<td>
<label for="half_day_extra_duty_hours">
Half Day Extra Duty Hours
</label>
</td>
<td class ="datefamily">
<%= select_time(#overtime.half_day_extra_duty_hours, {:include_blank => true, :time_separator => ":",:prefix => "half_day_extra_duty_hours"})%>
</td>
</tr>
<tr>
<td>
<label for="full_day_extra_duty_hours">
Full Day Extra Duty Hours
</label>
</td>
<td class ="datefamily">
<%= select_time(#overtime.full_day_extra_duty_hours, {:include_blank => true, :time_separator => ":",:prefix => "full_day_extra_duty_hours"})%>
</td>
</tr>
<tr>
<%= od.sr_check_box :is_salary_basis, {}, true, false, :label => "Is Salary Basis"%>
</tr>
<tr>
<%= od.sr_check_box :is_fixed_amount, {}, true, false, :label => "Is Fixed Amount"%>
<td colspan="2" id="ov_hm" style="display: none">
Half Day Amount
<%= od.text_field :half_day_amount, :onkeypress => "return numbersonly(event)", :style => "width:40px" %>
</td>
<td colspan="2" id="ov_fm" style="display: none">
Full Day Amount
<%= od.text_field :full_day_amount, :onkeypress => "return numbersonly(event)", :style => "width:40px" %>
</td>
</tr>
<%end%>
</table>
I just lost out here completely in getting that edit action work.Any help is greatly appreciated!
Your current edit link is:
<%= link_to "Re-Calculate",:action => "edit",:id=>employee.id,:flag=>"Re-Calculate" %>
In your edit action:
#overtime = OvertimeDefinition.find(params[:id][:employee_id]) ## gives me can't convert Symbol into Integer error.
As per the edit link, you are directly passing :id in query params which you can access as params[:id]. There is no params[:id][:employee_id] in your params hash so when you say params[:id][:employee_id] Ruby tries to convert :employee_id to an integer which is a symbol. Hence, the error.
I think you should be passing id of OvertimeDefinition record in :id from your link. And access it as
#overtime = OvertimeDefinition.find(params[:id])
in the Controller's action.
#overtime = OvertimeDefinition.find(params[:id]) ## gives me Couldn't
find OvertimeDefinition with ID=1353 error.Actually 1353 is the id of
that employee.
This is because you are passing employee id in params[:id] so obviously this will not work. You need to pass OvertimeDefinition id here.
#overtime = OvertimeDefinition.find(params[:employee_id]) ## gives me
couldn't find OvertimeDefinition without an ID error.
You are not passing any :employee_id in query params within edit link. So, params[:employee_id] will be nil and find method fails because you didn't pass any id to it.
Solution :
Update your edit link as below:
<%= link_to "Re-Calculate",:action => "edit",:id=> #overtimedefinition.id , :employee_id => employee.id,:flag=>"Re-Calculate" %>
Replace #overtimedefinition.id with appropriate id of OvertimeDefinition record. As you have not shared the code, I don't know the name of OvertimeDefinition variable.
Update your edit action as:
def edit
#flag = params[:flag]
#overtime = OvertimeDefinition.find(params[:id])
#employee = Employee.find(params[:employee_id])
end
At this time I need to save n cuantity of registers, it's depending of the cuantity of "preguntas"
This is the structure of the table
respuestaspolls
---------------
id
poll
subpoll
descripcion
respuesta
I'm loading the fields this way
<% #subpolls.each do |s| %>
<tr>
<thead>
<th>
<%= f.input :subpoll,
:label => false,
:error => false,
:input_html => {
:name => "subpoll[]",
:class =>'form-control input-sm',
:value => "#{s.name}",
:readonly => true,
:style => "
border:hidden;
font-weight:bold;"} %>
</th>
</thead>
<td>
<table class="table table-striped table-condensed table-bordered sort_table table-hover" id="sort_table">
<% Pregunta.where("subpoll_id = ?", s.id).each.with_index do |i, index| %>
<tr>
<td>
<%= f.input :descripcion,
:label => false,
:error => false,
:input_html => {
:name => "pregunta[]",
:class =>'form-control input-sm',
:value => "#{i.descripcion}",
:readonly => true,
:style => "border:hidden;
background-color:white;"} %>
</td>
<td width="15%">
<%= f.input :respuesta,
:collection => Respuesta.where("pregunta_id = ?", i.id),
:label => false,
:error => false,
:input_html => {
:class =>'form-control input-sm',
:name => "respuesta[]"} %>
</td>
</tr>
<% end %>
</table>
</td>
</tr>
<% end %>
as you can see I need to save the information of diferents fields called with the same name, so in each field I put that
subpoll[] and pregunta[] and respuesta[]
so I dont know how to get each content of each field from the controller to save the information, can somebody help me please
thanks
I have a parent - child relationship between Repairs & RepairItems.
A repair must have a least 1 RepairItem to be saved. I've made a nested (simple) form to create a Repair and display 3 blank repair_items.
I'm trying to work out how to make sure that a a Repair has at least 1 repair_item entered to allow the user to save the Repair. Otherwise I need to prompt the user that the repair can't be saved until at least 1 repair_item is entered..
Can anyone point me in the right direction for validation so that a user can't save a Repair without any Repair items entered? Thanks
class Repair < ActiveRecord::Base
attr_accessible :repair_id, :repairer_id, :fault_num, :vehicle_id, :date_reported, :date_closed, :hours_open, :mileage_open, :reported_to, :reported_by,
:repair_items_attributes
belongs_to :vehicle
belongs_to :repairer
has_many :repair_items, :dependent => :destroy
validates_presence_of :vehicle_id
validates_associated :repair_items
accepts_nested_attributes_for :repair_items, :reject_if => lambda { |a| a[:repair_type_id].blank? }, :allow_destroy => true
end
class RepairItem < ActiveRecord::Base
attr_accessible :repair_id, :problem, :solution, :repair_type_id, :priority, :repairer_id, :invoice, :cost, :tax,
:item_state_id, :mileage_closed, :hours_closed, :date_closed
belongs_to :repair
belongs_to :repairer
belongs_to :repair_type
belongs_to :item_state
#validates_presence_of :repair_id
validates_presence_of :repair_type_id
scope :open, where(:item_state_id => 1)
scope :monitor, where(:item_state_id=> 2)
scope :deferred, where(:item_state_id => 3)
scope :closed, where(:item_state_id => 4)
scope :cancelled, where(:item_state_id => 5)
end
class RepairsController < ApplicationController
before_filter :authorise
layout :resolve_layout
def index
#status = 1
#repairItems = RepairItem.open
end
def monitor
#status = 2
#repairItems = RepairItem.monitor
end
def deferred
#status = 3
#repairItems = RepairItem.deferred
end
def closed
#status = 4
#repairItems = RepairItem.closed
end
def cancelled
#status = 5
#repairItems = RepairItem.cancelled
end
def new
#repair = Repair.new
3.times { #repair.repair_items.build }
end
def create
# Instantiate a new object using form parameters
#repair = Repair.new(params[:repair])
# Save the object
if #repair.save
# If the save suceeds, redirect to the list action
redirect_to(repairs_path, :notice => 'Repair Created.')
else
# If the save fails, redisplay the form so user can fix problems
render :action => :new
end
end
<%= simple_form_for( #repair, :defaults => { :disabled => #current_user.read_only, :input_html => { :class => "span10" } }) do |f| %>
<fieldset>
<!-- This will display some text in red at the top of the form telling the user -->
<%= f.error_notification %>
<div class="st-row-fluid">
<div class="span2">
<%= f.association :vehicle, label_method: :fleet_num, value_method: :id, include_blank: true, label: 'Vehicle'%>
<p>Current Kms</p>
<p>Current Hours</p>
<p class="muted">Warranty Expires</p>
<p class="muted">Contract Maintenance</p>
</div>
<div class="span2">
<%= f.input :date_reported, :as => :date_picker, :input_html => { :class => "span10 st-datepicker"} %>
<%= f.input :mileage_open, :label => "Km/Miles" %>
<%= f.input :hours_open %>
</div>
<div class="span2">
<%= f.input :fault_num %>
<%= f.input :reported_to %>
<%= f.input :reported_by %>
</div>
</div>
<div class="row-fluid">
<h4> Items</h4>
<%= f.simple_fields_for :repair_items do |p| %>
<table class="table table-condensed">
<tr>
<%= render "repair_items", :p => p %>
</tr>
</table>
<% end %>
</div>
<%= f.error :base %>
<div class="st-form-actions">
<% if #current_user.read_only == false %>
<%= f.submit nil, :class => 'btn btn-success pull-right' %>
<% end %>
<%= link_to 'Cancel', repairs_path, :class => 'btn btn-danger pull-right' %>
</div>
</fieldset>
<% end %>
partial
<td><%= p.association :repair_type, label_method: :repair_type_label, value_method: :id, include_blank: true, label: 'Repair Type'%></td>
<td><%= p.input :problem %></td>
<td><%= p.input :solution %></td>
<td><%= p.input :priority %></td>
<td><%= p.association :repairer, label_method: :rep_name, value_method: :id, include_blank: true, label: 'Repairer'%></td>
</tr>
<tr>
<td><%= p.input :invoice %></td>
<td><%= p.input :cost %></td>
<td><%= p.input :tax %></td>
<td><%= p.input :date_closed, :as => :date_picker, :input_html => { :class => "span10 st-datepicker"} %></td>
<td><%= p.input :mileage_closed, :label => "Km/Miles" %></td>
<td><%= p.input :hours_closed %></td>
<td><%= p.association :item_state, label_method: :state_label, value_method: :id, label: 'Status', :default => 1 %></td>
Use validates_associated :repair_items in the repair to validate the repair items. The action has to construct the repair items using repair.RepairItem.build, so that the items will be associated with the repair, before attempting to save the repair. The repair is constructed before the repair items, but the repair_items will block the associated repair from saving.