隐藏字段标记值

时间:2017-08-08 18:53:01

标签: ruby-on-rails ruby ruby-on-rails-4

views/plans/new.html.erb我得到了plan_id和price params,内容如下:

<%= link_to "Sign up", new_store_registration_path(:plan_id => plan.id, :price => plan.price) %>

然后,该应用重定向到注册页面,并使用方法def after_inactive_sign_up_path_for(resource)def after_sign_up_path_for(resource)保留以前的参数并合并电子邮件参数:

registrations_controller.rb

class Stores::RegistrationsController <  Devise::RegistrationsController
  before_action :configure_permitted_parameters, if: :devise_controller?

   def new
     build_resource({})
     resource.build_account
     respond_with self.resource
     session[:registration_params] = request.query_parameters
   end


   def create
     build_resource(sign_up_params)
     resource.save
     yield resource if block_given?
     if resource.persisted?
       if resource.active_for_authentication?
         flash[:notice] = 'Successfully signed up'
         respond_with resource, location:   after_sign_up_path_for(resource)
       else
         flash[:notice] = "Signed up but #{resource.inactive_message}"
     expire_data_after_sign_in!
     respond_with resource, location: after_inactive_sign_up_path_for(resource)
       end
     else
       clean_up_passwords resource
       set_minimum_password_length
       respond_with resource
     end
   end


   def edit
     super
   end


   def update
     super
   end


    def destroy
     super
    end

    def cancel
     super
    end

  protected

   def configure_permitted_parameters
     devise_parameter_sanitizer.permit(:sign_up) { |u|
      u.permit(:email, :password, :password_confirmation, :remember_me,
           :account_attributes => [:first_name, :last_name, :buisness_name,
                                   :buisness_description, :web_site, :phone_number,
                                   :street, :city, :state, :zip_code, :country])
    }
  end


  def after_sign_up_path_for(resource)
     new_transaction_path(resource,  session[:registration_params].merge(ema‌​il: resource.email))
  end

   def after_inactive_sign_up_path_for(resource)
     new_transaction_path(resource, session[:registration_params].merge(ema‌​il: resource.email))
   end
end

提交注册后,该应用会重定向到views / transcation / new.html.erb,其中包含plan_idpriceemail参数。

 Parameters: {"ema‌​il"=>"example@gmail.com", "plan_id"=>"bs96", "price"=>"150.0"}

网址显示:

http://localhost:3000/transactions/new.1?ema%E2%80%8C%E2%80%8Bil=example%40gmail.com&plan_id=bs96&price=150.0

在views / transcation / new.html.erb中,ui中有braintree drop,脚本连同三个隐藏字段:

<div class="form-container radius-box glassy-bg small-10 small-centered medium-8 large-6 columns">
  <%= form_tag transactions_path do%>
      <div id="dropin"></div>
      <%= hidden_field_tag(:email, params["email"]) %>
      <%= hidden_field_tag(:plan_id, params["plan_id"]) %>
      <%= hidden_field_tag(:amount, params["price"]) %>
      <%=submit_tag "Pay #{params["price"]}$", class: "button mt1" %>
  <%end%>
</div>

<script>
    braintree.setup("<%=@client_token%>", 'dropin', {
        container: 'dropin'
    });
</script>

此时我试图将电子邮件参数保留到交易中 <%= hidden_field_tag(:email, params["email"]) %>

但是,如果我点击提交,我没有收到电子邮件,如下所示:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"KeS2xK7NIJZwFQvW2kJKupcpURnQweq+yoRgk9AJ1aaOgFIIym4RKadI4jc6vYynMo4vKR4eLmdIynfBG+EusQ==", "email"=>"", "plan_id"=>"bs96", "amount"=>"150.0", "payment_method_nonce"=>"0c22f2fa-e212-0ad3-753b-0d183d02522b"}

如果我使用此<%= params.inspect %>检查views / transcation / new.html.erb中的参数,则打印出来:<ActionController::Parameters {"ema‌​il"=>"example@gmail.com", "plan_id"=>"bs96", "price"=>"150.0", "controller"=>"transactions", "action"=>"new"} permitted: false>

<%= params[:email].inspect %>会返回nil

为什么我无法获得电子邮件参数的任何想法?

1 个答案:

答案 0 :(得分:0)

您正在将params合并到session [:registration_params]中,而不是尝试将它们提取到查询字符串params中。您可以从事务控制器内的会话中获取这些参数,并在成功保存后删除它们。