This question already has answers here:
ActionController::ParameterMissing (param is missing or the value is empty: film):
(3 answers)
Closed 7 years ago.
I am trying to send a post request using the Postman chrome extension to my Ruby on Rails app, but i keep getting the error
ActionController::ParameterMissing (param not found: order):
app/controllers/orders_controller.rb:27:in order_params'
app/controllers/orders_controller.rb:20:in create
The code in my orders_controller is
class OrdersController < ApplicationController
protect_from_forgery :except => :create
def new
#order = Order.new
end
def index
#orders = Order.all
end
def show
#order = Order.find(params[:id])
end
def create
#order = Order.new(order_params)
render text: params[:product]
end
private
def order_params
params.require(:order).permit(:product)
end
end
My key Value pairs to the Postman extension are product[product_name] Samsung
For you to use params.require(:order). the incoming parameters should be something like {"order"=>...}
Check the documentation at http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html to use Strong Parameters,.
Based on your information about the key-value pairs used, there is no key called "order" in the incoming-data. That's the reason - its failing.
Hope, this helps
Probably the params method is requiring something that you don't have. Inspect the params.
This worked for me:
Controller:
def progress_params
params.require(:progress).permit(:game_id, :level_id)
end
View:
<%= link_to "Completed", progresses_path(:progress =>{:game_id => #level.game_id.to_i, :level_id => #level.id.to_i} ), :method => :post %>
Related
I am trying to learn and write an update API and to start small I am passing a single params in the API and and try to get the response.
the controller :
module Api
module V1
class OrderApiController < ApiController
def order_update
response = Hash.new
result = Hash.new
#order = Order.find(params[:id])
if #order.update_attributes(order_params)
result['order_id'] = order.id
response['result'] = result
response.merge! ApiStatusList::OK
else
response.merge! ApiStatusList::INVALID_REQUEST
end
render :json => response
end
private
def order_params
params.require(:order).permit( :id)
end
end
end
end
the api route in routes.rb is:
match 'mobile/order_update' =>'order_api#order_update'
The url link what I give is
http://localhost:3000/api/v1/mobile/order_update?key=docket&id=1
However this throws the following error
ActionController::ParameterMissing at /api/v1/mobile/order_update
param is missing or the value is empty: order
I dont know what am I doing wrong. I am new to Rails as well as API generation. Please help
This is caused by the order_params method, in which you're requiring order(expecting order to be a nested hash), whereas, you're not nesting it.
An approach you could take is to visit:
http://localhost:3000/api/v1/mobile/order_update?key=docket&order[id]=1
Also, I see you're setting #order instance variable, but in your control block(if #order.update_attributes), you're using a local variable which would give you another error.
I'd recommend you go through the Hartl Tutorial as there are a lot of things you'd be able to learn from there
UPDATE
Based on the new error mentioned in the comment, I think you should rather be visiting:
http://localhost:3000/api/v1/mobile/order_update?order[key]=docket&id=1
This is assuming your orders table has a column key based on the params being set
Also, change your order_params to:
private
def order_params
params.require(:order).permit( :key) #since you cannot update a primary key, but I guess you want to update the key column
end
The solution I used is as follows
In my order_api_controller.rb , I have Changed
def order_update
response = Hash.new
result = Hash.new
#order = Order.find(params[:id])
if #order.update_attributes(order_params)
result['order_id'] = order.id
response['result'] = result
response.merge! ApiStatusList::OK
else
response.merge! ApiStatusList::INVALID_REQUEST
end
render :json => response
end
and edited it to this
def order_update
response = Hash.new
result = Hash.new
debugger
#order = Order.find(params[:order][:id]) # passed the order symbol into params
if #order.update_attributes(order_params)
result['order_id'] = #order.id # Modified local variable to instance variable as per oreoluwa's suggestion
response['result'] = result
response.merge! ApiStatusList::OK
else
response.merge! ApiStatusList::INVALID_REQUEST
end
render :json => response
end
And used the url as Follows
http://localhost:3000/api/v1/mobile/order_update?key=docket&order[id]=1
This seems to do the trick
When I try to create a user, Rails returns a ParameterMissing error:
ActionController::ParameterMissing in UserController#create
param is missing or the value is empty: user
My Controller
class UserController < ApplicationController
def create
#user = User.new(user_params)
render json: #user
end
private
def user_params
params.require(:user).permit(:fame, :lame, :mobile)
end
end
My User Model
class User < ActiveRecord::Base
self.table_name = "user"
end
What am I doing wrong?
Check your logs for the params that are being sent to your controller. Most likely, the params hash being sent by your view doesn't include the :user, key. To fix, you'll need to make sure your form_for is properly namespaced with a User object:
form_for #user do |f|
# ...
end
You can also use the as key to explicitly set the :user key in your params.
form_for #object, as: :user, method: :post do |f|
# ...
end
Update
Since the questioner was using postman to send data, the data sent to the server should be properly formatted like so:
user[firstName]
Thanks #Fabio and #Anthony
When you asked about the form, I actually realized that the parameter I sending with postman was actually incorrect as they should be like
user[firstName]
Updated
It actually deepns upon you how you send the params.
I send as
user[firstname] So I get like params[:user][:firstName]
If I send like firstname So this will be params[:firstName]
I'm building a basic admin rails application for a Service I use to make creating new users more efficient. Right now, I'm doing it manually, within the application, and it has some tedious steps that I'd like to automate.
The service considers users as "Members"
So I've created a Members scaffold in my rails project which has the same parameters as members do in the Service.
Instead of entering some data in the Service application, I want to do that in my app. So I have a form with several parameters that create a Member and save those parameters: :usname :usemail :usstudio_uid
Next, I want to POST to the Service API with the initial fields that were entered to create an "invitation" for the new member.
I'm trying to do that by calling on a HTTParty function in my Member's Show view.
My form is saving the parameters correctly, I'm connecting to the Service API via HTTParty and creating an invitation OK, but the Member parameters I want to send aren't populating. Instead it's turning what I thought was a reference to the parameter in plain text.
Snippet of that function:
:body => {"callback_url" => "https://foo.com/#invitations",
"consumer_key" => "my consumer_key", "email" => :usemail,
I want :usemail to reference the parameter of the Member being referenced in the show page(i.e. cliff#foo.com). Instead, as you'll see below, the Service API instead thinks the parameter is a string, returning "usemail" as the email parameter, not that of the Member object I want to reference.
I'm pretty new to rails and coding, so it's probably an obvious answer, but I spent a good 6 hours yesterday trying to figure it out. Help! :)
members.rb model:
class Member < ActiveRecord::Base
end
members_controller rb:
Class MembersController < ApplicationController
before_action :set_member, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#members = Member.all
respond_with(#members)
end
def show
#member = Member.find(params[:id])
end
def new
#member = Member.new
respond_with(#member)
end
def edit
respond_with(#member)
#member.save
end
def create
#member = Member.new(member_params)
#member.save
respond_with(#member)
end
def update
#member.update(member_params)
respond_with(#member)
end
def destroy
#member.destroy
respond_with(#member)
end
private
def set_member
#member = Member.find(params[:id])
end
def member_params
params.require(:member).permit(:usname, :usemail, :usphone, :uspassword, :usconfirm_password, :usinvitation_uid, :usstudio_uid, :uscallback_url, :usmember_uid, :ususername, :usaccess_token)
end
end
In my members_helper.rb:
def getinvitation(member)
result = HTTParty.post "https://foo.com/api/v2/studios/studio_uid/invitations", :body => {"callback_url" => "https://foo.com/#invitations",
"consumer_key" => "my consumer_key", "email" => :usemail,
"name" => :usname, "studio_uid" => :usstudio_uid
}.to_json, :headers => {'X-Auth-Token' => "my token"}
JSON.parse(result.body)
x = ActiveSupport::JSON.decode(result.body)
end
I call on the function in the member's show path:
views/members/show.html.erb
<%= getinvitation(#member) %>
Here is the response I get:
{"uid"=>"29ad0740f4aa47d788bb2a34e9ab7d78", "studio_uid"=>"ORfspJitFbVG",
"date_sent"=>"Sun Feb 1 16:49:21 2015", callback_url"=>"https://app.ustudio.com/#invitations?
invitation_uid=29ad0740f4aa47d788bb2a34e9ab7d78&studio_uid=ORfspJitFbVG",
"consumer_key"=>"my consumer_key", "email"=>"usemail", "name"=>"usname"}
Basically, this is what I was looking for
"email" => member.usemail not :usemail or "usemail"
I'm trying to learn rails 4.1 from rails 4 in action book, I've got to chapter 13, in this chapter defined API for JSON and XML.
It defined a controller like this :
class Api::V1::ProjectsController < Api::V1::BaseController
def index
respond_with(Project.for(current_user).all)
end
def create
project = Project.new project_params
if project.save
respond_with(project, :location => api_v1_project_path(project))
else
respond_with(errors: project.errors.messages)
end
end
private
def project_params
params.require(:project).permit(:name)
end
end
And a rspec test like this :
it "unsuccessful JSON" do
post "#{url}.json", :token => token, :project => {}
expect(last_response.status).to eql(422)
errors = {"errors" => { "name" => ["can't be blank"]}}.to_json
expect(last_response.body).to eql(errors)
end
when I run the test, I get these results :
ActionController::ParameterMissing: param is missing or the value is
empty: project
I know, it's because of strong parameters.
i solved issue like this but any one has a batter idea?
def project_params
if params[:project].nil?
params[:project] = {:name => ''}
end
params.require(:project).permit(:name)
end
it has resolved this problem in my case
params.require(:project).permit(:name, :description) if params[:project]
You can permit nested parameters as explained in strong parameters in github.
In your case, this is probably what you want:
params.permit(:project => [:name, :description])
I am trying to upgrade my app from Rails 3 to Rails 4 and I can't seem to get the syntax right in line 12:
class ProjectsController < ApplicationController
before_filter :find_project
...
private
def find_project
#project = Project.find(params[:id])
end
def valid_people
if params[:project][:person_ids].present? # how to do this with strong parameters?
person_ids = params[:project][:person_ids].map(&:to_i)
valid_ids = current_user.people.pluck(:id)
redirect_to root_path if (person_ids - valid_ids).any?
end
end
def project_params
params.require(:project).permit(:name, :description, :person_ids)
end
end
I keep getting this error: Unpermitted parameters: person_ids
How can I use only the person_ids parameter?
Thanks for any help.
The idea of strong parameters is that you call the function you have defined to get your parameters
if params[:project][:person_ids].present? # how to do this with strong parameters?
would become
if project_params[:person_ids].present? # how to do this with strong parameters?
also i'm guessing :person_ids is an array if so replace your :person_ids with {:person_ids => []}