I can't serve static images on Ruby on Rails - ruby-on-rails

I'm trying to access:
http://localhost:3000/images/Header.png
but I keep getting this error:
Routing Error
No route matches "/images/Header.png" with {:method=>:get}
And here are my routes:
ActionController::Routing::Routes.draw do |map|
map.resources :worker_antpile_statuses
map.resources :worker_antpiles
map.resources :antcolonies
map.resources :antpiles
map.resources :clients
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

Static assets need to go in the /public folder or else you'll get the routing error. For a static image at http://localhost:3000/images/Header.png you want to place Header.png in RAILS_ROOT/public/images
When given any url, Rails will check for a file existing at the path from RAILS_ROOT/public directory before attempting to match any routes.
For example when a Rails server receives a request for http://localhost:3000/users/1 it will attempt to send the contents of RAILS_ROOT/public/users/1. If no file exists, the route matching routine kicks in.

The last two lines of your routes.rb are catch-all routes that will match any previously unmatched URLs.
Comment out those two lines and it will work, which is what you want to do when you use RESTful routing in all your application.
See this excellent guide for all you need to know about Rails routes.

Related

Rails Routes missing prefix for resources post route

I created a new controller and actions for a new model. I have setup the rails routes like below:
namespace :api do
namespace :v1 do
resources :universes
end
end
I am trying to write up some RSpec tests using the path helpers (ex. get(api_v1_universe_path(universe.id))). When I go to look at the post route for invites the route is missing the prefix as shown here:
POST /api/v1/universes(.:format) api/v1/universes#create
new_api_v1_universe GET /api/v1/universes/new(.:format) api/v1/universes#new
edit_api_v1_universe GET /api/v1/universes/:id/edit(.:format) api/v1/universes#edit
api_v1_universe GET /api/v1/universes/:id(.:format) api/v1/universes#show
I know I can manually add a line like post "/", to: "universe#create", as: "create" to get a route with a prefix. However, I wanted to know what was causing prefix to be missing from the post route so I can possibly fix that instead?

Rails Route? How To Rename URL in Routes

I would like to rename my categories URL from https://website.com/categories/apples (#1) to
https://website.com/hi-apples-bye (#2)
I am trying to accomplish two things:
Display the #2 URL in the SITEMAP and PATH
Routes:
resources :categories
get '/hi-:id-bye', to: "categories#show"
However, I get URL https://website.com/categories/apples in the sitemap and https://website.com/hi-apples-bye for the path.
Any help would be appreciated! I am a rookie...
Not sure if you need all category resource routes to have the same route prefix but for the example you have given you could just have:
resources :categories # this is to keep the existing REST routes
# outside categories do ... end
get '/hi-:id-bye', to: "categories#show" # this is to add the route you want
See this section for more details: https://guides.rubyonrails.org/routing.html#non-resourceful-routes

Getting no route matches on the show action

I'm trying to use the path from the following route, here is what it's like in rake routes
chapter GET /chapters/:id(.:format) {:action=>"show", :controller=>"chapters"}
chapter_path creates a link to /chapters/x which is correct but I get the routing error when trying to access it.
No route matches {:controller=>"chapters"}`
this is my routes (I am using shallow routing to create a books_chapters and book_chapters_new paths.
resources :books do
resources :chapters, :shallow => true
end
when I test the route with rake routes, I get books_chapters, books_chapters_new, chapters and books, so I don't know what's wrong.
when i remove :shallow => true, i can access /books/1/chapters/6 but I just want it to be /chapters/6
this is what my terminal looks like
so /chapters/id and /chapters/id/edit should be working fine.
I've restarted the server with touch tmp/restart.txt and ran rails s to see if the routes worked there too and rake routes is giving me acceptable routes, but they don't work for me.
Are you supplying the parameter for the path helper, something like
chapter_path(#chapter)
I couldn't figure out how to get :shallow routes to work, and there isn't an example on how to use :shallow in the rails guide so, instead I have to just use nested routes like so
resources :books do
resources :chapters
end
now this means the something like chapters_url or chapters_path won't work.
So I have to do something like this everywhere
book_chapter_url(#chapter.book, #chapter)
or
edit_book_chapter_path(#chapter.book, #chapter)
It works but there's a bit of code smell because I use #chapter twice and the whole url should be able to resolve just through the chapter id instead.

Short-hand routes and redirections with Ruby on Rails

I have some resources in my routes file:
resources :snippets
resources :pastes
Users can access a snippet like this:
/snippets/72384
/pastes/ac6Xs28
As a short-hand, for tweets, IRC, etc… I want /s/<id> to be redirected to /snippets/<id>, and /p/<id> to /pastes/<id>. I want this redirection to use the Location HTTP header and the 301 Moved Permanently HTTP status code.
How am I about to do this? Thanks.
I'm using Rails 3 with WEBrick
You can do it in routes.rb
match "/s/:id", :to => redirect("/snippets/%{id}")
Here's a nice overview of routing in Rails3.

How to remove controller name in REST design in Rails3?

Given a User resource, it goes like this
/user/:shortname
But how can the controller name be removed to get just
/:shortname
How can I declare this in routes.rb while keeping all CRUD functionality instant?
Updated: After reading this I'm moving to Sinatra over Rails to handle this API-like design better.
Define a custom match:
match ':shortname' => 'users#action'
Replace action in users#action with the name of the action that is supposed to receive the request. Just remember to place it in the appropriate order in your routes file. Rails looks at each line of your routes file starting at the top and selects the first matching route. ':shortname' would match any first-level path, including /users! So put it below any routes using a first-level path, which would include all of your resource routes. Here's an example:
resources :users
resources :posts
match '/blog' => 'posts#index'
match ':shortname' => 'users#action'
In routes, you should be able to do something like
resource :users, :path => '/:shortname'
Try that out and rake routes to see if that comes out as expected.

Resources