Reputation: 12871
I'm having trouble understanding an error I'm getting from a view after doing some namespacing:
Request: http : // dev /data/state_categories.json [GET]
Error:
NoMethodError in Data::StateCategories#index
Showing (dev_root)/app/views/data/state_categories/index.json.jbuilder where line #3 raised:
undefined method `state_category_url'
File in question: app/views/data/state_categories/show.json.jbuilder
json.array!(@state_categories) do |state_category|
json.extract! state_category, :name, :id
json.url state_category_url(state_category, format: :json) #<-- this is the line that is erroring
end
I had some scaffolding setup for a resource and then decided to namespace under "api". I made a new "data/" dir in controllers and in views, and moved the resource's controller and view into those dirs, which seemed to be necessary. Now there is a method in one of my views that isn't being found, not sure what I need to fix it. I never needed to define this method before I moved everything, it just worked. Where did I break the magic?
config/app.routes.rb
namespace :data, defaults: {format: :json} do
resources :state_categories
end
app/controllers/data/state_categories_controller.rb
class Data::StateCategoriesController < ApplicationController
before_action :set_state_category, only: [:show, :edit, :update, :destroy]
# GET /state_categories
# GET /state_categories.json
def index
@state_categories = StateCategory.all
debugger
end
private
# Use callbacks to share common setup or constraints between actions.
def set_state_category
@state_category = StateCategory.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def state_category_params
params.require(:state_category).permit(:name, :description)
end
end
Output of rake routes
data_state_category GET /data/state_categories/:id(.:format) data/state_categories#show {:format=>:json}
PATCH /data/state_categories/:id(.:format) data/state_categories#update {:format=>:json}
PUT /data/state_categories/:id(.:format) data/state_categories#update {:format=>:json}
DELETE /data/state_categories/:id(.:format) data/state_categories#destroy {:format=>:json}
Upvotes: 1
Views: 214
Reputation: 13354
Check your routes. I think you'll want to use data_state_category_url
in your jbuilder template
Upvotes: 1