Stripe different plans - ruby-on-rails

So I got stripe up and running, however I have 3 different payment plans.
I'd like to capture what plan they clicked on, and use that to connect them to their right plan id.
This is what my form looks like:
<%= form_tag charges_path, id: "payment-form" do %>
<span class="payment-errors"></span>
<div class="form-row">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number" maxlength="20" />
</label>
</div>
<div class="form-row">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc" maxlength="4"/>
</label>
</div>
<div class="form-row">
<label>
<span>Expiration (MM/YYYY)</span>
<input type="text" size="2" data-stripe="exp-month" maxlength="2"/>
</label>
<span> / </span>
<input type="text" size="4" data-stripe="exp-year" maxlength="4"/>
</div>
<button type="submit">Submit Payment</button>
<% end %>
I link to them like this:
<%= link_to 'Start Your Free Trial', new_charge_path(:plan_id => 1) %>
and the controller looks like this:
def create
customer = Stripe::Customer.create(
:email => 'exaimple#stripe.com',
:plan => plan_id,
:card => params[:stripeToken]
)
current_user.update_attributes(:stripe_customer_token => customer.id)
redirect_to root_url, notice: 'Successfully subscribed'
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
which results in the error:
undefined local variable or method `plan_id'

Related

Calling method in rails

In my application I have an form to create new company. Here I have to enter company name and company url.Here is my code for the from.
<%= form_tag(controller: "/company", action: "add_startup_to_index", method: "post") do %>
<div class="modal-body">
<div class="form-group">
<label class="control-label">Company Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter the name of the company..." required />
</div>
<div class="form-group">
<label class="control-label">Company URL</label>
<input type="text" class="form-control" id="url" name="url" placeholder="e.g. http://www.company.com..." required />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-conf" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary btn-conf">Add Company</button>
</div>
<% end %>
This is my method for saving above data.
def add_startup_to_index
#url = api_version_root + 'startups/new'
response = RestClient.post #url,
{ startup: { friendly_name: params[:name],
url: params[:url]
}
}, api_token_hash
record = JSON.parse(response.body)
flash[:info] = 'Startup has been added and Crunchbase sync started.'
redirect_to('/startups/' + record['company_id']) && return
rescue RestClient::ExceptionWithResponse => err
handle_rest_error http_code: err.http_code
end
This is working fine and I can save the companies. Now I want to validate the URL. For that I have below method.
def valid_url?(url)
return false if url.include?("<script")
url_regexp = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
url =~ url_regexp ? true : false
end
Since I am new for rails I have no idea how to call that method within my form. I had tried nested form_tag. But it is now allowed.
I tried as below.
<%= form_tag(controller: "/company", action: "add_startup_to_index", method: "post") do %>
<div class="modal-body">
<div class="form-group">
<label class="control-label">Company Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter the name of the company..." required />
</div>
<%= form_tag(controller: "/company", action: "valid_url", method: "post") do %>
<div class="form-group">
<label class="control-label">Company URL</label>
<input type="text" class="form-control" id="url" name="url" placeholder="e.g. http://www.company.com..." required />
</div>
<% end %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-conf" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary btn-conf">Add Company</button>
</div>
<% end %>
Can any one help me for this.
Lanka, if I understand you correctly, what you are looking for is client side form validation. Before HTML5 this was a tedious task involving JS. With HTML5, url validation is baked right in. Simply try to change the type of the input to url, i.e. change
<input type="text" id="url" name="url" ...>
to
<input type="url" id="url" name="url" ...>
Then, when a non-conforming url string is entered, the browser will not submit the form and automatically indicate the issue. This even works out of the box with a default url pattern. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/url for more information on custom patterns, placeholders and much more.

how to update user in ruby on rails

I am trying to update user info and i am having error that it didn't get any data from my form
Here is my user.controller:
def update
if !session[:id]
redirect_to '/users'
else
#user = User.find(params[:id])
#user.update(update_params)
if #user.valid?
#user.save
redirect_to '/events'
else
flash[:errors] = #user.errors.full_messages
redirect_to "/users/#{#user.id}/edit"
end
end
end
def update_params
params.require(:user).permit(:fname, :lname, :email, :city, :state)
end
edit update form
here is my edit.html.erb form:
<form action="/users/<%= #user.id %>/update" method="post">
<input type="hidden" name="_method" value="patch">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="form-group row">
<label for="fname" class="col-sm-2 col-form-label">First Name</label>
<div class="col-sm-10">
<input type="text" name="user[fname]" class="form-control" placeholder="<%= #user.fname %>">
</div>
</div>
<div class="form-group row">
<label for="lname" class="col-sm-2 col-form-label">Last Name</label>
<div class="col-sm-10">
<input type="text" name="user[lname]" class="form-control" placeholder="<%= #user.lname %>">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" name="user[email]" class="form-control" placeholder="<%= #user.email %>">
</div>
</div>
<div class="form-group row">
<label for="location" class="col-sm-2 col-form-label">Location</label>
<div class="col-sm-5">
<input type="text" name="user[city]" class="form-control" placeholder="<%= #user.city %>">
</div>
<div class="col-sm-5">
<select class="custom-select" name="user[state]">
<%= options_for_select(us_states)%>
</select>
</div>
</div>
<div class="form-group row clearfix">
<div class="col-sm-10 offset-sm-2">
<button type="submit" class="btn btn-primary float-right">Update</button>
</div>
</div>
</form>
Just updated my html form
this is error in terminal when i hit submit from my form:
(0.1ms) begin transaction User Exists (1.7ms) SELECT 1 AS one
FROM "users" WHERE ("users"."email" = 'nhan13574#gmail.com' AND
"users"."id" != 1) LIMIT 1 (0.1ms) rollback transaction Redirected
to http://localhost:4000/users/1/edit
Can you share the form? mostly the issue is there.
Also, #user.update actually saves the data in db, no need to save after it.. here is a refactored version of your snippet:
def update
return redirect_to '/users' unless session[:id]
#user = User.find(params[:id])
if #user.update(update_params)
redirect_to '/events'
else
flash[:errors] = #user.errors.full_messages
redirect_to "/users/#{#user.id}/edit"
end
end

rails deeply nested form_for not able to save twice

I have a form where teams can register for a tournament. The form saves & routes as expected when I first save the form, however if I return to the root_path & repeat the process of registering a team, the form doesn't save at all (clicking on the "Submit" button does nothing).
My feeling is that something is hanging around from the original save that is stopping a new one from happening, but I'm not able to put my finger on what it is. Code is below, hope it is sufficient.
(routes.rb)
root 'tournaments#index'
resources :tournaments do
resources :teams do
resources :players, :only => [:new, :create]
end
end
(models)
class Tournament < ApplicationRecord
has_many :teams
has_many :players, :through => :teams
accepts_nested_attributes_for :teams
accepts_nested_attributes_for :players
end
class Team < ApplicationRecord
belongs_to :tournament, required: false
has_many :players
accepts_nested_attributes_for :players
end
class Player < ApplicationRecord
belongs_to :team, required: false
has_one :tournament, :through => :team
end
(tournaments_controller.rb)
def index
#tournaments = Tournament.all
end
def show
#tournament = Tournament.find(params[:id])
end
def new
#tournament = Tournament.new
end
def create
#tournament = Tournament.new(tournament_params)
if #tournament.save
flash[:notice] = "#{#tournament.name} saved."
redirect_to root_path
else
render :new
end
end
private
def tournament_params
params.require(:tournament).permit(:name, :deadline, :divisions, :info, :payment, :tournament_date, team_attributes: [:name, :division, :contact_email, :contact_name])
end
(teams_controller.rb)
def create
#team = Team.new(team_params)
if #team.save
flash[:notice] = "Team Registered."
redirect_to tournament_team_path(params[:tournament_id], #team.id)
else
redirect_to new_tournament_team_path(params[:tournament_id])
end
end
def new
#tournament = Tournament.find_by_id(params[:tournament_id])
#team = Team.new
8.times do
#team.players.build
end
end
def show
#tournament = Tournament.find(params[:tournament_id])
#team = Team.find(params[:id])
end
private
def team_params
params.require(:team).permit(:name, :tournament_id, :division, :contact_name, :contact_email, players_attributes: [ :name, :gender, :referee ])
end
Entry link from tournaments#show page to the teams#new action (this works ... as far as that it links to the correct page)
<%= link_to(new_tournament_team_path(params[:id])) do %><div class="rounded_btn"><h3>Register Team</h3></div><% end %>
(teams/new.html.erb) - this is the form that doesn't work. I should use partials, but I'd like to get it working to start.
<main class="long_page">
<h1 class="tournament_name"><%= #tournament.name %></h1>
<h3 class="tournament_name">Team Registration</h3>
<section style="align-items: baseline;">
<div class="rounded_box">
<%= form_for [#tournament, #team] do |f| %>
<%= f.hidden_field :tournament_id, :value => #tournament.id %>
<div>
<p>
<%= f.label :division, "Division: " %>
<%= f.select :division, options_for_select([["Mixed", "mixed"], ["Mens", "mens"]]) %>
</p>
<p>
<%= f.text_field :name, required: '' %>
<label alt='Team Name' placeholder='Team Name'></label>
</p>
<p>
<%= f.text_field :contact_name, required: '' %>
<label alt='Contact Person' placeholder='Contact Person'></label>
</p>
<p>
<%= f.text_field :contact_email, required: '' %>
<label alt='Contact Email' placeholder='Contact Email'></label>
</p>
<div class="clear"></div>
</div>
<%= f.fields_for :players do |builder| %>
<div>
<%= builder.text_field :name, required: '' %>
<%= builder.select :gender, options_for_select([["Male", "male"], ["Female", "female"]]) %>
<%= builder.select :referee, options_for_select([["No", "no"], ["Yes", "yes"]]) %>
</div>
<% end %>
</div>
<div class="btn_holder">
<p>
<%= f.submit 'Submit Team', :class => 'rounded_btn' %>
</p></div>
<% end %>
</div>
</section>
This generates the following HTML output:
<body>
<container>
<header class="header" id="header">
<img alt="Taipei Touch Association Logo" src="/assets/taipei_touch_logo_faded-4bbdd185e462f4d8af3b2d25f221f27f4ae479c8adfa4701b8c3f03e9e31b36c.svg">
<span class="header_span">
<h4>Taipei Touch Association</h4>
<h3>Tournament Management System</h3>
</span>
</header>
<main class="long_page">
<h1 class="tournament_name">Dummy Tournament</h1>
<h3 class="tournament_name">Team Registration</h3>
<section style="align-items: baseline;">
<div class="rounded_box">
<form class="new_team" id="new_team" action="/tournaments/1/teams" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="+CtsL2QcMjj1703iGPpwgs8vYu7TWJfWt9jTUgCYTMaFE9JdvYIMJqdb87z4vKuDzUgb8RO96Mdvk5fW/uJUSw==">
<input value="1" type="hidden" name="team[tournament_id]" id="team_tournament_id">
<div>
<p>
<label for="team_division">Division: </label>
<select name="team[division]" id="team_division"><option value="mixed">Mixed</option>
<option value="mens">Mens</option></select>
</p>
<p>
<input required="required" type="text" name="team[name]" id="team_name">
<label alt="Team Name" placeholder="Team Name"></label>
</p>
<p>
<input required="required" type="text" name="team[contact_name]" id="team_contact_name">
<label alt="Contact Person" placeholder="Contact Person"></label>
</p>
<p>
<input required="required" type="text" name="team[contact_email]" id="team_contact_email">
<label alt="Contact Email" placeholder="Contact Email"></label>
</p>
<div class="clear"></div>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][0][name]" id="team_players_attributes_0_name">
<select name="team[players_attributes][0][gender]" id="team_players_attributes_0_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][0][referee]" id="team_players_attributes_0_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][1][name]" id="team_players_attributes_1_name">
<select name="team[players_attributes][1][gender]" id="team_players_attributes_1_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][1][referee]" id="team_players_attributes_1_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][2][name]" id="team_players_attributes_2_name">
<select name="team[players_attributes][2][gender]" id="team_players_attributes_2_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][2][referee]" id="team_players_attributes_2_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][3][name]" id="team_players_attributes_3_name">
<select name="team[players_attributes][3][gender]" id="team_players_attributes_3_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][3][referee]" id="team_players_attributes_3_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][4][name]" id="team_players_attributes_4_name">
<select name="team[players_attributes][4][gender]" id="team_players_attributes_4_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][4][referee]" id="team_players_attributes_4_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][5][name]" id="team_players_attributes_5_name">
<select name="team[players_attributes][5][gender]" id="team_players_attributes_5_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][5][referee]" id="team_players_attributes_5_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][6][name]" id="team_players_attributes_6_name">
<select name="team[players_attributes][6][gender]" id="team_players_attributes_6_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][6][referee]" id="team_players_attributes_6_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
<div>
<input required="required" type="text" name="team[players_attributes][7][name]" id="team_players_attributes_7_name">
<select name="team[players_attributes][7][gender]" id="team_players_attributes_7_gender"><option value="male">Male</option>
<option value="female">Female</option></select>
<select name="team[players_attributes][7][referee]" id="team_players_attributes_7_referee"><option value="no">No</option>
<option value="yes">Yes</option></select>
</div>
</form></div>
<div class="btn_holder">
<p>
<input type="submit" name="commit" value="Submit Team" class="rounded_btn" data-disable-with="Submit Team">
</p></div>
</section>
</main>
</container>
</body>
Note: This problem persists even when using the nested_forms gem.
I deleted <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> from my /views/application.erb.html file & now the funtion seems to work.

How to add custom fields to stripe?

I'm using stripe.js, got this:
<%= form_tag charges_path, id: 'payment-form' do %>
<span class="payment-errors"></span>
<div class="form-group">
<label>
<span>Your email</span>
<input type="text" size="20" data-stripe="email">
</label>
</div>
<div class="form-group">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number">
</label>
</div>
<div class="form-group">
<label>
<span>Expiration (MM/YY)</span>
<input type="text" size="2" data-stripe="exp_month">
</label>
<span> / </span>
<input type="text" size="2" data-stripe="exp_year">
</div>
<div class="form-group">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc">
</label>
</div>
<input type="submit" class="submit" value="Submit Payment">
<% end %>
How do I add new fields to submit to stripe? such as name, address, etc. I think it has something to do with changing data-stripe but I couldn't find a "list" of the values that stripe accepts. Any ideas?
As per Stripe docs Updatable Stripe objects (Account, Charge, Customer, Refund, Subscription, and Transfer) have a metadata parameter. You can use this parameter to attach key-value data to these Stripe objects.
You can check HERE for the list of attributes of the Charge object.
In your question you refer to some attributes which can be better related to the user (Customer) rather than the Charge itself.
You can check HERE for the list of attributes of the Customer object.
You can use something like the following in your controller:
customer = Stripe::Customer.create(
:email => params[:email],
:source => params[:stripeToken],
:metadata => {
:address => params[:address],
:city => params[:city],
:region => params[:region],
:country => params[:country]
}
)

Nested form accepts_nested_attributes_for giving dynamic Array - Rails4

I am working on Rails4 with Nested form, accepts_nested_attributes_for i can able to generate the nested form but its giving dynamic array the the form when i inspect the form.
<input type="text" name="event_venue[event_contact_details_attributes][1403763304978][name]" id="event_venue_event_contact_details_attributes_1403763304978_name" class="form-control">
But it should be,
<input type="text" name="event_venue[event_contact_details_attributes][1][name]" id="event_venue_event_contact_details_attributes_1_name" class="form-control">
<div class="formWrapper">
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" name="event_venue[event_contact_details_attributes][1403764358820][name]" id="event_venue_event_contact_details_attributes_1403764358820_name" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone</label>
<input type="text" name="event_venue[event_contact_details_attributes][1403764358820][telephone]" id="event_venue_event_contact_details_attributes_1403764358820_telephone" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="text" name="event_venue[event_contact_details_attributes][1403764358820][email]" id="event_venue_event_contact_details_attributes_1403764358820_email" class="form-control">
</div>
<div class="clearfix"></div>
<input type="hidden" value="false" name="event_venue[event_contact_details_attributes][1403764358820][_destroy]" id="event_venue_event_contact_details_attributes_1403764358820__destroy"><a onclick="remove_fields(this); return false;" href="#">remove</a>
</div>
</div>
Can you help me out where i am missing the things...!!!
In first nested form set it's coming right,
<div class="formWrapper">
<div class="col-md-6">
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" value="AS" name="event_venue[event_contact_details_attributes][0][name]" id="event_venue_event_contact_details_attributes_0_name" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone</label>
<input type="text" value="AS" name="event_venue[event_contact_details_attributes][0][telephone]" id="event_venue_event_contact_details_attributes_0_telephone" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="text" value="AS" name="event_venue[event_contact_details_attributes][0][email]" id="event_venue_event_contact_details_attributes_0_email" class="form-control">
</div>
<div class="clearfix"></div>
<input type="hidden" value="false" name="event_venue[event_contact_details_attributes][0][_destroy]" id="event_venue_event_contact_details_attributes_0__destroy"><a onclick="remove_fields(this); return false;" href="#">remove</a>
</div>
</div>
Event Venue Model,
has_many :event_contact_details, :dependent => :destroy
accepts_nested_attributes_for :event_contact_details, allow_destroy: true
Controller,
def new
#event_venue = EventVenue.new
#event_venue.event_contact_details.build
end
form,
<%= f.fields_for :event_contact_details do |builder| %>
<%= render :partial => 'event_venues/event_contact_detail_fields',
:locals => { :f => builder } %>
<% end %>
<p><%= link_to_add_fields "Add More", f, :event_contact_details %></p>

Resources