Question: I have a problem while I'm triying to return a json from Ruby.

This is my controller(**parts\_controller**).

```
    def show()
    @part = Part.find(params[:id])
    unless @part.nil?
        render json: @part
    end       
end

```

my routing file:

```
Rails.application.routes.draw do

  root to: "home#index"
  resources :parts

end

```

and this es my post method from Ionic.

```
postData()
{

  let headers = new Headers();
  headers.append('Content-Type', 'application/json');

    let body = {
   id: 1
  };

    this.http.post('http://localhost:3000/parts/1', JSON.stringify(body), {headers: headers})
    .map(res => res.json())
    .subscribe(data =>{
        console.log(data);
   });          
}

```

if I manually enter to <http://localhost/3000/parts/1> it returns me the JSON but when I'm triying to get it by post it throws: **POST <http://localhost:3000/parts/1> 404 (Not Found)**

I also have the views for show and index method, thank you for your time.

Answer: 
How to fix it
-------------

```
this.http.get('http://localhost:3000/parts/1', {headers: headers})
    .map(res => res.json())
    .subscribe(data =>{
        console.log(data);
   }); 

```

WHy?
----

because the show should have GET not POST method. A POST is used to send (POST) data to the Backend rather than GETting the data.

If you use rake routes or rails routes you will find the correct paths with the methods that you can use with each and every action.
I created a parent class for all my http requests,

> 
> api.service.ts
> 
> 
> 

```
import {HttpClient, HttpParams} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {ENV} from '@app/env';
import {Observable} from "rxjs/Observable";

@Injectable()
export class Api {
  constructor(public http: HttpClient) {}

  post(endpoint: string, body: any) {
    return this.http.post(ENV.SERVER_URL + '/' + endpoint, body);
  }
}

```

and I wrote a class to work before every request

> 
> http.token.interceptor.ts
> 
> 
> 

```
export class TokenInterceptor implements HttpInterceptor {
  constructor(public auth: AuthService) {}
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    request = request.clone({
      setHeaders: {
        Accept: 'application/xxx.v1+json',
        Authorization: "bearer " + this.auth.getToken()
      }
    });
    return next.handle(request);
  }
}

```

so the added headers along with every request goes to the server.

> 
> part.service.ts
> 
> 
> 

```
...
constructor(public api: Api) {}

  getParts(): Observable<Part[]>{
    return this.api.get("parts")
      .map(res => {
        if(res) {
          return res.map(item => {
            return new Part(item);
          });
        }
      });
  }
...

```

We're just now ready to use the service to subscribe

> 
> any service or component
> 
> 
> 

```
this.partService.getParts().subscribe(parts => {
   ..
   });
},(error: Response) => {
  return Observable.throw(error)
});

```

---

you need to rack-cors for server-side

> 
> Gemfile.rb
> 
> 
> 

```
gem 'rack-cors'

```

and you must added to ...

> 
> application.rb
> 
> 
> 

```
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', headers: :any, methods: %i[get post options patch delete]
      end
    end

```