Rails - start your route from second element of the path - ruby-on-rails

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")

Related

Rails routes config thinks action method is an object id

I'm developing rails application and encountered such problem.
I have movies_controller.rb, where I have these actions and routes defined:
Prefix Verb URI Pattern Controller#Action
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
new_movie GET /movies/new(.:format) movies#new
edit_movie GET /movies/:id/edit(.:format) movies#edit
movie GET /movies/:id(.:format) movies#show
PATCH /movies/:id(.:format) movies#update
PUT /movies/:id(.:format) movies#update
DELETE /movies/:id(.:format) movies#destroy
root GET / redirect(301, /movies)
movies_by_director GET /movies/by_director(.:format) movies#by_director
But when I try to go to /movies/by_director?director="something", rails think, that I'm navigating to movies#show action with parameter :id = by_director.
What am I doing wrong?
Routes are matched in the order they are specified so make sure the route for "by_director" is defined above the resource routes for movies.
Something like this should do the trick:
get '/movies/by_director' => 'movies#by_director'
resources :movies
There are two problems in one here:
The default pattern matching for :id is loose enough that by_director is interpreted as an :id.
Routes are matched in order and GET /movies/:id appears before GET
/movies/by_director.
You can manually define GET /movies/by_director before your resources :movie as infused suggests or you could add a constraint to narrow what :ids look like:
resources :movies, constraints: { id: /\d+/ } do
#...
end
Manually ordering the routes is fine if there's just one or two of them to deal with, constraining :id is (IMO) cleaner and less error prone.

How do I use current_page? to reference a mounted engine path?

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) %>

controller#index method showing routing error

i'm new to ruby on rails.
I've set up a resources directive in the routes file.
resources :employees
Which creates the following routes
root / employees#index
employees GET /employees(.:format) employees#index
POST /employees(.:format) employees#create
new_employee GET /employees/new(.:format) employees#new
edit_employee GET /employees/:id/edit(.:format) employees#edit
employee GET /employees/:id(.:format) employees#show
PUT /employees/:id(.:format) employees#update
DELETE /employees/:id(.:format) employees#destroy
But when I put /employees in to the address bar I get
No route matches {:controller=>"employees", :action=>"show"}
Even though /employees should call employees#index not employees#show
Wham am i missing here?
Thanks :)
Maybe you have a link in the layout or template like employee_path (not employees_path).
And you have got this error.

How to pass and receive parameters in url in Ruby on Rails?

I am new to ruby, i would like to pass parameters in the url and receive it in controller.
I am expecting operation like
www.mysite.com/getuser/id/22
where getuser is the param name and 22 is its value.
Please provide me if there is any useful links that i can refer to.
Thanks in advance
Please read everything in the Rails Guide to Routing. There are two main cases there:
RESTful routes: only use GET, PUT, POST, DELETE. Rails maps that by using the method resources. resources :pages will lead to the following routes (and URLs) automatically:
sites GET /sites(.:format) {:action=>"index", :controller=>"sites"}
POST /sites(.:format) {:action=>"create", :controller=>"sites"}
new_site GET /sites/new(.:format) {:action=>"new", :controller=>"sites"}
edit_site GET /sites/:id/edit(.:format) {:action=>"edit", :controller=>"sites"}
site GET /sites/:id(.:format) {:action=>"show", :controller=>"sites"}
PUT /sites/:id(.:format) {:action=>"update", :controller=>"sites"}
DELETE /sites/:id(.:format) {:action=>"destroy", :controller=>"sites"}
Other routes: There is a rich set of methods you can use in your routes file to add additional routes. But keep in mind: If you just want to address a resource, it is better to stick to restful routes. A typical example is_ match ':controller(/:action(/:id))'. This allows URLs like:
localhost:3000/sites/help: controller == SitesController, action == help
localhost:3000/sites/search/something: controller == SitesController, action == search, parameter in params is something under the key id. So inside the action search, you will find params[:id] bound to "something".
In your config/routes.rb file you write:
resources :users
Then you create the controller:
rails g controller users
Inside your controller file you have something(contrived) there like :
def show
my_id_param = params[:id]
end
When you go to:
www.mysite.com/user/22
my_id_param will be 22
I think rails guides are quite good resource, just google for them, has ton of good info there.

Rails: routing question

I have this in my routes:
resources :cvits
which produces these routes:
cvits GET /cvits(.:format) {:controller=>"cvits", :action=>"index"}
POST /cvits(.:format) {:controller=>"cvits", :action=>"create"}
new_cvit GET /cvits/new(.:format) {:controller=>"cvits", :action=>"new"}
edit_cvit GET /cvits/:id/edit(.:format) {:controller=>"cvits", :action=>"edit"}
cvit GET /cvits/:id(.:format) {:controller=>"cvits", :action=>"show"}
PUT /cvits/:id(.:format) {:controller=>"cvits", :action=>"update"}
DELETE /cvits/:id(.:format) {:controller=>"cvits", :action=>"destroy"}
but I would like my urls to be singular (eg /cvit/, /cvit/new, /cvit/:id). What would be the easiest way to change this??????
Thanks!!!!
SOLVED: Figured it out, I did:
resources :cvits, :path => 'cvit'
Well:
resources :cvit
Check doc here: http://guides.rubyonrails.org/routing.html#singular-resources
Or a better fit:
resources :cvits, :path => "cvit"
Same doc page.
You just want a singular resource:
resouce :cvit
# instead of
resources :cvits
Note that your controller names etc. will still be plural (CvitsController). In order to specify otherwise you can pass:
resource :cvit, :controller => 'cvit'
Also, note that when you do this you have no index action. Singular resources assume there's only one thing there, instead of being many.
Assuming that is what you have (a singular resource), this is better than passing the path option. The path option is just overriding the name and not the behavior (i.e. you still have an index, even though that doesn't make sense if you're dealing with a singular resource).

Resources