Rails namespaced routes to wrong Controller - ruby-on-rails

So I'm just beginning with RoR and figured I do a basic blog with API endpoints aswell. The problem is that my api requests seem to be routed to the wrong controller,
I have the following as my routes.rb
Blog::Application.routes.draw do
namespace :api do
namespace :v1 do
resources :articles
end
end
end
I also have controllers/api/v1/articles_controller.rb, which has the following content:
module API
module V1
class ArticlesController < ApplicationController
respond_to :json
def index
respond_with Article.all
end
end
end
end
My logic says that when I hit http://localhost:3000/api/v1/articles, this should be the Controller to respond, however the actual Controller that responds is the one in the root of controllers (controllers/articles_controller.rb) and not the one in the /api/v1 path. When I remove the Controller that actually responds, I'll get uninitialized constant Api::V1::ArticlesController instead.
Even rake routes gives me the expected routes, however actually hitting those endpoints fails. Output of rake routes is the following:
api_v1_articles GET /api/v1/articles(.:format) api/v1/articles#index
POST /api/v1/articles(.:format) api/v1/articles#create
new_api_v1_article GET /api/v1/articles/new(.:format) api/v1/articles#new
edit_api_v1_article GET /api/v1/articles/:id/edit(.:format) api/v1/articles#edit
api_v1_article GET /api/v1/articles/:id(.:format) api/v1/articles#show
PUT /api/v1/articles/:id(.:format) api/v1/articles#update
DELETE /api/v1/articles/:id(.:format) api/v1/articles#destroy
The only similar question I found on SO is nested namespace route going to wrong controller however, there's no accepted answer there and it's been a year. Maybe another attempt will help resolve this issue

Your module is API, but Rails is looking for Api. Ruby's modules are case-sensitive.

Related

Rails: NoMethodError Undefined method

I'm trying to build a basic Rails/React app which takes in a series of user inputs, then displays them on the screen within a letter template (think MadLibs.) I've followed several React tutorials and I have a decent amount of experience with Rails but I've gotten amazingly hung up with an API I've created. For context I'm using this tutorial here as a guide, but am building my own app.
This is my routes file:
Rails.application.routes.draw do
root to: 'site#index'
namespace :api do
namespace :v1 do
resources :letters, only: [:index, :create]
end
end
end
This is my base_controller.rb file:
class Api::V1::BaseController < ApplicationController
respond_to :json
end
and this is my letters_controller.rb file:
class Api::V1::LettersController < Api::V1::BaseController
def index
respond_with Letter.all
end
def create
respond_with :api, :v1, Letter.create(letter_params)
end
private
def letter_params
params.require(:letter).permit(:id, :param_one, :param_two)
end
end
The controller files are both stored in controllers/api/v1 and this is reflected in my routes file. I'm able to use the index method no problem and get all Letter objects to display. I'm able to use the create method somewhat, in that a new Letter is created and saved to the database (sqlite btw.) Problem comes with the response, as in there isn't one. Instead of a response coming back to my AJAX call I'm getting the following:
NoMethodError (undefined method `api_v1_letter_url' for #<Api::V1::LettersController:0x007f3085f1d300>):
app/controllers/api/v1/letters_controller.rb:7:in `create'
I have no earthy idea why this isn't working. From everything that I've read feeding the respond_with call the parameters I have should cause the returned url to work and come back with a response. Instead I get this strange 500.
I've never had so much trouble with a program that it's given me a migraine before. If anyone can help it will be greatly appreciated.
If you run rake routes, you will see that there is no declaration for api_v1_letter_url.
This is because you do not declare a show method on routes.rb (you explicitly only use index and create). If you want the call in your controller to work as-is, you need a show method:
Rails.application.routes.draw do
root to: 'site#index'
namespace :api do
namespace :v1 do
resources :letters, only: [:index, :create, :show]
end
end
end
Now, run rake routes, you'll see it's there.
To implement show in your controller:
def show
respond_with Letter.find(params[:id])
end

Rails controller using wrong method

I'm working on a Rails project that is giving me some problems. I've got a controller characters_controller.rb that has two methods.
class CharactersController < ApplicationController
before_action :authenticate_player!
def view
#character = Character.find(params[:id])
unless #character.player_id == current_player.id
redirect_to :root
end
end
def new
end
end
I've got routes set up for each of those.
get 'characters/:id', to: 'characters#view'
get 'characters/new', to: 'characters#new'
The first route works fine. I can get go to /characters/1 and I'm shown the appropriate view and the requested information. If I visit /characters/new I'm shown an error that references characters#view.
raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
and
app/controllers/characters_controller.rb:6:in `view'
So /characters/new is trying to get a Character from the database with an id of "new" but that doesn't work well. Any idea what I may be doing wrong?
Order matters in routes.rb, the router will find the first route that matches.
In your case, it would never go to characters#new, because the line above it will always match.
A simple solution would be to swap the two lines.
A better solution might be to use resource routing as documented in the Rails routing guide.
Rails parses routes sequentially and therefore it is considering 'new' as the :id for characters/:id route (which encountered first).
Just swap the order of routes as follow:
get 'characters/new', to: 'characters#new'
get 'characters/:id', to: 'characters#view'
If using this order in your routes.rb, for /character/new request, rails will understand that request is handled by view action with paramas[:id] = 'new'
Let characters/new before the other will resolve your problem:
get 'characters/new', to: 'characters#new'
get 'characters/:id', to: 'characters#view'
Try to use resourceful routes, much cleaner:
resources :characters, only: [:new, :show]
Also I suggest rename def view to def show to follow rails convention
Just use
resources :characters, :path => "characters"
in your routes.rb

How are resources connected to controllers without a Model

I am writing a basic application that scrapes data. I have the following in my routes.rb.
Rails.application.routes.draw do
constraints subdomain: 'api' do
namespace :api, path: '/' do
resources :apps, :only => :show
end
end
In controllers I have something like this although I am not sure how are resources connected to Controller.
class AppsController < ApplicationController
def show
puts "this works"
respond_to do |format|
format.json { render json: #user }
end
end
def apps
puts "my app"
end
end
Also, I dont have a Model. Does that mean that in my resources :apps calls a method in AppsController called apps?
If I wanted it t call apps then how's it possible ?
how does a controller in rails know what route does it belong to
I am trying to add a GET /apps?filter=5 that returns my scraped data in the form of JSON and with filter as parameter to that it means that return 5 JSON objects to me
#config/routes.rb
constraints subdomain: 'api' do
namespace :api, path: '/' do
get :apps, to: "apps#apps", on: :collection #-> api.url.com/apps
end
end
A much more coherent way to do it would be...
#config/routes.rb
constraints subdomain: 'api' do
namespace :api, path: '/' do
resources :apps #-> api.url.com/apps -> apps#index
end
end
I think you're getting confused with how Rails works, especially with your data.
I post this all the time, maybe it will help you:
As you can see, your request is not tied to a specific "model", nor is a controller bound to it either. I'll explain the importance of the MVC (Model View Controller) aspect of rails in a minute.
Your thought process is that each request / resource has to have a corresponding model / dataset to pull from. Strictly, this is not the case, although many would believe it to be.
Remember that Rails is just an application framework - it has to work with all the same protocols & restrictions as the other frameworks & languages out there.
When you send a request to Rails (through your browser URL), it takes that request, and matches it to the appropriate controller. This controller action will then pull data from your model (if you've set it up like that), render the view with that data, and return the processed HTML to the browser.
Thus, you don't have to have a model bound to a particular controller action, or anything. You just need to make sure your controllers & views are mapped accordingly.
OOP
I think the part you're getting hooked up on is the object orientated nature of Ruby / Rails.
Although every part of the Rails framework is meant to work with objects, this only applies on a request-basis.
For example, whilst it's typically recommended to keep your controllers resourceful, you don't have to adhere to that methodology if you don't want to. Many newbies don't know the difference.
Thus, when you use the following:
#config/routes.rb
constraints subdomain: 'api' do
namespace :api, path: '/' do
resources :apps, only: :show #-> api.url.com/:id -> apps#show
end
end
... what you're denoting is a controller bound by its resourceful nature. This would typically be expected to use model data, but it's not essential...
In controllers I have something like this although I am not sure how
are resources connected to Controller.
Rails.application.routes.draw provides a DSL which hooks into Rack (the interface between the HTTP server and Rails). It generates rules for where to route the response from Rack.
The DSL is provides has a lot of ways to do the same things. In this example, the resources :apps, :only => :show line basically says you want to generate all of the REST verbs for the AppsController, but you only want the :show verb, so the router will only generate a route to AppsController#show. Note that you can run rake routes to get a list of your routes.
Also, I dont have a Model. Does that mean that in my resources :apps
calls a method in AppsController called apps? If I wanted it t call
apps then how's it possible ?
Models are totally separate abstractions. Once the code reaches your controller you are in plain Ruby land until you return out of that controller action. Models are simply plain Ruby objects with the ability to talk to the database.
In your code if you wanted to call apps from the show method (or action) then you can just call it from there since it's in the same scope.
how does a controller in rails know that ok that is my route. In this case, apps
I'm not sure I understand this question, could you elaborate?
I am trying to add a GET /apps?filter=5 that returns my scraped data in the form of JSON and with filter as parameter to that it means
that return 5 JSON objects to me
For one, you'll need to add a route for /apps. There are several ways you can do this. Here's one approach. I'm going to call it index instead of apps since that's more conventional:
# config/routes.rb
get '/apps' => 'apps#index'
# app/controllers/apps_controller.rb
class AppsController < ApplicationController
respond_to :json
def index
limit = params[:filter].to_i
#users = User.first(limit) # Implement this however you wish
respond_with(#users)
end
end
My syntax might be a little off here with the respond_to and respond_with, but it should explain how the controller routes
Routing simply maps URLs to a controller/action, the existence of a model with the same name does not matter.
To get to the apps action that you defined in the AppsController you need to define a route that maps to apps#apps < This syntax means AppsController, apps action.
An example of a route that would map to the AppsController apps action:
get '/apps', to: "apps#apps"
This is a weird example. It's not conventional to have a def apps action inside AppsController, what exactly are you trying to accomplish with this action?
If you want a rest call to /apps that returns a JSON list of apps, then this all you need to do.
router
resources :apps, only: [:index]
controller
class AppsController < ActionController::Base
def index
puts "This is the index route in AppsController"
end
end
In the router, when you specify resource :apps, only: [:index]. This routes the request GET /apps to AppsController#index

Ruby on Rails : add a new route

I'm new with RoR so this is a newbie question:
if I have a controller users_controller.rb and I add a method foo, shouldn't it create this route?
http://www.localhost:3000/users/foo
because when I did that, I got this error:
Couldn't find User with id=foo
I of course added a view foo.html.erb
EDIT:
I added to routes.rb this code but I get the same error:
resources :users do
get "signup"
end
This doesn't work automatically in rails 3. You'll need to add
resource :users do
get "foo"
end
to your routes.rb
You'll definitely want to have a look at http://guides.rubyonrails.org/routing.html, it explains routing pretty well.
Rails is directing you to the show controller and thinks that you're providing foo as :id param to the show action.
You need to set a route that will be dispatched prior to being matched as /users/:id in users#show
You can accomplish this by modifying config/routes.rb by adding the following to replace your existing resource describing :users
resource :users do
get "foo"
end
Just to add to the other answers, in earlier versions of Rails there used to be a default route
match ':controller(/:action(/:id))(.:format)'
which gave the behaviour you describe where a request of the form controller/action would call the given method on the given controller. This line is still in routes.rb but is commented out by default. You can uncomment it to enable this behaviour but the comment above it explains why this is not recommended:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
At the schema ':controller/:action(.:format)', you can also easily do the following
resources :users do
get "foo", on: :collection
end
or
resources :users do
collection do
get 'foo'
end
end
http://guides.rubyonrails.org/routing.html#adding-collection-routes

adding controllers with a namespace admin as a subfolder

i have a simple cms on ROR 3.2.
with this folder scheme:
app |controllers |my controllers
but i wanted to have an "admin" section where i could have some controllers too.
so i created
rails generate controller admin/Users
app | controllers |admin & my admin controllers
so my file is:
users_controller.rb
class Admin::UsersController < ApplicationController
def index
render(:text => "sou o index!")
end
def list
render(:text => "sou o list")
end
end
On my routes i have:
namespace :admin do
resources :users
end
match ':controller(/:action(/:id))(.:format)'
Im new to rails and i cant figure out the solution. Cant find it anywhere.
The PROBLEM is
i try do acess:
http://localhost:3000/admin/users/list
and i get this error:
Unknown action The action 'show' could not be found for
Admin::UsersController
You seem to not have an understanding of how Rails's RESTful routing works by default. I recommend reading the Resource Routing section of the Rails Guides. By default, when using resources in your routes, the show action is what is used to display a particular model record. You can customize this behavior to an extent in that you can change the URL that for the show action, but not the method name in the model:
resources :users, :path_names => { :new => 'list' }
If you are going to use RESTful routing (which you should), you should remove the default route (match ':controller(/:action(/:id))(.:format)'). Also, you can run rake routes at any time from the terminal to see details about your current routing configuration.
Your on the right track, however, there are a few more steps involved to complete your solution for a backend admin CRUD section. Check out the following example of how to create it yourself:
https://stackoverflow.com/a/15615003/2207480

Resources