[英]Passing parsed variables into different models form
I have a products scaffold set up, and I just created a new foo controller and view. 我已经建立了一个产品支架,并且刚刚创建了一个新的foo控制器和视图。 In my foo controller I parse a url, and get back an arry of objects.
在我的foo控制器中,我解析了一个url,并获取了所有对象。 How can I pass each of these variables into the products form as defaults?
如何将这些变量中的每一个作为默认值传递到产品表单中?
my controller: 我的控制器:
require 'net/http'
require 'json'
def index
if params[:commit] == "Add Product"
@productId = params[:q]
@resultsReceived = true
if
url = URI.parse("url" + params[:q].to_s)
@response = JSON.parse(Net::HTTP.get_response(url).body)
end
else
@resultsReceived = false
@response = []
end
respond_to do |format|
format.html
end
end
end
My current index 我目前的索引
<%= form_tag("/foo", :method => "get") do %>
<%= label_tag(:q, "Enter Product Number") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Add Product") %>
<% end %>
<% if @resultsReceived == true %>
Title: <%= @response["product"]["title"] %> </br>
ID_Str: <%= @response["product"]["id_str"] %> </br>
Image Url: <%= @response["product"]["image_url"] %> </br>
Base Item Price: <%= @response["product"]["base_item_price"] %> </br>
Current Item Price: <%= @response["product"]["price"] %> </br>
Seller Name: <%= @response["product"]["mp_seller_name"] %> </br>
Description: <%= @response["product"]["descr"] %> </br>
<% end %>
I want the variable above to be passed to my already existing products from. 我希望将上面的变量传递给我已经存在的产品。
I think you have to move the access of the other data from your index action to either your your new action, or an action you create 'new_prefilled' possibly. 我认为您必须将对其他数据的访问从索引操作移至您的新操作,或可能创建的“ new_prefilled”操作中。 It would helpful if the attributes matched (the stuff you get from the url has the same attribute names as your product model)
如果匹配的属性会很有帮助(您从url获得的内容与您的产品型号具有相同的属性名称)
ie 即
def new_prefilled
if url = URI.parse("url" + params[:oid].to_s)
@response = JSON.parse(Net::HTTP.get_response(url).body)
end
@product = Product.new(@response['product'])
render 'new'
end
Then you'd have to add a route, 然后,您必须添加一条路线,
get '/products/new/:oid' => 'products#new_prefilled'
Then in your index action, you would do this: 然后在索引操作中,您将执行以下操作:
if params[:commit] == "Add Product"
redirect_to "/products/new/#{params[:q]}"
end
So you would render your new products view, but it would be pre-filled with that data you got from the other site. 因此,您将呈现新产品视图,但将使用从其他站点获得的数据预先填充该视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.