Reputation: 31
Beginner Rails Question:
I have a hidden_field_tag
at the top of my form and I want to save the params value to the database in order to facilitate creating a subscription. I have my view, controller, and model files below. What is the best way to do this? Let me know if you need info..here to learn and thanks in advance!
View:
<%= form_for @register, html: {id: 'payment-form'} do |f| %>
<%= hidden_field_tag :plan, params[:plan] %>
<div class="form-group">
<%= f.label :name_of_person_completing_form, "Name of Person Completing Form" %>
<%= f.text_field :name_of_person_completing_form, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email, "Your Email" %>
<%= f.text_field :email, placeholder: "Ex: [email protected]", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :role_in_school, "Role in School" %>
<%= f.text_field :role_in_school, placeholder: "Ex: School Counselor, Assistant Principal", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :school_name, "School Name" %>
<%= f.text_field :school_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :grade_levels, "Grade Levels" %>
<%= f.text_field :grade_levels, placeholder: "Ex: 9-12, 6-8, 6-12", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :street_name, "Street Name" %>
<%= f.text_field :street_name, placeholder: "Ex: 123 School Drive", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :city, "City" %>
<%= f.text_field :city, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :state, "State" %>
<%= f.text_field :state, placeholder: "Ex: Virginia, Pennsylvania, California", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :zip_code, "Zip Code" %>
<%= f.text_field :zip_code, class: 'form-control' %>
</div>
<div class="form-group">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element", class = "form-control">
<!-- a Stripe Element will be inserted here. -->
</div>
<!-- Used to display Element errors -->
<div id="card-errors" role="alert"></div>
</div>
<div class="actions form-group">
<%= f.submit "Submit!", class: 'btn btn-success', id: 'form-signup-btn' %>
</div>
<% end %>
Controller:
class SchoolRegistrationsController < ApplicationController
def new
@register = SchoolRegistration.new
end
def create
@register = SchoolRegistration.new(register_params)
if @register.save_with_subscription
flash[:success] = "Congratulations! You registered your school!"
redirect_to new_user_registration_path
else
flash[:danger] = @register.errors.full_messages.join(", ")
redirect_to new_registration_path
end
end
private
def register_params
params.require(:school_registration).permit(:name_of_person_completing_form, :email, :role_in_school, :school_name, :grade_levels, :street_name, :city, :state, :zip_code)
end
end
Model:
class SchoolRegistration < ApplicationRecord
validates :name_of_person_completing_form, presence: true
validates :email, presence: true
validates :role_in_school, presence: true
validates :school_name, presence: true
validates :grade_levels, presence: true
validates :street_name, presence: true
validates :city, presence: true
validates :state, presence: true
validates :zip_code, presence: true
belongs_to :plan
attr_accessor :stripeToken
def save_with_subscription
if valid?
customer = Stripe::Customer.create(description: email, plan: plan_name, source: stripeToken)
# This will make a call to stripe server and charge their card then create subscription
self.stripe_customer_token = customer.id
save!
# This runs save on the spot and sends it to the database
end
end
end
Upvotes: 0
Views: 1361
Reputation: 3005
I would include the plan_id in permitted parameters:
def register_params
params.require(:school_registration).permit(:name_of_person_completing_form, :email, :role_in_school, :school_name, :grade_levels, :street_name, :city, :state, :zip_code, :plan_id)
end
I would add the hidden parameter as part of the form:
<%= form_for @register, html: {id: 'payment-form'} do |f| %>
<%= f.hidden_field :plan_id %>
I would set the value in the controller:
def new
# You should also validate if the plan exists in advance
@register = SchoolRegistration.new(plan_id: params[plan])
end
Upvotes: 0
Reputation: 2543
You should use hidden_field instead of hidden_field_tag
.
Using hidden_tag
you'll be able to catch plan
in params[:school_registration]
.
This one will do what you want:
<%= f.hidden_field :plan, value: params[:plan] %>
and don't forget to permit plan
Updated
You can go on with hidden_field_tag
also but in this case you have to specify the name of the field manually:
hidden_field_tag 'school_registration[plan]', params[:plan]
Upvotes: 1
Reputation: 7777
Try to the following
<%= f.hidden_field :plan, value: params[:id] %>
And update register_params
method on private
section
params.require(:school_registration).permit(:name_of_person_completing_form, :email, :role_in_school, :school_name, :grade_levels, :street_name, :city, :state, :zip_code, :plan)
added plan
Hope to help
Upvotes: 0