我有一個的routes.rb文件看起來像這樣:如何在Rails中創建後更改路徑的位置?
namespace :api do
namespace :v1 do
resources :posts, except: [:new, :edit]
end
end
這讓我產生像「mywebsite.com/api/v1/posts」,而不僅僅是「mywebsite.com/posts」之類的網址默認。
我的創建方法如下:
def create
@post = Post.new(params[:post])
if @post.save
render json: @post, status: :created, location: @post
else
render json: @post.errors, status: :unprocessable_entity
end
end
的location: @post
偉大的工作,直到我命名空間我的網址。我怎樣才能讓location: @post
反映這些變化?