I have a route in my rails application:
get 'welcome/usertypeone' => 'welcome#usertypeone'
This doesnot look good in the browser as the URL comes out like:
www.mywebsite/welcome/usertypeone
Any way I could change the way the url looks to something like:
www.mywebsite/welcome/teacher
without needing to change the route itself as i would need to change it in loads of places. Just seeing if there is a better solution to this.
If you're explicitly defining the route (as you are, as opposed to using resource etc) you can configure the route as you'd like.
get 'welcome/teacher' => 'welcome#usertypeone'
This will make www.mywebsite.com/welcome/teacher route to the same controller and action.
You will, however, need to update the route throughout your application from welcome_usertypeone_path to welcome_teacher_path. Your text editor probably has a search and replace function making this a 10 second step.
Maybe what you want looks like this:
get 'welcome/:user_type' => 'welcome#usertypeone'
This will take whatever is after the welcome/ and put it in params as params[:user_type], but it will still go to the WelcomesController and the usertypeone action.
You can go to your routes.rb file and try:
get 'welcome/teacher' => 'welcome#usertypeone'
Then you should get the url you require: www.mywebsite/welcome/teacher
Related
I have a model 'Item'. It all works fine, however am not completely satisfied with its show path. I want to use certain parameters from items table to construct a more SEO friendly url. So here is my question..
How can I change my Show action url
from
'mysite.com/items/1'
to
'mysite.com/items/item-name/item-city/item-id' where item-name, item-city, and item-id are dynamic for each specific item.
Is it possible to achieve this without a gem? if yes, how? If no, which gem would you suggest to achieve this in simplest way?
Thanks
One approach to this problem is to use route globbing:
http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
You should be able to do something like this:
get 'items/*names/:id', to: 'items#show', as: :item_names_path
And put whatever you want in *names. This would take a little experimentation to get it right. I might add a method to item to create the names array.
def names
[city.name, name].uniq.compact
end
Then I believe you would call item_names_path(#item.names, #item)
You can do something relatively simple and stays true to Rails by adding a to_param method to your model like so:
def to_param
"#{id}-#{name}-#{city.name}"
end
What this does is every time you use a method like the item_path, it will use the #item.to_param method (this is what it does now, and returns :id). Generating the normal route, but replacing the :id param with the SEO friendly one.
And, on the other end, when you go to find(params[:id]) in the controller in your show, edit, delete, or update actions, it will to a to_i on it and turn it back into an id. This is what it does now, but to_i on an int is still an int.
Your urls will look something like
/items/56-boston-itemname
The other benefit to this, if you happen to change the item name or the city name, it will change all the urls appropriately, but old urls that were sent in email will still work.
I am having trouble getting rid of a period inside of my url. I've look up others solution to this problem but either of them were for the index action.
Here is what a url looks like
/shared_songs.32 #current url structure
/shared_songs/32 #would like this format
Here is what is inside of my routes.rb
get 'shared_songs/:note_id' => "shared_songs#show" #works fine
get 'shared_songs', to: "shared_songs#index", as: "shared_songs" #/shared_songs.32
Inside of my index.html.erb file I currently have
link_to song.name, shared_songs_path(song)
Any idea how to resolve this problem?
The reason this is happening is because you are taking a url helper that doesn't have any dynamic segments (:id, :user_id etc.) in the path, but you're giving it a value anyway (song). Not knowing what else to do with it, rails uses that value as the format, which is why you end up with /shared_songs/32
shared_song_path(song) doesn't work because you don't current have a route called shared_song. As several of the comments say, by far the easiest way is to do
resources :shared_songs
This will give you a functioning shared_songs_path (for the index, doesn't expect any arguments_ and shared_song_path (requires a parameter). You'll have to change your controller slightly because the the id of the song will be in params[:id] instead of params[:note_id]
Instead of:
link_to song.name, shared_songs_path(song)
Do:
link_to song.name, shared_song_path(song)
song, not songs
It might help if you define your routes in a RESTful manner: something like resources :shared_songs. As explained much more clearly in the Rails docs, using the resources helper will automatically set up appropriate routes to the corresponding controller actions.
I have 2 questions:
I have a controller called homepage. I have a view called samplegraph in my homepage's view directory. I want to get the routing working correctly such that www.homepage.com/samplegraph takes me to the samplegraph page.
As far as I can tell, the route for it in routes.rb should be something like this:
GET 'homepage/samplegraph' => 'homepage#showgraph1'
If I'm understanding rails routing correctly, this statement routes GET requests to homepage/samplegraph to the homepage controller's showgraph1 action. At this point I'm not particularly sure what the showgraph1 action should be in order to render the view page(samplegraph). At the moment the action is simply empty. I don't really know what to put here.
Second question:
Also, while I was researching rails routing, I was looking into resource based routing. For my purposes, I don't need most of the stuff generated by that. One thing I am interested in is that invoking resource based routing automatically generates Paths for you via helpers(I think?).
How would I generate a Path for my route, such that I'd be able to use a link_to method to link various parts of the application together? Any help/comments would be greatly appreciated.
Firstly, if you want to get 'samplegraph' page rendered by hitting 'www.homepage.com/samplegraph', you will need to update your route.
Replace
get 'homepage/samplegraph' => 'homepage#showgraph1'
with
get '/samplegraph' => 'homepage#showgraph1'
Now in showgraph1 action of your homepage controller, you will need to render samplegraph view page at last line of the action.
render 'samplegraph'
As of you second question, just hit rake routes on your terminal from your app directory. It will show all routes with helpers which you can use with link_to. You will need to append _path to those routes while using with link_to
Like #RAJ said first of all you need to change your route to
get '/samplegraph' => 'homepage#showgraph1'
At this point I'm not particularly sure what the showgraph1 action should be in order to render the view page(samplegraph)
Rails doesn't care if your action is empty or not, it'll still render your actions view even if it's empty. Since your action is named showgraph1 so it'll make rails look for showgraph1.html.erb with path views/homepage/showgraph1.html.erb
To change this behavior you need to use render 'samplegraph' in your action
One thing I am interested in is that invoking resource based routing automatically generates Paths for you via helpers(I think?)
Rails generate path and url helpers for each route and it doesn't depend on how your routes are defined but you can customize your helper methods by specifying as: option
get 'homepage/samplegraph' => 'homepage#showgraph1', as: 'showgraph'
This will make your helper methods showgraph_path and showgraph_url
I want to implement a tracking pixel for inside an email.
The tracking pixel URL should look something like this:
http://example.com/__track.gif?id=XXXXXXX&u=XXXXX
Questions:
How do I create a route specifically for that?
while it says it's .gif, I don't actually want a gif file, as I want a rails controller to get called and be able to run a few methods when called, using the url params.
Thoughts?
Put the following in your routes.rb file:
match "/_track.gif" => "controller_name#action_name"
Replacing controller_name & action_name with those appropriate.
I recommend you read over the Rails Guide for routing, as match is noted in the very first section.
I have a small rails app that has default scaffold generated routes eg. /stadia/1.xml. However I have to support legacy client app that can't construct such URLs correctly. What I need to do is to map URL in the form:
/stadia?id=1?format=xml to /stadia/1.xml
Or even something like:
/myApp?model=<model_name>?id=<id>?format=xml to /<model_name/<id>.xml
Is it possible to craft appropriate route in Rails?
I don't have good answer for this. What I would do is change first part of url to /stadia_legacy for legacy urls or change first part of urls for RESTful routes.
Then you can map in routes:
map.stadia_legacy :stadia_legacy, :controller => 'stadias', :action => 'please_redirect_me'
Then in stadias controller in action please_redirect_me you can check all params (they are availble in params hash: params[:id], params[:format] etc.) and redirect to correct url. Or you can write all routes manualy to correct controller and action.
What if you do some url rewrite in apache ?
I had a similar question. No answers so far, so it seems routes.rb config doesn't offer an easy way of doing this (routing based on query parameters), which I find surprising actually.
So an ugly workaround would be to have a 'myApp' default route, and then have a special redirecting controller which would look at the query params (because in controllers you do have access to that) and redirect accordingly.