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]
Related
I have an array of parameters and i want to replace all parameters by looping over array in ruby on rails.
I am using before_action in controller as
before_action :cost_format
This is an array of parameters. The format of the parameter is string, i want to run a function on each parameter and convert it into float. So i am looping an array and calling function on each element.
def cost_format
x = [params[:cost_1], params[:cost_2], params[:cost_3]]
x.each do |i|
convert_cost(i)
end
end
I have a function convert_cost as following
def convert_cost(x)
x.gsub(',', '.').to_f
end
How do i loop an array so that my parameters get replaced.? Parameters are not being replaced by above code. Any hint would be appreciated.
Thanks
I think you'll want something like this:
def cost_format
%i(cost_1 cost_2 cost_3).each do |key|
params[key] = convert_cost(params[key])
end
end
def convert_cost(val)
val.gsub(',', '.').to_f
end
This grabs each key from your params and replaces the value with it passed through the convert_cost method.
Edit: you might be able to ignore this section about convert_cost due if it works with the format you're getting your numbers in. Please excuse my ethnocentrism :)
I've not updated your convert_cost method, though I'm a little wary about whether it will work at the moment. If you've got, for example "1,234,567" and you call your gsub you get "1.234.567". Calling to_f on that gives you 1.234, which I wouldn't think you'd want?
Instead of that, you could use:
def convert_cost(val)
val.gsub(',', '').to_f
end
E.G.
convert_cost("1,234,567")
# => 1234567.0
Combining all that, the following would be converted like so:
params = { cost_1: "1,234,567", cost_2: "123", cost_3: "456.5", cost_4: "I won't be touched" }
# after `cost_format` before_action runs:
# => {:cost_1=>1234567.0, :cost_2=>123.0, :cost_3=>456.5, :cost_4=>"I won't be touched"}
Let me know how you get on or if you have any question - hope this helps.
Could you try something like
x.map{|param| param.gsub(',', '.').to_f}
directly on your array.
So your cost_format method will look like:
def cost_format
x = [params[:cost_1], params[:cost_2], params[:cost_3]]
result = x.map{|param| param.gsub(',', '.').to_f}
# do stuff with result
end
I think the reason it's not working because, each method returns its receiver, but map returns a new array.
I want to call a controller function with ajax that works fine, problem is the object generation.
I try it this way:
#tag = Tag.new params[:tagname]
#tag.save
But so I got error 500 back
If I do this
#tag = Tag.new params.permit[:tagname]
#tag.save
That "works" but in database the column tagname is Null ...
whats the problem?
What can I do to fix that?
If I understand what you're doing here correctly, your problem is that you're passing a string to Tag#new. However, unless you've modified the Tag class, you actually need to do something like:
Tag.new name => params[:tagname]
That is, "new" doesn't take a string (again, unless you've modified it) it takes a Hash.
Correct solution is
#tag = Tag.new
#tag.tagname = params[:tagname]
Thaks for the hint with hash problem ;)
You could do this in a single statement using create
Tag.create(tagname: params[:tagname])
I have a params hash that looks like this:
puts contact_params
=> {"classifiable_classification_codes_attributes"=>{"0"=>{"id"=>"5", "relateds_attributes"=>{"0"=>{"classifiable_id"=>"6", "id"=>"15"}}}}}
So I expect when I do this contact_params["classifiable_classification_codes_attributes"], I subsequently get {"0"=>{"id"=>"5"...
In fact, that is exactly what happens in the console:
> contact_params["classifiable_classification_codes_attributes"]
=> {"0"=>{"id"=>"5", "relateds_attributes"=>{"0"=>{"classifiable_id"=>"6", "id"=>"15"}}}}
However, in the controller when I try do this, it returns a nil value, as if classifiable_classification_codes_attributes is not a key. I also tried the symbol form :classifiable_classification_codes_attributes. But neither of them return any results.
What might I be doing wrong?
I'm guessing this might be a Strong Parameters issue in which case you need to add something like
private
def my_params
params.require(:classifiable_classification_codes_attributes).permit(: id, :relateds_attributes)
end
Read more at: http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters
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"].
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.