1
I’m creating a project in Ruby on Rails, where I create a shout with the following route in Postman:
POST:http://localhost:3000/api/yells
{
"user_id":"1",
"title":"caneca",
"desciption":"beber",
"yell_type":"oferta",
"price":"20,00",
"categories":[{"name":"porcelana"}]
}
Controller #create :
def create
#@yell = Yell.new(yell_params.except(:categories))
@yell = Yell.new({title: params[:title], desciption: params[:desciption], price: params[:price], user_id: params[:user_id], yell_type: params[:yell_type]})
Array(params[:categories]).each do |rel|
@category = Category.find_by_name(rel[:name])
if @category
#only creates the relationship
else
@yell.categories.build(name: rel[:name]) #creates the relationship and category
end
end
if @yell.save
render json: @yell, status: :created, location: api_yell_path(@yell)
else
render json: @yell.errors, status: :unprocessable_entity
end
end
I’m doing a project with a friend of mine who’s doing the frontend, and when he tests in his machine for Postman, replacing the localhost
for my sake ip
, creates a yell
and categories, but with all empty values.
I imagine it’s because the params[:values]
is coming null, but it was not to come. Also because in my machine works and creates everything right. It still manages in the routes GET
pull everything right. There’s something I have to set up on the server or something else on the routes, or on Postman?
Can anyone help me I’m really without any notion of how to solve this problem.
See help: http://stackoverflow.com/questions/12705545/ruby-on-rails-params-is-nil-undefined-method-for-nilnilclass
– Filipe Moraes
However you are referring to "payment_type" but in POST request you are not passing as parameter, at least in the POST you entered in the question.
– Filipe Moraes
Or maybe this -> http://answall.com/questions/12840/ruby-on-rails-undefined-method-errors-for-nilclass?rq=1
– David
The weird thing is, on my Postman, it works, and on his, it doesn’t, I thought it might be something from the route. And I had already seen this Question, but in them they do the treatment for when something is null, but my mistake is because it is null, it was for him this receiving.
– Marcius Leandro
Actually it was an error in the call, I was forgetting to pass the content-type in the header of the flames, I found that had to put this in Routes also putting the following parameters:
defaults: { format: 'json' }
this will signal that all my calls in that context are of the json type.– Marcius Leandro