【问题标题】:Adding parameters in a query string, Rails在查询字符串中添加参数,Rails
【发布时间】:2017-08-09 11:56:58
【问题描述】:

我正在将查询字符串中的参数添加到 new_store_registration 并由 after_sign_up_path_for 方法保存的路径。 注册控制器.rb

def new 
  session[:registration_params] = request.query_parameters
end


def after_sign_up_path_for(resource)
  new_transaction_path(resource, session[:registration_params])
end

我正在提取以下参数:

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

注册后,应用会重定向到 views/transcation/new.html.erb,其中包含 plan_id、价格参数。

此时我正在尝试在 new.html.erb 中传递电子邮件参数并保留其余参数:

<%= hidden_field_tag(:email, params[:email]) %>
<%= hidden_field_tag(:plan_id, params["plan_id"]) %>
<%= hidden_field_tag(:amount, params["price"]) %>

但是电子邮件参数是空的

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

知道为什么这不起作用???

更新1

提交注册后,我得到了这个:

Started POST "/stores" for 127.0.0.1 at 2017-08-09 15:24:17 +0300
Processing by Stores::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓",  "authenticity_token"=>"8szvzrbEVUb1xvYcKwidi+4AYnQD46R96Y0C6ONiI65mEtRkATtJ  IRXCb60UNyKs0knwpeDnMLwy9iXmK8TRgw==", "store"=> {"email"=>"example@gmail.com", "password"=>"[FILTERED]",  "password_confirmation"=>"[FILTERED]", "account_attributes"=> {"first_name"=>"ted", "last_name"=>"tedpaps", "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.2ms)  SELECT  1 AS one FROM "stores" WHERE  "stores"."email" = ? LIMIT ?  [["email", "example@gmail.com"], ["LIMIT",  1]]
  SQL (0.5ms)  INSERT INTO "stores" ("email", "encrypted_password",  "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["email",  "example@gmail.com"], ["encrypted_password",  "$2a$11$Xet14fY.N3rvvPZ2sjdn6ObbKShVMkw/4qmPWiPG/OjsRmiIxqlGq"],  ["created_at", "2017-08-09 12:24:17.640427"], ["updated_at", "2017-08-09  12:24:17.640427"]]
  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", "ted"], ["last_name", "tedpaps"], ["created_at", "2017-08-09 12:24:17.642495"], ["updated_at", "2017-08-09 12:24:17.642495"], ["store_id", 1], ["city", "1"], ["state", "1"]]
   (1.6ms)  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 160ms (ActiveRecord: 2.6ms)

并被重定向到:

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-09 15:24:17 +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.0ms)
Completed 200 OK in 1209ms (Views: 37.7ms | ActiveRecord: 0.0ms)

【问题讨论】:

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


    【解决方案1】:

    查看您的更新,您的 email 参数名称中似乎有一个字符,看起来像 a 但不是。可见

    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-09 15:24:17 +0300
    

    仔细检查并重新输入email 您设置它的位置。

    【讨论】:

    • hmmmm...我看不到a 错字,它在哪里?
    • 有编码的ema%E2%80%8C%E2%80%8Bil=example%40gmail.com,表示有字符需要编码,即不是a
    • Rails 的一种新事物,所以指针会很感激,我如何找到并编码字符?
    • 我在new_transaction_path(session[:registration_params].merge(email:resource.email)) 中有一些隐藏的空白字符...我不得不重新输入这一行,它成功了!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多