I have a products scaffold set up, and I just created a new foo controller and view. In my foo controller I parse a url, and get back an arry of objects. 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. It would helpful if the attributes matched (the stuff you get from the url has the same attribute names as your product model)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.