I have the following set of routes which point to the same view:
get 'mypath', to: 'home#mypath', as: 'mypath'
get 'mypath-v2', to: 'home#mypath', as: 'mypath_v2'
get 'mypath-v3', to: 'home#mypath', as: 'mypath_v3'
How can I check if I am using one route or the other inside the view?
For example if I want to get mypath-v2 or mypath_v2, how would I do it?
Well, as for me it is better to do such things using params. You can define your routes like this:
get "mypath/:version", :as => "mypath"
In this case you will be able to use params[:version] to clarify current path.
You would call it by appending _path or _url
For instance here is an example in a link:
<%= link_to 'Link to mypath-v2 page', mypath_v2_path %>
OR
<%= link_to 'Link to mypath-v2 page', mypath_v2_url %>
To compare them you can look at the request object. When you call request.path or request.fullpath it will bring in the actual address request path of the link.
So...
<%= if request.path.eql?('mypath-v2') ? "Used It" : "Other Route" %>
Related
Ok, say you have this:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt"
How do I create a link_to path from it?
Would it be something like this: (it doesn't work, btw)
<%= link_to "New Appointments", controller:tutor_appointments, method:new_appt %>
I'm always confused on routing stuff when it comes to figuring out how to do link_to link.
I do understand that tutor_appointments is the controller and new_appt is the method.
Ideally, you would name the route:
http://guides.rubyonrails.org/routing.html#naming-routes
And then you can refer to the route by that name.
eg. if you had:
match "tutor_appointments/new_appt" => "tutor_appointments#new_appt", as: 'new_appointment'
Then you could do:
link_to 'New Appointments', new_appointment_path
However, in this case it sounds like what you actually want is resource routing:
http://guides.rubyonrails.org/routing.html#resources-on-the-web
And you want a 'new' action for your 'tutor_appointments' resource.
I have the following code in my routes.rb file .
resources :users do
member do
get :following,:followers
end
collection do
put :activate_email
end
end
And I have a user email activation link like this :
<%= link_to "Activate",activate_email_users_url(email_token: #user.email_token),method: :put %>
When I click on the activate link , this is the url that is generated
http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ
Update: Ok, So I think I kno what the problem is . When I look at the html source of the activation email in my gmail which contains the link_to , there is no data-method='put'. So that seems to be the problem . It is always sending a default GET request instead of PUT.
This is my user_mailer/registration_confirmation.html.erb file
<%= javascript_include_tag "application" %>
</head>
Please click on the following link to activate your email
<%= link_to "Activate",activate_email_users_url(email_token: #user.email_token), method: :put %>
This gives the following error :
undefined method `protect_against_forgery?' for #
So , the code <%= javascript_include_tag "application" %> is causing this error. Is there any way around this ?
Sorry, I do not know your purpose, but apparently you have a purpose to activate user.
Try this, if this solution not work, please tell me your action (activate_email) on controller!
see on rake routes output :
activate_email_users PUT /users/activate_email(.:format) users#activate_email
user GET /users/:id(.:format) users#show
when your generate
http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ
Your problem was activate_email considered to be :id
users/activate_email => users/:id
And solution for your problem :
Try removing the method from the link. Its better specifying the method in your routes file. How about replacing match by put in routes as :
resources :users do
member do
get :following,:followers
end
end
put "/users/activate_email/:email_token" => "users#activate_email", :as => "activate"
and on view
<%= link_to "Activate", activate_path(:email_token => #user.email_token) %>
I have not tested this, but I guess this will suffice.
UPDATE
for Question : undefined method `protect_against_forgery?'
Add this to a helper that only your mailer template uses:
def protect_against_forgery?
false
end
NOTE : If You have new question, please create new "Ask Question" and aprrove answer is usefull for this question
If you're trying to activate a single user account you probably don't want to be specifying your route on the collection (which you would use for actions that operate on multiple users).
Here's some (untested) code that should point you in the right direction:
controller :users do
put '/activate/:email_token', :to => :activate, :as => 'activate_email'
end
Which should route a PUT to /activate/xxxx to the UsersController#activate action with a params[:email_token] set as xxxx. It should also give you a #activate_email_url route which you can pass the activation token (you can check what routes your app provides by running rake routes on the command line).
Google redirected me to this question even-though mine was related to rendering a template into a string and not just in the browser. My solution for the template problem was something along these lines:
action_controller = ActionController::Base.new()
action_controller.class_eval do
def protect_against_forgery?
false
end
end
file_string = action_controller.render_to_string('/some_template/template_file',locals: { local_variable: 1 }
In my Rails project, in my index view, I have a link
<%= link_to 'Show all posts', show_all_path %>
In routes.rb, I have a route:
match "show_all" => "Posts#show_all"
When I click on that link, it goes from
http://<domain name>/my_rails_project
to
http://<domain name>/my_rails_project//show_all
It works fine, but I'm wondering why there are two backslashes in front of show_all instead of one. And can I make it so that only one backslash appear?
I think your route needs more information:
`match "/:project_name/show_all" => "posts#show_all", :as => "show_all"
In your view:
link_to 'Show all posts', show_all_path(#project.name)
This assumes you have a #project variable in the page you're viewing.
try use get
get "show_all", :to => 'posts#show_all', as: 'show_all'
Is it possible to generate absolute URL's in rails using link to? [NOTE: THIS IS IN A MAILER]
I tried to do:
<%= link_to root_url, root_url%>
But I get a runtime error:
*Missing host to link to! Please provide :host parameter or set default_url_options[:host]*
I need this to be dynamic because the application will run on a wildcard domain (*.domain.com)
If you use the _url suffix, the generated URL is absolute. Use _path to get a relative URL.
<%= link_to "Home", root_url %>
<%= link_to "Home", root_path %>
Depending on your use case, string interpolation might be a good solution:
link_to(body, "http://#{site_url}")
I found this plugin:
http://www.simonecarletti.com/blog/2009/10/actionmailer-and-host-value/
and it works great!
In routes.rb insert :
root :to => 'controller#action'
Or replace your current map.root with the correct one.
See documentation about this : routes.rb usage
My link_to tag is:
<%= link_to("My test title",{:controller=>"search", :action=>"for-sale", :id=> listing.id, :title => listing.title, :search_term => search_term}) %>
and produces this ugly URL:
http://mysite.com/search/for-sale/12345?title=premium+ad+%2B+photo+%5Btest%5D
How can I get link_to to generate:
http://mysite.com/search/for-sale/listing-title/search-term/12345
Been trying this a few different ways and cannot find much online, really appreciate any help!
Tahe a look at this
add this in your config/routes.rb
map.connect ':controller/:action/:title/search_item/:id', :controller=>'search', :action=>'for_sale'
restart your server and check.
Hope that helps :)
You need to change the URL structure in routes.rb to match what you want the URL to look like, and parse the parameters accordingly in your controller method's args.