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
Related
I have a form in rails with input values, and 1 of the values is a LOV (List of Values) and a second input field is also a LOV but depends on the input of the other field.
The input value of the first field is not saved in between. So I need to use that field value before it is saved.
So, the example:
I choose a company from the list of values of all companies for the field supplier, the second field supplier_address will be a LOV with all the addresses of that company, so the LOV of the second field is dependent on the value chosen in the first field, company.
What I tried:
def new
#purchase_requisition = PurchaseRequisition.new
#purchase_requisition.number = find_next_user_value("Purchase_Requisition")
##purchase_requisition.supplier_address_id = PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
#purchase_requisition = PurchaseRequisition.new(purchase_requisition_params)
respond_to do |format|
#purchase_requisition.supplier_address_id = PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
end
end
but I still get the error:
param is missing or the value is empty: purchase_requisition
Can someone please help me?
Thank you!!!
The error you're encountering isn't being caused by the code you've provided. You're probably using strong parameters and have a method like this:
def purchase_requisition_params
params.require(:purchase_requisition).permit(# some list of attributes #)
end
The problem is that params[:purchase_requisition] doesn't exist. Probably because the form_for in your view isn't referencing a purchase_requisition object. Try adding as: to your form_for to send your params under that param key:
form_for #requisition, as: :purchase_requisition, ....
Otherwise, you'll have to post more details about your view and controller to help isolate the issue you're having.
Also, in your controller code you want:
PurchaseRequisition.new(purchase_requisition_params[:supplier_id])
Instead of:
PurchaseRequisition.new.purchase_requisition_params[:supplier_id]
Supposing, all of your parameters belong to the same object (there isn't any nested attribute), this can be what you are looking for:
def purchase_requisition_params
params.require(:purchase_requisition).permit(:org_id, :document_type, :number, :supplier_id, :supplier_address_id, :partner_id, :project_id, :construction_site_id, :purchase_order_date, :expected_receiving_date, :promised_receiving_date, :supplier_order_number, :supplier_shipping_number, :supplier_invoice_number, :currency, :status) ##you don't need id here
end
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'm playing around with Netflix's Workflowable gem. Right now I'm working on making a custom action where the user can choose choices.
I end up pulling {"id":1,"value":"High"} out with #options[:priority][:value]
What I want to do is get the id value of 1. Any idea how to pull that out? I tried #options[:priority][:value][:id] but that seems to through an error.
Here's what the action looks like/how I'm logging the value:
class Workflowable::Actions::UpdateStatusAction < Workflowable::Actions::Action
include ERB::Util
include Rails.application.routes.url_helpers
NAME="Update Status Action"
OPTIONS = {
:priority => {
:description=>"Enter priority to set result to",
:required=>true,
:type=>:choice,
:choices=>[{id: 1, value: "High"} ]
}
}
def run
Rails.logger.debug #options[:priority][:value]
end
end
Here's the error:
Error (3a7b2168-6f24-4837-9221-376b98e6e887): TypeError in ResultsController#flag
no implicit conversion of Symbol into Integer
Here's what #options[:priority] looks like:
{"description"=>"Enter priority to set result to", "required"=>true, "type"=>:choice, "choices"=>[{"id"=>1, "value"=>"High"}], "value"=>"{\"id\":1,\"value\":\"High\"}", "user_specified"=>true}
#options[:priority]["value"] looks to be a strong containing json, not a hash. This is why you get an error when using [:id] (this method doesn't accept symbols) and why ["id"] returns the string "id".
You'll need to parse it first, for example with JSON.parse, at which point you'll have a hash which you should be able to access as normal. By default the keys will be strings so you'll need
JSON.parse(value)["id"]
I'm assuming the error is something like TypeError: no implicit conversion of Symbol into Integer
It looks like #options[:priority] is a hash with keys :id and :value. So you would want to use #options[:priority][:id] (lose the :value that returns the string).
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"].
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