Because param ["value"] is returning nothing

1

I am creating a project in Ruby on Rails, where I create a cry 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 is doing the frontend, and when he tests on his machine by postman, replacing localhost with my ip , creates a yell and categories, but with all empty values.

I suppose it's because params[:values] is coming null, but it was not to come. Because in my machine it works and creates everything right. It still succeeds on the%% routes by pulling everything right. Is there something I have to configure on the server or something else on the routes, or in the postman?

Someone can help me, I really have no idea how to solve this problem.

    
asked by anonymous 26.01.2016 / 18:39

2 answers

0

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

I have found that by default I have set all my calls as json , so I encapsulated all my calls to API with:

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

You can not forget in the drivers that are called by this route to place before the name of the class a Api::

#nomes_controller.rb
class Api::NomesController < ApplicationController
  ...
end
    
03.02.2016 / 13:44
1

Try to access the parameters using:

params[:yells][:categories}.each |rel|
end
    
27.01.2016 / 03:12