0

I'm new in RoR. I have search everywhere in this site and I could not find an answer to my question.

I tried to create api in Ruby on Rails without view. Basically, I successful in do method get and post to create new data.

My problem now I'm getting no routes match error for update and delete. Below is my code for posts controller:

def update
  @post = Post.where(id: params[:id], user_id: session[:user_id]).first
  if @post.update_attribute(:description, params[:description])
    render json: @post
  else
    render json: {error: 'Process not completed'}
  end
end

def destroy
  @post = Post.where(id: params[:id], user_id: session[:user_id]).first
  if @post.destroy
    render json: {status: 'Successful'}
  else
    render json: {status: 'Your cannot delete this data'}
  end
end

and below is my code for routes

namespace :api do
  namespace :v1 do
    get 'users/request_token', to: 'users#request_token'
    resources :users
    resources :posts
  end
end

I know this is basic code for you guys. But I hope there's someone can help new people like me to improve in RoR.

Below is view for my error enter image description here

Flip
  • 6,233
  • 7
  • 46
  • 75
Aizuddin Badry
  • 454
  • 3
  • 9
  • 22

3 Answers3

3

You have to call
"http://localhost:3000/api/v1/posts/333" for delete post with method "DELETE",
and for edit/update post "http://localhost:3000/api/v1/posts/333" with method "PUT".

Krishna Vyas
  • 285
  • 2
  • 8
1

While deleting a post you need to pass a id along with the url

example: localhost:3000/api/v1/posts/:id?token=xyz

you can check your routes using the rake routes command

I hope this helps

kajal ojha
  • 1,248
  • 8
  • 20
0

Your calling route must me like /api/v1/posts/:post_id and in case of update it must be PUT type and in case of delete it must be of DELETE type request. Please give it a try and let me know if it helps.

Ankit Jain
  • 26
  • 4