从设计注册控制器

时间:2017-08-08 07:07:05

标签: ruby-on-rails ruby devise

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

点击链接注册后

   Started GET "/stores/sign_up?plan_id=bs96&price=150.0" for 127.0.0.1 at 2017-08-10 13:46:23 +0300
Processing by Stores::RegistrationsController#new as HTML
  Parameters: {"plan_id"=>"bs96", "price"=>"150.0"}
  Rendering stores/registrations/new.html.erb within layouts/application
  Rendered stores/shared/_links.html.erb (1.2ms)
  Rendered stores/registrations/new.html.erb within layouts/application (14.8ms)
Completed 200 OK in 110ms (Views: 46.9ms | ActiveRecord: 1.9ms)

提交注册后:

  Started POST "/stores" for 127.0.0.1 at 2017-08-10 13:47:07 +0300
Processing by Stores::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"A65CebXfvIw14X7tnOYrrtjfbB3uZzMtcbmd2IFc91yKt8KSyKToJt2kIl3xyV6AK9HVfQrXFpLG9RPtrvUOrw==", "store"=>{"email"=>"example@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "account_attributes"=>{"first_name"=>"david", "last_name"=>"davisp", "buisness_name"=>"1", "buisness_description"=>"1", "web_site"=>"1", "phone_number"=>"1", "street"=>"1", "city"=>"1", "state"=>"1", "zip_code"=>"1", "country"=>"1"}}, "commit"=>"Sign up"}
   (0.1ms)  begin transaction
  Store Exists (0.1ms)  SELECT  1 AS one FROM "stores" WHERE "stores"."email" = ? LIMIT ?  [["email", "example@gmail.com"], ["LIMIT", 1]]
  SQL (0.4ms)  INSERT INTO "stores" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["email", "example@gmail.com"], ["encrypted_password", "$2a$11$DN/K2dBlo4DzoVf9uMlD9uo8iKYp14ROT8w3eR9Tt8fD7B1ksdvt."], ["created_at", "2017-08-10 10:47:07.920623"], ["updated_at", "2017-08-10 10:47:07.920623"]]
  SQL (0.2ms)  INSERT INTO "accounts" ("buisness_name", "buisness_description", "web_site", "phone_number", "street", "zip_code", "country", "first_name", "last_name", "created_at", "updated_at", "store_id", "city", "state") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["buisness_name", "1"], ["buisness_description", "1"], ["web_site", "1"], ["phone_number", "1"], ["street", "1"], ["zip_code", "1"], ["country", "1"], ["first_name", "david"], ["last_name", "davisp"], ["created_at", "2017-08-10 10:47:07.922113"], ["updated_at", "2017-08-10 10:47:07.922113"], ["store_id", 1], ["city", "1"], ["state", "1"]]
   (10.0ms)  commit transaction
Redirected to http://localhost:3000/transactions/new.1?ema%E2%80%8C%E2%80%8Bil=example%40gmail.com&plan_id=bs96&price=150.0
 Completed 302 Found in 161ms (ActiveRecord: 10.7ms)

重定向到views / plans / new.html.erb:

   Started GET "/transactions/new.1?ema%E2%80%8C%E2%80%8Bil=example%40gmail.com&plan_id=bs96&price=150.0" for 127.0.0.1 at 2017-08-10 13:47:07 +0300
Processing by TransactionsController#new as 
  Parameters: {"ema‌​il"=>"example@gmail.com", "plan_id"=>"bs96", "price"=>"150.0"}
  Rendering transactions/new.html.erb within layouts/application
  Rendered transactions/new.html.erb within layouts/application (1.8ms)
Completed 200 OK in 1106ms (Views: 42.0ms | ActiveRecord: 0.0ms)    

最后,如果我点击视频/计划/ new.html.erb付费:

Started POST "/transactions" for 127.0.0.1 at 2017-08-10 13:47:24 +0300
Processing by TransactionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x/FWkCSqla3ugmaOgP79RySg6HMpafZel5ddl7nzIRZO6NZ7Wd HBBwbHOj7t0Yhp165RE83Z0+Eg29OillrY5Q==", "email"=>"", "plan_id"=>"bs96", "amount"=>"150.0", "payment_method_nonce"=>"fb99a62a-025d-0283-5932-fb3a297cc0e9"}
898559083
8yx4w8
Redirected to http://localhost:3000/
Completed 302 Found in 2535ms (ActiveRecord: 0.0ms)

0 个答案:

没有答案