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.
Related
I'm new to rail. I have few clarifications, which I have listed below.
I have referred many links and stack overflow questions, everywhere it's mentioned to use request and fetch the details regarding the path, controller, action etc. but if I use request in my routes.rb it throws undefined local variable or method error.
I used constraints in my routes.rb and from there it calls a method matches? from a class where dynamic constraints are defined inside lib/constraints directory. In here the matches?(request) receives a parameter named request, which has details about current route, from where the parameter value is sent?, I have this doubt because while using this method inside routes.rn in constraint(ClassName) I'm not sepecifying the method name (matches?) or the parameter request
I would like to know the working of things behind the scene.
Thank You
Are you looking for it
request.url
# => "http://localhost:3000/lists/7/items"
request.path
# => "/lists/7/items"
If you have URIs:
localhost:3000/users/:id
localhost:3000/users
localhost:3000/users/new
....
You can do something like
rake routes | grep user
in order to get only those that contain user in its path name.
Hope this helps..
conventionally you don't access the current path in the routes.rb, at least as how it seems you're asking to access it. There are a number of different matching methods and techniques used in the rails router that serve the utility I imagine your looking for.
Here's a link to the docs: https://guides.rubyonrails.org/routing.html
Can you share exactly why you want to access the curret route in the routes.rb file?
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
I'm used scaffold_controller to generate a controller for a model I have. In my view, I have the following link to
link_to, 'Like', like_path(param: 'param')
In my controller that was generated, I have a private method set_like which is called and I can figure out why. I just want the link to go to the likes#new path, but it's going to the set_like method first. I feel like this is a new rails thing and I'm not sure why. Any ideas?
Looks like you want to use "new_like_path" rather than "like_path".
You probably would benefit from reading this closely: http://guides.rubyonrails.org/routing.html
When you use "like_path" you are linking to an existing record, whose id you should pass to the "like_path" route generator. Like:
like_path(2) # link to like with id==2
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