How do I use current_page? to reference a mounted engine path? - ruby-on-rails

I use :class =>('active' if current_page?desired_path) to add a selected tag to navigation links if we're looking at that current page:
<%= link_to "Portfolio", portfolio_path, :class =>('active' if current_page?portfolio_path) %>
I want this functionality to work with a page that points to a mounted engine. From my routes.rb:
mount Blogit::Engine => "/blog"
I want to do something like this:
<%= link_to "Blog", blogit_path, :class =>('active' if current_page?blogit_path) %>
. . . but apparently blogit_path isn't the appropriate path as my class is never set to active. I have also tried current_page?('/blog') since I'm at the url http://myapp.com/blog - but even that doesn't work.
Note that current_page?blog_root_path works as long as I'm within the blog engine's path. I get errors if I'm outside of the engine.
How do I use current_page to reference a mounted engine path?
Here are the associated rake routes results:
. . .
blogit /blog Blogit::Engine
root GET / home#index
portfolio_index GET /portfolio(.:format) portfolio#index
portfolio GET /portfolio(.:format) portfolio#index
Routes for Blogit::Engine:
GET /posts/page/:page(.:format) blogit/posts#index
post_comments POST /posts/:post_id/comments(.:format) blogit/comments#create
post_comment DELETE /posts/:post_id/comments/:id(.:format) blogit/comments#destroy
posts GET /posts(.:format) blogit/posts#index
POST /posts(.:format) blogit/posts#create
new_post GET /posts/new(.:format) blogit/posts#new
edit_post GET /posts/:id/edit(.:format) blogit/posts#edit
post GET /posts/:id(.:format) blogit/posts#show
PATCH /posts/:id(.:format) blogit/posts#update
PUT /posts/:id(.:format) blogit/posts#update
DELETE /posts/:id(.:format) blogit/posts#destroy
blog_root GET / blogit/posts#index

I have limited knowledge of mountable engines. I have been recently looking at this railscast though and it may help.
Following that, it could be that you need to pass in an as option to the mountable engine in your routes file, eg:
mount Blogit::Engine => "/blog", as: "blogit_engine"
Then, when in your main app, your path helper for blogit root should be:
blogit_engine.root_url
Which would lead, in your case to:
<%= link_to "Blog", blogit_path, :class =>('active' if current_page?blogit_engine.root_url) %>

Related

Rails - start your route from second element of the path

Is there a way in rails to start routing from 2nd part of the url path?
for example localhost:3000/tenant_name/posts for resources:posts
tenant_name is a name of the schema in my database.i want switch to respective tenant using the tenant_name.
when i run this now will get No route matches [GET] "/tenant_name/posts"
I need to visit posts even if replace "tenant_name"with any tenant_name.How to do?
Using scope without any options and only a scope name, it will just change the resources path.
scope :sometext_here do
resources :posts
end
This will generate url like -
Prefix Verb URI Pattern Controller#Action
posts GET /sometext_here/posts(.:format) posts#index
POST /sometext_here/posts(.:format) posts#create
post GET /sometext_here/posts/:id(.:format) posts#show
PATCH /sometext_here/posts/:id(.:format) posts#update
PUT /sometext_here/posts/:id(.:format) posts#update
DELETE /sometext_here/posts/:id(.:format) posts#destroy
Alternative way to use it -
get '/:dynamic_text/posts' => 'posts#index', as: :all_posts
So it can be used as
all_posts_path(dynamic_text: "sometext_here")

Routing error in R

I'm new to Ruby and Ruby on rails and have hit a problem with routing. I have 3 controllers, application controller, a bike controller and a ride controller. My routing table is as follows:
Rails.application.routes.draw do
get 'cycle_tracker/index'
resources :rides
resources :bikes
root 'cycle_tracker#index'
When I run rails routes it brings the following:
Prefix Verb URI Pattern Controller#Action
cycle_tracker_index GET /cycle_tracker/index(.:format) cycle_tracker#index
rides GET /rides(.:format) rides#index
POST /rides(.:format) rides#create
new_ride GET /rides/new(.:format) rides#new
edit_ride GET /rides/:id/edit(.:format) rides#edit
ride GET /rides/:id(.:format) rides#show
PATCH /rides/:id(.:format) rides#update
PUT /rides/:id(.:format) rides#update
DELETE /rides/:id(.:format) rides#destroy
bikes GET /bikes(.:format) bikes#index
POST /bikes(.:format) bikes#create
new_bike GET /bikes/new(.:format) bikes#new
edit_bike GET /bikes/:id/edit(.:format) bikes#edit
bike GET /bikes/:id(.:format) bikes#show
PATCH /bikes/:id(.:format) bikes#update
PUT /bikes/:id(.:format) bikes#update
DELETE /bikes/:id(.:format) bikes#destroy
root GET / cycle_tracker#index
In my main view I have the following (I'm simply trying to create a link from my main page to rides/new.
<%= link_to 'rides', :controller => new_ride_path %>
If I try and access http://127.0.0.1:3000/rides/new then it works as expected. However, if I simply try and access http://127.0.0.1:3000 then I get the following:
showing D:/Dev/CycleTracker/app/views/cycle_tracker/index.html.erb where line #2 raised:
No route matches {:action=>"index", :controller=>"rides/new"}
If I try to use new_ride_url instead of path I get the following:
Showing D:/Dev/CycleTracker/app/views/cycle_tracker/index.html.erb where line #2 raised:
No route matches {:action=>"index", :controller=>"http://127.0.0.1:3000/rides/new"}
I imagine this is probably a fairly straightforward issue, but any help appreciated.
<%= link_to 'rides', new_ride_path %>
Try that - you don't need the :controller => part
if you want to use the controller-action style you need to do this:
<%= link_to "Rides" , controller: "rides", action: "new" %>

Which route matches a fixed controller with variable action?

I have this controller called activities, which contains methods, such as updateweight, displaycalories, etc.
<ul>
<li><%= link_to "Home", "/" %></li>
<li><%= link_to "Update Weight", "/activities/upweight" %></li>
<li><%= link_to "Food Ressources", "/activities/food_res" %></li>
<li><%= link_to "Exercises Technics", "/activities/exercises" %></li>
</ul>
Which route would be able to get to the activities controller and to the method with the name that comes after? I've tried ressources :activities, but I'm getting bizarre results.
Thanks for helping
Handling any arbitrary action in a controller is non-RESTful. Much older rails versions handled this like:
match ':controller(/:action(/:id))(.:format)'
.. and in fact that's still possible with Rails today. You should be careful not to mix RESTful and arbitrary actions in a single controller since actions like 'create' and 'delete' should not respond to GET requests.
Alternatively you could promote 'weight', 'foods' and 'exercises' to their own resources.
Also, you apparently can't spell 'resources' or 'techniques' though I rather like the image of exercising one's technics.
To find the available routes, utilize the command rake routes from command line of your rails app directory. This will show you a list of the potential routes you can use as links. They will typically look like:
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
etc...
In your case though, you are making non-default url's, so you need to add them to your config/routes.rb file. So for example add:
get '/activities/upweight' => 'activities#upweight'
This would connect the chosen url to the correct controller#action you desire using a GET http request.
Also note, if you've created your routes for the activities controller in routes.rb using the line resources :activities, then you need to make sure to add the custom routing line above this. resources :activities serves as a catch-all for custom urls, and will route them to the controller's show method.
In your case the route can look like these:
match "/activities/upweight", :as => "activities#upweight"
match "/activities/food_res", :as => "activities#food_res"
match "/activities/exercises", :as => "activities#exercises"
Provided that you have method named as upweight, food_res, exercises

Rails 3 routing

I have a weird problem.
I was using rails with scaffold to create CRUD and stuff.
Then I added a function called cnt that should give me the count of table entries.
For example Albums.count for a cipher.
But this morning, the whole routing was directing to that page.
When I tried to visit xxx/elements
I was linked do /cntalbums.
Looked like a routing thing - this is my routes.rb:
Something::Application.routes.draw do
resources :albums
get "home/index"
resources :elements
root :to => 'home#index'
end
That's all.
I deleted the view of cnt, I deleted the method, which was locaten in albums_controller.
So there is no friggn spot left, where cnt is implemented.
But still it seems like every GET links to that page.
I got this one in my logs:
Started GET "/albums" for xxxx at Fri Oct 05 09:54:42 +0200 2012
Processing by AlbumsController#cnt as HTML
Parameters: {"cntalbums"=>"albums"}
Completed 500 Internal Server Error in 47ms
ActionView::MissingTemplate (Missing template albums/cnt, application/cnt with {:formats=>[:html], :locale=>[:en], :handlers=>[:erb, :builder]}. Searched in:
* "/kunden/nnax.de/webseiten/entwicklung/poezy/app/views"
):
app/controllers/albums_controller.rb:18:in `cnt'
I had:
match ':cntalbums' => 'albums#cnt', :as => 'cnt_albums', :via => :get
Before; but I deleted that already.
So I don't have ANY clue, why I cannot see my albums, elements and so on.
Finally: rake routes gives me that:
albums GET /albums(.:format) albums#index
POST /albums(.:format) albums#create
new_album GET /albums/new(.:format) albums#new
edit_album GET /albums/:id/edit(.:format) albums#edit
album GET /albums/:id(.:format) albums#show
PUT /albums/:id(.:format) albums#update
DELETE /albums/:id(.:format) albums#destroy
elements GET /elements(.:format) elements#index
POST /elements(.:format) elements#create
new_element GET /elements/new(.:format) elements#new
edit_element GET /elements/:id/edit(.:format) elements#edit
element GET /elements/:id(.:format) elements#show
PUT /elements/:id(.:format) elements#update
DELETE /elements/:id(.:format) elements#destroy
home_index GET /home/index(.:format) home#index
root / home#index
so for my understanding it is clear; I wanna go to albums or elements and it enters the function of the fitting controller.
But it ALWAYS tries to reach AlbumsController#cnt
Although that function does not even exist anymore
ANY hints ? :/
Rails routes are first-come-first-routed, so if the CNT rule was above everything else, Rails will route that. Looking at the rule
match ':cntalbums' => 'albums#cnt', :as => 'cnt_albums', :via => :get
notice that any URL will match this. The path /hello/world will result in :cntalbums => "hello/world".
If you've deleted the line from your routes but are still being routed by it, are you sure that you've restarted your Rails server? (maybe even try clearing your browser's cache, but that really shouldn't be the problem)
try this
Something::Application.routes.draw do
resources :albums
...
match ':cntalbums' => 'albums#cnt', :as => 'cnt_albums', :via => :get
...
get "home/index"
resources :elements
root :to => 'home#index'
end
because rails routes are first come first serve

Nested Route link_to helper working in one place but not another

I have a nested routes problem that I can not figure out. I have an app that has nested routes like so:
resources :events do
resources :sessions
end
I am trying to use the following link_to in my code:
<%= link_to "New Session", new_event_session_path %>
When I run rake routes, it will show the proper URL with a GET method existing:
new_event_session GET /events/:event_id/sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
When I try to use the link_to in one place it works, when I try to use it in another place it does not, it instead will give me this error:
No route matches {:controller=>"sessions", :action=>"new"}
The only difference between the two files is the location of the files in the app (one is under views/events the other is under views/sessions and the url being called:
/events/1 --vs-- /events/1/sessions
I am still a noob with rails, so this is probably a stupid question, but I have hit a bit of a wall. Any help is appreciated.
You just need to pass event object to path helper:
new_event_session_path(#event)

Resources