>

초보자 레일 질문 :

와이즈 비즈가있다  내 양식의 맨 위에 구독을 쉽게 만들 수 있도록 params 값을 데이터베이스에 저장하고 싶습니다. 아래에 내보기, 컨트롤러 및 모델 파일이 있습니다. 가장 좋은 방법은 무엇입니까? info .. 여기에서 배우고 감사의 말이 필요하면 알려주십시오!

보기 :

hidden_field_tag

컨트롤러 :

<%= 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 %>

모델 :

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
  • 답변 # 1

    hidden_field_tag 대신 hidden_field를 사용해야합니다 .

    hidden_tag 사용  당신은 plan 를 잡을 수 있습니다  와이즈 비즈 .

    이것은 당신이 원하는 것을 할 것입니다 :

    params[:school_registration]
    
    

    <%= f.hidden_field :plan, value: params[:plan] %> 를 허용하는 것을 잊지 마세요

    업데이트

    plan 로 갈 수 있습니다  또한이 경우 필드 이름을 수동으로 지정해야합니다.

    hidden_field_tag
    
    

  • 답변 # 2

    다음을 시도하십시오

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

    <%= 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) 추가

    도움이 필요합니다

  • 답변 # 3

    plan_id를 허용 된 매개 변수에 포함 시키려고합니다 :

    plan
    
    

    숨겨진 매개 변수를 양식의 일부로 추가합니다 :

    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

관련 자료

  • 이전 asp.net mvc - 전체 모델을 보내지 않고 부분 뷰와 올바르게 바인딩하는 방법
  • 다음 javascript - jQueryvalidate ()의 요소에 이미 "활성"클래스가 있는지 확인하십시오