简体   繁体   中英

How to fix Rails respond_with model that is not namespaced?

I am building an API with Rails. The controllers are namespaced with Api::V1 but the models are not. I'm adding responds_to :json and passing the model to respond_with but it fails when generating the route from the model. It should be api_v1_resource_url , but it is simply returning resource_url . What can I do to fix this?

Without seeing the relevant code in your question, try this:

respond_with(:api, @yourmodel)

I'm presuming your code is something like the following:

def Api::V1
  responds_to :json
  def create
    @yourmodel = YourObject.new(params[:yourmodel])
    @yourmodel.save
    respond_with(:api, @yourmodel) // TRY THIS
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM