Rails - 将隐藏字段标记保存到数据库

时间:2018-01-18 02:58:02

标签: ruby-on-rails ruby database hidden-field

初学者栏问题:

我的表单顶部有hidden_field_tag,我想将params值保存到数据库中,以便于创建订阅。我有我的视图,控制器和模型文件。做这个的最好方式是什么?如果您需要信息,请告诉我。提前学习和感谢!

查看:

<%= 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: schoolcounselor@school.com", 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 %>

控制器:

 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

型号:

 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 个答案:

答案 0 :(得分:1)

您应该使用hidden_field代替hidden_field_tag

使用hidden_tag,您将能够在plan中抓住params[:school_registration]

这个会做你想要的:

<%= f.hidden_field :plan, value: params[:plan] %>

并且不要忘记允许plan

<强>更新

您也可以继续hidden_field_tag,但在这种情况下,您必须手动指定字段的名称:

hidden_field_tag 'school_registration[plan]', params[:plan]

答案 1 :(得分:0)

尝试以下

<%= f.hidden_field :plan, value: params[:id] %>

并在register_params部分

上更新private方法
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)

添加plan

希望能提供帮助

答案 2 :(得分:0)

我会将plan_id包含在允许的参数中:

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

我会将隐藏参数添加为表单的一部分:

<%= form_for @register, html: {id: 'payment-form'} do |f| %>
  <%= f.hidden_field :plan_id %>

我会在控制器中设置值:

def new
  # You should also validate if the plan exists in advance
  @register = SchoolRegistration.new(plan_id: params[plan])
end