I got a strange issue
this is my routes.rb
get '/fin', to: 'pages#fin'
mount S::Engine => '/pod'
S::Engine.routes.draw do
resources :articles, only: [:show, :index], path: 'posts'
root 'articles#index'
not let say I go to my root path of the S engine. inside the view I'm trying to put a link to '/fin' so I write
<%= link_to "Fin", fin_path %>
But I get an unknown route error.
I also tried to use the controller:'/pages' to solve the controller resolving, but it didn't help
like this: url_for controller:'/pages',action:"fin"
The route exists, and the problem is related somehow to the scoping.
thanks,
Try: get 'fin', to: 'pages#fin' and then try to run rake routes in your terminal to see if fin_path is there. Also what do you have in your fin method in your pages controller?
Related
Can anyone point me to info on how the name of the params object is generated?
I just spent far too long trying to access params[:venue_search] following a form submission, only to find I needed to be referring instead to params[:searches_venue_search] presumably because I've organised the venue_search resource into a "search" folder and/or because of the preceding "search" directory in the URL that I've specified in the routes.
What's the logic behind this, anyone?
The routes:
resources :venue_searches, controller: 'searches/venue_searches', model: 'searches/venue_search', only: [:create, :new], path: "/search/venues"
match "search/venues/show", to: "searches/venue_searches#show", as: :venue_search
Cheers!
get "search/venues/show", to: "search/venue_searches#show", as: :venue_search
This will generate following route
venue_search GET /search/venues/show(.:format) search/venue_searches#show
If I have a document called 'letter_1.html' stored in 'static_pages/letters/letter_1.html', how do I correctly do routing and use named path notation to link?
Below is my code in routes.rb:
match '/letters/letter_1', to: 'static_pages/letters#letter_1', via: 'get'
If I want to call it I tried the following:
<%= link_to "Letter", letter_1_path %>
My guess is that 'letter_1_path' is the problem because of the two underlines. But what is the correct syntax to fix it? Also, I'm not sure if routing is done correctly with the subfolder.
Thanks!
You have to actually name the route:
match '/letters/letter_1', to: 'static_pages/letters#letter_1', via: 'get',
as: :letter_1
Then you can use letter_1_path and letter_1_url
There is also the rails 4 way:
namespace :static_pages do
resources :letters, only: [] do
collection do
get :letter_1
end
end
end
Which will give you letter_1_static_pages_letters.
Checkout the guides http://guides.rubyonrails.org/routing.html
This is my first Rails project, I am trying to piece things together slowly.
When I'm trying to view the page I generated using rails g controller <controller> <page>, I find myself going to 0.0.0.0:3000/controller/page.html, How can I configure it so that my route file globally allows viewing the page via the page name, rather than controller/page, if no such way exists, then how can I route controller/page.html to /page.html
I've looked around, and haven't really found any explanation, maybe I'm looking in the wrong places?
In config/routes.rb:
get '/page' => 'controller#action'
If your controller is:
class UsersController < ApplicationController
def something
end
end
Then config/routes.rb would be:
get '/page' => 'users#something'
For static pages you could want to use public folder though, everything you put there is directly accessible, for example public/qqqqqq.html would be accessed in localhost:3000/qqqqqq.html
We've just achieved this by using the path argument in resources method:
#config/routes.rb
resources :controller, path: ""
For you specifically, you'll want to make something like this:
#config/routes.rb
resources :static_pages, path: "", only: [:index]
get :page
get :other_page
end
#app/controllers/your_controller.rb
def page
end
def other_page
end
This will give you routes without the controller name. You'll have to define this at the end of your routes (so other paths come first)
Obviously this will form part of a wider routes file, so if it doesn't work straight up, we can refactor!
It sounds like this is a static page, so you can do as juanpastas says, or another option is to create a folder under your app/views directory to hold these pages. Maybe something like
app/views/static_pages/the_page.html.erb
Then in your config/routes.rb you can add:
match '/your_page_name', to: 'static_pages#the_page', via: :get
I am looking to create a route to a controller but I want to not have the name of that controller stipulated in the URL.
So the url would be something like this.
www.blabla.com/about_us.html
I don't want a about_us controller, instead I want a MainController and a about_us action.
I've read the routes from the inside out, but none of the instances indicated how to do this.
Does anyone know how to match a controller-less route to a specific controller?
You can see an example of something similar in the Routing guide:
http://guides.rubyonrails.org/routing.html#naming-routes
get 'exit', to: 'sessions#destroy', as: :logout
You could use:
get 'about_us', to: 'main#about_us'
you can use "path": option
resources :main, path: "/", only: [] do
get :about_us
end
Now, all actions in main controller will be accessed directly without controller name in url
eg: localhost:3000/about_us
Try:
resources :main,:path => '',:only => []
Now all actions of MainController can be accessed without controller name main in the url.
match 'about_us', :to => 'MainController#about_us'
I settled on this...
MainController.action_methods.each{ |a| a = a.to_s; match a => 'main#'+a}
Private actions are not accessible, but all others are
I'm writing a simple Rails app with one main controller and I want to map /(:action(:id)) to this controller (basically remove the controller prefix at the beginning), but still have the path helpers I get by using map.resources.
I tried map.root :controller => 'notes', but I get an error:
undefined method `note_path' for #<ActionView::Base:0x102038b50>
where I use the link_to_unless_current function in my view.
Edit: Here is the code in index.html.erb that gives the error.
<% for note in category.notes %>
<h3><%= link_to_unless_current h(numbered_title note), note %></h3>
<% end %>
UPDATE: I don't know how I came to this question, but author just pointed out that this is 3 years old, which I totally missed. I will leave this answer if somebody needs that behaviour in rails 3. In rails 2 it is not valid...
Route root will not work with whole controller as this has to point on specific action.
First parameter of resources will be used to determine path (normally it would be /notes) and at the same time to create helpers like notes_path. What you want to do is set this to '/', but also add :as option to give proper helpers names. So finally it should look like:
resources '/', controller: :notes, as: :notes
Also quite important thing to notice, if you want to use any other resources, you should put them above notes route. Otherwise rails will recognize resources name as id of notes action show.
Example:
resources '/', controller: :notes, as: :notes
resources :comments
Going to /comments will try to find note with id 'comments'.
resources :comments
resources '/', controller: :notes, as: :notes
Opening /comments will go to comments_controller#index.