Because stop["value"] is returning nothing

Asked

Viewed 78 times

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

  • 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.

  • Or maybe this -> http://answall.com/questions/12840/ruby-on-rails-undefined-method-errors-for-nilclass?rq=1

  • 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.

  • 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.

2 answers

1

Try to access the parameters using:

params[:yells][:categories}.each |rel|
end
  • So @Ghilherme, my biggest doubt is why the call works this way as I’m doing on my machine, and on his not.

  • You put a little code there, maybe it’s something with your friend’s routes. create seems functional.

0


The problem is that my friend was forgetting to pass Header on his call, he should pass a content-type=aplication/json.

I found that there is a way for me to define by default that all my calls are of the type json, for that I encapsulated all my calls to API with:

namespace :api, defaults: { format: 'json' }  do
  ...
end

cannot forget that in the controllers that are called by that route place before the class name a Api::

#nomes_controller.rb
class Api::NomesController < ApplicationController
  ...
end
  • It’s the second comment that I see that you answer yourself. Nothing against. I just find a good strategy to earn points in stackoverflow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.