It seems there is no URL helper generated automatically when you apply custom.
So below is what is defined in my routes.rb file but when I do rake routes I don't have any URL helper generated at all.
get "events/display_event/:id", :to => "events#display_event"
It seems it is generated only for resources in the routes.
Is there a way to generate this automatically or do I have to manually specify without using the helper?
If it has to be done manually in the view, what is the best approach for this?
This should do it.
get "events/display_event/:id" => "events#display_event", as: :display_event
Related
Couldn't find answer searched everywhere. Maybe I am using rails incorrectly.
I have made a custom route:
get "/posts/:page/category/:query" => 'posts#index', as: :posts_category
As you can see above I have customized in my route a path with a 'category' string within it.
How do I use link_to to address this.
I have tried
link_to posts_category(:page=>1, :query=>3)
This doesn't work because the link created by rails does NOT include the the /category/ within the path. How do I add that in the link_to.
Kind Regards,
Your code works fine (although I'm assuming it was a typo that you wrote posts_category() instead of posts_category_path()), so maybe you have another route defined earlier in your routes.rb with the same helper name, it will be masking this one.
Run rake routes and look for something else with the posts_category helper.
I'm creating an engine that needs to insert some routes into the application's router. For this particular gem, I'd rather not application's routes.rb if possible. Is there a way to insert routes at a particular location in the router via code? I'm looking for an API that does something like:
Rails.application.routes.insert("resources :foos", :before => "some string in routes.rb")
If I create a config/routes.rb inside the engine and define some routes, this kind of works. Rails is smart enough to mix the engine's routes into the application's routes, but it tacks them on at the end of the route list. I need them to appear at the beginning so the engine's routes take priority.
I'm aware that I can namespace the routes by mounting the engine in the application's routes.rb, but this creates a routing structure that I don't really want. I want the engine's routes to look they are a part of the application by defining some routes in the actual application.
I have a workaround which is to add the following to the application's routes.rb.
Rails.application.routes.draw do
MyEngine.setup_routes(self)
#...other routes below
end
MyEngine.setup_routes looks like
def self.setup_routes(map)
map.get 'a_path', :to => 'a_controller#a_path'
end
This at least allows me to control the point where the routes get defined in the application's route list, but the user still has to manually update his routes.rb (or I have to build an installer that does it). It seems like there should be a way to tell rails to tack some routes onto the start of the route list...
I use rails 4 for a restFUL API and would like to use http://apidocjs.com/ as i did with php.
I could generate my api documentation in /doc but after I'm wondering what is the best "rails way" to route to this doc. Should I create a controller or just routes to my html file like :
get '/doc', :to => redirect('/doc/index.html')
I tried it but I get
No route matches [GET] "/doc/index.html"
So what is the best way to do that ? I feel like I don't think "rails way"..
If your documentation is completely generated and just static html, you can simply place it within your public folder and it will be routed automatically. In other words, you can create the docs folder within the public folder and then access your pages via
http://example.com/docs/index.html
In development this would be
http://localhost:3000/docs/index.html
If you're looking for something more robust, I'd highly recommend high_voltage by thoughtbot.
You could try like this provided you have controller named as docs and action named as index.
get 'docs', :to => 'docs#index', :as => 'doc'
resource :doc, only: :show
Then create this file
/app/views/docs/show.html
No controller needed
The URL will be
/doc
I am trying to create a scaffold called 'Pages'.
So far everything is fine but the page structure now needs to change where it currently is:
http://0.0.0.0:3000/pages/the-page-name
What I need to do now is have this instead:
http://0.0.0.0:3000/the-page-name
In my routes.rb I have this:
resources :pages
This obviously maps all routes within the model to this base but I want to hide this.
Is it entirely possible?
Thanks,
Taken from the Rails Routing Guide, you could do this:
get '*pages', to: 'pages#show', format: false
I would recommend you make it the very last route you have, since the Rails router matches the request with the first route, and having a wildcard early in your routes file will end up clobbering all your other routes/resources.
You can specify a path
resources :pages, path: ''
I have a resource in a Rails 3.2 app called "news". I know it is not the best name to use for a resource but I'd prefer not to change it.
I have an issue with the routing since new_news_path doesn't work.
How can I define a custom route for the new action? something like unused_news_path for news#new?
thanks for your help.
Try this in your routes.rb:
match "unused" => "news#new"
When you do a rake routes | grep unused, you will get the following output:
unused /unused(.:format) news#new
So you now can use unused_path on your views and controller to get to the correspondent action.