Reputation: 464
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.
Upvotes: 0
Views: 1199
Reputation: 26
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.
Upvotes: 0
Reputation: 1278
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
Upvotes: 1
Reputation: 285
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".
Upvotes: 3