I have parameter passing in console shows as:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Oj9EGihSOwgdXGLLQWqVESYMP/N4K0KzDS4KyVhWXPg=", "rfp"=>{"user_id"=>"", "client_id"=>"", "othercms"=>"", "otherecommerce"=>"", "numberofpage"=>"", "designcomplexity"=>"", "browser"=>"", "nuhacks"=>"", "nujavascript"=>"", "numberofmenu"=>"", "designpages"=>"", "designformobilepages"=>"", "framworks"=>"", "test_report_ids"=>[""], " payment_gateway_ids"=>[""], "payment_gateway_ids"=>["2"], "**payment_gateways"=>{"name"=>"slsk"}**, "commit"=>"Create Rfp", "project_id"=>"18"}
Controller:
#rfp = Rfp.new(params[:rfp])
if [:payment_gateway][:name]
#pm=PaymentGateway.new([:payment_gateways][:name])
end
as payment gateway is independent model:
Even though paymengt gateway name passing in params it shows above error. What is missing? Please give me any help. Thanks in advance.
I think you dont understand data types.
On first line, you initialized new instance of Rfp class, and then you are trying to retrieve index of nothing, instead of array or hash.
There are two solutions for this.
I noticed that payment_gateways are inside rfp parameters, so i guess its association or attribute of it, so you can check "show me all the names of payment_gateways in newly initialized object"
if #rfp.payment_gateways.map(&:name).any?
Check in params:
if params[:rfp].present? and params[:rfp][:payment_gateways].present? and params[:rfp][:payment_gateways][:name].present?
After that, initialize your PaymentGateway instance:
`#pm = PaymentGateway.new(params[:rfp][:payment_gateways])`
As per your code it should be if params[:rfp][:payment_gateways][:name] not if [:payment_gateway][:name]
So It should look like
as your incomplete params there is rfp as well so it might be params[:rfp][:payment_gateways][:name]
if params[:rfp][:payment_gateways][:name]
#pm=PaymentGateway.new(params[:rfp][:payment_gateways][:name])
end
or inliner
#pm=PaymentGateway.new(params[:rfp][:payment_gateways][:name]) if params[:rfp][:payment_gateways][:name]
In Controller, it should be:
#rfp = Rfp.new(params[:rfp])
if params[:payment_gateway]
#pm=PaymentGateway.new(params[:payment_gateways][:name])
end
or even better
#rfp = Rfp.new(params[:rfp])
#pm=PaymentGateway.new(params[:payment_gateways][:name]) unless params[:payment_gateway].nil?
also check, whether it should be params[:payment_gateway] or params["payment_gateway"].
Related
My View make a redirect an pass the Params
?cart_item[price]=5%2C70€
.
I try to get this with
#price = [:cart_item][:price]
, but there i get the error "can't convert Symbol into Integer".
I try it with codes like ":cart_item_price" or ":cart_item.price" but always no variable was founded
How can i get this price in my other action ?
You are forgetting something in your code.
What you must do is #price = params[:cart_item][:price] this should work.
Also ensure you check if the key cart_item exists before you assign the value to the price variable.
Something like this should work #price = params[:cart_item][:price] if params[:cart_item]
You have omitted the params hash to extract the value from. Hence the error.
Hope that helps.
I try to get this with
#price = [:cart_item][:price]
I think you need to put an object before this [:cart_item][:price]. Maybe params?
#price = params[:cart_item][:price]
From Previous:
Rails 4 Nested Resources/Routes... almost there...?
My private method within my lines controller to load the manufacturer into the controller is throwing an error...
I use before_filter :load_manufacturer
and the function is:
def load_manufacturer
#manufacturer = Manufacturer.find(params[:manufacturer_id])
end
When I try to edit the line instance in the form, I get:
Couldn't find Manufacturer with id=manufacturer_id
But it is passing the manufcaturer params ok...
Parameters:
{"manufacturer_id"=>"manufacturer_id",
"id"=>"17"}
Your manufacturer id is incorrectly set. Its set to string "manufacturer_id" instead of an integer id value(in String format). The problem lies else where. As you can see
{"manufacturer_id"=>"manufacturer_id"
Should look something like
{"manufacturer_id"=>"1"
manufacturer_id should be an integer value
Is it possible to dynamically create key names of a hash? I'm passing the following hash parameters:
params[:store][:store_mon_open(5i)]
params[:store][:store_mon_closed(5i)]
params[:store][:store_tue_open(5i)]
params[:store][:store_tue_closed(5i)]
.
.
.
params[:store][:store_sun_open(5i)]
params[:store][:store_sun_closed(5i)]
To check if each parameter exists, I'm using two arrays:
days_of_week = [:mon, :tue, ..., :sun]
open_or_closed = [:open, :closed]
But, I can't seem to figure out how to dynamically create the params hash (the second key( with the array. Here's what I have so far:
days_of_week.each do |day_of_week|
open_or_closed.each do |store_status|
if !eval("params[:store][:store_#{day_of_week}_#{store_status}(5i)").nil
[DO SOMETHING]
end
end
end
I've tried a bunch of things including the eval method (as listed above) but rails seems to dislike the parentheses around the "5i". Any help is greatly appreciated!
You should be able to do
if params[:store]["store_#{day_of_week}_#{store_status}(5i)".to_sym]
Note that you were missing the ? on .nil? and that !object.nil? can be shortened to just object
Assuming this is a HashWithIndifferentAccess, you should be able to access it via string just as you could with a symbol. Thus:
days_of_week.each do |day_of_week|
open_or_closed.each do |store_status|
key = "store_#{day_of_week}_#{store_status}(5i)"
unless params[:store][key]
# DO SOMETHING
end
end
end
If it's not a HashWithIndifferentAccess then you should just be able to call key.to_sym to turn it into a symbol.
Been trying to get this working for the past several hours, I'm probably over looking something quite simple. Basically I have a "posts" model, and one of the columns I have created is named guid, which is just a user definable field that gets saved to the database.
I'm looking to find_by the guid instead of the default id.
Of course I've tried #post = Post.find_by_guid(params[:guid]) in my posts controller show action, but when I attempt to call it from the url (ex: mysite/post/53), in the server console it says guid is NULL
Webrick Console:
Processing by PostsController#show as HTML
Parameters: {"id"=>"53"}
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."guid" IS NULL LIMIT 1
....
if the route is
get "/posts/:id"
Then you will its the id that is going to be placed in the params which you can see in the output. So use params[:id]
#post = Post.find(params[:id])
You should change your route if you are passing a GUID:
get "posts/:guid" ...
Hit the url "posts/21EC2020-3AEA-1069-A2DD-08002B30309D"
And then in the controller:
#post = Post.find_by guid: params[:guid]
Rails passes instance variables (any variable starting with #) set in the controller to the views.
Referencing instance variables in the view that the controller did not set will return nil, which does not have any of the methods you are going to call on it, hence the "No method" errors.
Any time you run into trouble, check your assumptions at the console. I recommend using Pry. Using Pry (calling "binding.pry" in your code) you can actually stop the execution at any point and look around. That way you never have to guess.
The find_by_[column] method is deprecated.
Use the following syntax: find_by guid: params[:guid]
Your parameters hash does not contain a :guid key. If you were to use mysite/post/53?guid=SOMETHING then you will see a :guid key in params.
Do you want the 53 in this example to be the GUID parameter? There are ways to change the Rails routes to get that parameter to be guid instead of id, but if 53 is the GUID, you can always go:
Post.find(:guid => params[:id]).first
Im trying to figure out how i would get attributes/propertys from an object.
locations.rb model would have attribute :city
#location = Location.where(:id => 1)
Seems to go find inside my controller but how would I get access to the :city from that #location ?
I tried all combinations and thought it had to be:
#location.city
puts #location.city needs to print value of city
But instead I'm getting
undefined method `city' for #<ActiveRecord::Relation:0x007ff2d1506330>
Anyone could clarify why this not work and what I miss here? Ive searched for examples on how to do this and tried all combinations to no avail :(
thx
Arel's where query returns a collection of models, even if there is only one result.
Location.where(:id => 1) will effectively return an array (actually an ActiveRecord::Relation) of Location objects, even though there will only be one result from this particular query in that array.
To get around this, either do
#location = Location.where(:id => 1).first
# or...
#location = Location.find(1) # Finds by ID
To retrieve an object from its id, use
#location = Location.find(1) # 1 is the id your looking for
Read the guide on Active Record Query Interface to find more about find and where.
Actually, where returns an array not an instance but find does. As city is the attribute your model has instance method city which you cant use for array. So try #location = Location.find(1) then #location.city