0

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

3 Answers 3

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for replying and I will try this out!
0

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

1 Comment

Thank you for replying and helping I will give this a go!
0

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

1 Comment

Thank you for answering and I will give it a try!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.