简体   繁体   English

尝试从另一个控制器获取变量 response_to

[英]Trying to get variable from another controller with respond_to

I have a controller named Orders , and another one named Customers .我有一个名为Orders的控制器,另一个名为Customers So, on my Order I want to save who my customer is.所以,在我的Order我想保存我的customer是谁。 These two models have associations.这两个模型有关联。

So, in my new order view, I used one form to bring who the customer is by his "cpf" (cpf is a document that we use here in Brazil to identify citizens), and I did this:因此,在我的新订单视图中,我使用了一个表格,通过他的“cpf”(cpf 是我们在巴西用于识别公民的文件)来显示客​​户是谁,我这样做了:

<%= form_tag search_customer_path, method: :get do %>
  <%= text_field_tag :cpf, params[:cpf], placeholder: 'CPF' %>
  <%= button_tag type: :submit, class: "btn btn-primary" do %>
    <%= 'Selecionar' %>
  <% end%>
<% end%>

To make this search_costumer_path and make the checkout_path (that we'll use later on), i dit this to routes.rb :为了制作这个search_costumer_path并制作checkout_path (我们稍后会使用),我将它写入routes.rb

get '/cart/checkout', to: 'orders#new', as: :checkout #-> This gives me the checkout_path
get 'search_customer', to: 'customers#search_customer'

And on my customers_controller.rb I did this:在我的customers_controller.rb我这样做了:

def search_customer
  @customer = Customer.find_by!(cpf: params[:cpf])
  respond_to do |format|
    format.html { redirect_to checkout_path } #-> @customer should be available in the checkout_path view, but it's not
  end
end

The problem is that when the app goes back to the checkout_path , @customer is nil .问题是当应用返回checkout_path@customernil

Aditional information: To try to find the error, I replaced the customers_controller 's search_customer action to byebug , so I could handcode on the server to see if the params are passing and the variable is being set, and there I have no problem.附加信息:为了尝试查找错误,我将customers_controllersearch_customer操作替换为byebug ,因此我可以在服务器上手动编码以查看参数是否正在传递以及是否正在设置变量,并且我没有问题。 I got something like this:我得到了这样的东西:

Input> params
Return> <ActionController::Parameters {"cpf"=>"05864899995", "button"=>"", "controller"=>"customers", "action"=>"search_customer"} permitted: false>
Input> @customer = Customer.find_by(cpf: params[:cpf])
Return> #<Customer id: 2, name: "Thiago Fiorese", cpf: "05864899995", cel_phone: "83984572906", email: "thiagofiorese@gmail.com", address: "Rua Helena Meira Lima", neighborhood: "Tambaú", city: "João Pessoa", state: "PB", zipcode: "58039081", complement: "Apto 1001", reference: "Ao lado do Motiva", created_at: "2020-01-20 19:36:16", updated_at: "2020-01-20 19:36:16", number: "691">
Input> @customer.name
Return: "Thiago Fiorese"

So... the params are passing, and the variable theoretically is being saved since I used the same code on my search_customer action.所以......参数正在传递,并且理论上该变量正在被保存,因为我在我的search_customer操作中使用了相同的代码。 I'm guessing the problem is sending that variable back to the checkout page (orders controller) with the respond_to , since when I'm redirected back to checkout_path , the @customer is nil .我猜问题是将该变量发送回带有respond_to的结账页面(订单控制器),因为当我被重定向回checkout_path@customernil

If you need this info: this app is a point-of-sale, so the user making the order is the shop owner or his employees, and the customer will not have access to the app, but should be saved on the order for statistics purposes.如果您需要此信息:此应用程序是一个销售点,因此下订单的用户是店主或其员工,客户将无法访问该应用程序,但应保存在订单上以进行统计目的。

You could just pass the @customer.id variable as a parameter like so:您可以将 @customer.id 变量作为参数传递,如下所示:

respond_to do |format|
  format.html { redirect_to checkout_path(customer_id: @customer.id }
end

and then in your orders_controller然后在你的 orders_controller

def new
  @customer = Customer.find(params[:customer_id])
  ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM