Routing to another html.erb file (Ruby on Rails) - ruby-on-rails

I am learning how to login from Rails and I wanted to know one thing:
I have several files which I want to show when a certain condition is met, in this case logging in will redirect me to another file called starter.html.erb
I am trying to redirect it through both the controller and the routes files and I get the following error:
No route matches [GET] "/app/views/usuarios/starter.html.erb"
Can you please tell me what I'm doing wrong? Thanks!
Controller portion:
redirect_to search_starter_path
routes.rb portion:
get "/search/starter" => redirect("/app/views/usuarios/starter.html.erb")

You have to use the controller#action syntax to redirect. Example: if your controller is usuarios_controller.rb you should have inside an action named starter. Then in your routes.rb put this entry:
get "search/starter" => 'usuarios#starter'

Related

How to handle routing error in Rails 4 with alchemy cms gem?

So in our Rails 4.2 application, there is the alchemy_cms gem which requires its routes to be mounted last in config/routes.rb.
SampleApp::Application.routes.draw do
#other routes for the rails app here
# :
# :
mount Alchemy::Engine => '/'
end
We get routes like "/somehacker/routingurl" which then falls out to the Alchemy::Engine to handle, resulting in a default 500 error. If I wanted to do a custom 404 error, then the proper way to do it is to build a custom 404 page and have Alchemy handle the redirect? Otherwise since the Alchemy docs specify that it has to be the last route in config/routes.rb, there won't be a way to add a catchall route to redirect to some kind of error page I assume.
EDIT:
One of the problems is that there are some routes that are like the invalid "somehacker" route above that do need to be parsed by the Alchemy routing engine, such as "/en/us" where "en" is a valid locale. This is why I initially thought to put the route handling in the Alchemy engine's routes file.
If it is difficult for you to configure and use the Alchemy cms gem to redirect unknown routes into a custom defined page, you can use the bellow method to implement the same with a small coding tweak given bellow:
Rails 4.XXX
1. First Method.
(routes.rb)
You can still use a simple get to redirect all unknown routes.
get '*path', to: 'home#index'
If you wish to provide routing to both POST and GET requests you can still use match, but Rails wants you to specify the request method via via.
match "*path" => "home#index", via: [:get, :post]
Remember that routes.rb is executed sequentially (matching the first route that fits the supplied path structure), so put wildcard catching at the bottom of your matchings.
Here you can replace the home#index with any custom path that you defined in you application, also note that it is important to keep this redirection code only at the bottom of routes.rb.
2. You can follow the bellow tutorial on the same problem in a different perspective to solve can be found.
Custom 404 error page with Rails 4

No Route Matches for a very basic Rails4 App

I'm very new to RoR and I'm trying to get a very basic site going. I have my home page working okay, but when I try to add a new page, I keep getting "No route matches" in the log. Here is the output from rake routes:
Prefix Verb URI Pattern Controller#Action
inventing_index GET /inventing/index(.:format) inventing#index
ideas_index GET /ideas/index(.:format) ideas#index
root GET / ideas#index
However, when I go to mysite.com/inventing or mysite.com/inventing/index I get the no route matches error. mysite.com/ shows the app/views/ideas.erb as hoped. All I did was rails generate controller inventing index. Is there something else I have to do to activate the route?
I'm running ruby 2.0.0p247 and rails 4.0.0 with passenger/apache on centos 6. I installed all the ruby/rails/passenger stuff, so its possible something isn't setup properly.
Thanks
EDIT: Here is my routes.db file:
Rortest::Application.routes.draw do
get "inventing/index"
get "ideas/index"
root to: 'ideas#index'
end
tldr: Your problem is probably the route. Change get 'inventing/index' to get 'inventing/index'=> 'inventings#index' to correctly point the route to your controller action.
There are four things you need to do when adding a new page/route. I usually do them in this order for static pages:
1) Create a controller with the appropriate action for each page
You already did this with rails generate controller inventing index, but make sure that:
In your app/controllers folder, you do indeed have a file called inventings_controller.rb
In inventings_controller.rb, you have at least this:
class InventingsController < ApplicationController
def index
end
end
2) Create a view for each controller action
Make sure that:
In your app/views/inventings folder, you have a file called index.html.erb
Puts some simple HTML in that file, like <h1>Testing123</h1>, just so you can see if it's working.
3) Add a route for each controller action
It looks like this may be the problem. In your routes.rb file, you'll need this:
get 'inventing/index' => 'inventings#index'
Your root to works because it's actually pointing directly to your controller action, but rails isn't smart enough to guess that inventing/index should go to the index action in your inventing**s** controller.
4) As others have suggested, restart your rails app
You can do this with ctrl+c at the command line, then rails s again to start it back up.

Rails routing error when using resource but now when using GET

I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)
"Routing Error
No route matches [GET] "/suggestion-boxes"
In my routes.rb file I have:
SuggestionBoxApp::Application.routes.draw do
resources :suggestion_boxes
end
This is what I get when I run rake routes:
suggestion-box-app$ rake routes
suggestion_boxes GET /suggestion_boxes(.:format) suggestion_boxes#index
POST /suggestion_boxes(.:format) suggestion_boxes#create
new_suggestion_box GET /suggestion_boxes/new(.:format) suggestion_boxes#new
edit_suggestion_box GET /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
suggestion_box GET /suggestion_boxes/:id(.:format) suggestion_boxes#show
PUT /suggestion_boxes/:id(.:format) suggestion_boxes#update
DELETE /suggestion_boxes/:id(.:format) suggestion_boxes#destroy
However, if I modify my routes file to
SuggestionBoxApp::Application.routes.draw do
get "suggestion-boxes" => "suggestion_boxes#index"
end
Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.
I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.
Any insights would be very much appreciated.
The error is that you are not renaming the REST route, instead the controller one.
Try declaring
resources :suggestion_boxes, :as => "suggestion-boxes"
in your config/routes.rb file.
Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding

Rails 3 - Made a new controller action and view but cant route it

I'm using Rails 3
I have a controller called subscriber_controller.rb
I just added an action called "def houston" and added this to my routes file
match '/houston' => 'subscriber#houston'
get "subscriber/houston"
but whenever I try to go to "http://localhost:3000/houston" which was the goal i get the error
Routing Error
No route matches "/houston"
What else do I need to add to the routes file to find the correct action in the controller and have the url work as http://localhost:3000/houston ?
What does "rake routes" give you?
I tried the same thing and it worked.
Check that you saved your routes file.

Newbie Question:Have to type localhost/controller/index in browser to get index method to display in Rails. Why?

I am new to Rails and I am reading Sitepoint's Simply Rails 2 but I am using Rails 3.0.0. In the book we had just set up our first controller (StoriesController) and fired up the server and typed in http://localhost:3000/stories and it should have displayed the index.html.erb file but instead when I type in that url I get "Routing Error: No route matches "/stories" but when I type in http://localhost:3000/stories/index it works properly. Can somebody explain to me why rails is not loading the index.html.erb file implicitly when I go to localhost/stories?
Depending on how you created your routes (in config\routes.rb). Unfortunately, if you scaffold a controller, rails now generates a route like this:
get 'posts#index'
If it is a restful-controller, you better write
resources :posts
Or if it is a special controller (with only an index action) you could write
match '/posts' => 'posts#index'
To provide the fallback match ':controller(/action(/:id(.:format))) is generally avoided. Because it opens up all your controller-methods. The preferred way is that you declare explicitly how to access your site.

Resources