Rails url_for Gravatar routing error - ruby-on-rails-3

This function is defined in the application_help.rb:
def gravatar_url_for(email, options = {})
url_for(
{
:protocol => 'http://',
:host => 'www.gravatar.com',
:controller => 'avatar',
# :controller => 'avatar.php',
:gravatar_id => Digest::MD5.hexdigest(email),
:only_path => false
}.merge(options)
)
end
It's used in views:
<%= image_tag(gravatar_url_for user.email, {:d => 'identicon', :s => 32, :r => 'g'}) %>
Occasionally, its usage will result in a routing error:
No route matches {:controller=>"avatar", :d=>"identicon", :r=>"g", :gravatar_id=>"486575e581db04b7c8ca218af8488657", :s=>32}
A valid email is being supplied when the error occurs.
If I replace the url_for() with this logic, it works as expected:
url_for("http://www.gravatar.com/avatar/" + Digest::MD5.hexdigest(email) + "?d=identicon&s=40&r=g")
** edit **
I had removed the following line from the routes.rb file:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'
Is there a way to get the url_for to work without the 'legacy wild controller route'?

You might want to take a look at the Gravtastic plugin for Rails which supports Gravatar images in both Ruby and JavaScript.

Related

How to Force SSL to Specific Named Route in Rails

I have a Rails Application in which I have used force_ssl method as
def force_ssl # to use https for payment page
if !Rails.env.development? && !request.ssl?
redirect_to :protocol => 'https'
end
end
for a Particular action name 'abc' with named route
match '/find-a-abc' => "home#abc"
when I go to URL
http://local.demo.com/find-a-abc
it will redirect_to https://local.demo.com/abc
Any Solution? so it will redirect_to Particular route, rather than redirecting to an Action when using a https Protocol.
It doesn't look like you've provided a named route here (i.e. match '/find-a-abc' => "home#abc", :as => :named_route). You will need to do this and call named_route_url rather than just the controller and action to get the right URL.
If you want a specific route to always be handled with SSL, you could define the route like so:
scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
match '/find-a-abc' => "home#abc", :as => :abc
end
Then abc_url should always be "https://local.demo.com/find-a-abc"

Rails 3 - routing to a controller method

heres what I'm currently using:
Rails -v 3.2.3
I'm trying to make a path in routes.rb that will route to a method in my links_controller.rb.
routes.rb
match 'modify_points' => 'links#modify_points', :as => :modify_points
links_controller.rb method
def modify_points
#link = Link.find(params[:id])
#link.update_attribute :points, #link.points + params[:by].to_i if params[:by] =~ /[+|-]?1/
render_text #link.points
end
here is how i am calling the method in my view:
<%= link_to('UP', modify_points_path(link, :by => 1), :remote => true) %>
however when i click on this UP link.... nothing happens. what should happen is link.points should increase by 1, however when i check in the console, my points are still at zero. am i creating the routes correctly?
I have always used something like this:
match '/modify_points' => 'link#modify_points', :as => 'modify_points'
Does that generate the route you want? What does rake routes show?

Is a route like `"controller#action"` not the same as `:controller => 'controller'` and `:action => 'action'`?

I have a Rails 3.1 app where I just got some weird behavior.
I had two routes declared as follows:
# OLD METHOD
get 'accept_terms', :to => "users_terms#show"
put 'accept_terms', :to => "users_terms#accept'"
Running rake routes included the following:
# accept_terms
# GET /accept_terms(.:format) {:action=>"show", :controller=>"users_terms"}
# PUT /accept_terms(.:format) {:action=>"accept'", :controller=>"users_terms"}
The GET worked fine, but the PUT produced this error:
AbstractController::ActionNotFound (The action 'accept'' could not be found for UsersTermsController):
I confirmed that the action did exist on that controller.
While tinkering around with the problem, I changed the route declarations to:
get 'accept_terms', :controller => 'users_terms', :action => 'show'
put 'accept_terms', :controller => 'users_terms', :action => 'accept'
Running rake routes produced:
# accept_terms
# GET /accept_terms(.:format) {:controller=>"users_terms", :action=>"show"}
# PUT /accept_terms(.:format) {:controller=>"users_terms", :action=>"accept"}
With this, both GET and PUT worked fine.
Is :to => "controller#action not the same as :controller => 'controllerName', :action => 'actionName'?
The only difference I see in the produced routes is the order of :action and :controller...
In the old method you end the users_terms#accept with both a single quote and a double quote.

Devise + Omniauth - testing - getting no route matches "/oauth/authorize"

I'm trying to write tests for authentication with Twitter and Facebook. I am using Devise and Omniauth. When I try it out it works correctly, but I can't get the tests to pass for it.
I'm following instructions, but it isn't in depth enough for me, plus I'm doing things a little differently (already have some code base). I have a custom controllers for omniauth_callbacks and registrations.
My problem is that when I run the tests it says:
(::) failed steps (::)
No route matches "/oauth/authorize" (ActionController::RoutingError)
<internal:prelude>:10:in `synchronize'
(eval):2:in `click_link'
./features/step_definitions/web_steps.rb:57:in `/^(?:|I )follow "([^"]*)"$/'
features/link_twitter.feature:19:in `And I link twitter'
Failing Scenarios:
cucumber features/link_twitter.feature:16 # Scenario: User links twitter
Where is this /oauth/authorize route coming from and how do I handle that?
It does not look like it is getting to my OmniauthCallbacksController. It follows the link and then dies. I think it has to do with Omniauth's call back method during test mode, but I'm not sure how to change / manage that.
Update: Here are my routes for Devise.
devise_for :users, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks",
:registrations => 'registrations',
:passwords => 'passwords',
:sessions => 'sessions',
:confirmations => 'confirmations'
} do
match 'confirmations/unconfirmed' => 'confirmations#unconfirmed', :as => :unconfirmed
match 'confirmations/send_advisor_confirmation/:id' => 'confirmations#unregistered_advisor_confirmation', :as => :send_advisor_confirmation
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
end
I forgot to put the following into env.rb
# features/support/env.rb
OmniAuth.config.test_mode = true
For more information read about the testing here.

Defining :id in routes to be something other than id in rails 3

I am porting a Merb app to Rails 3. In Merb we could put an Identify block around a route to define how an :id route parameter was to be supplied, e.g.,
# this is a Merb route that I want to port to Rails 3 routing; I get everything except
# how to replicate the behavior of Merb's Identify block which doesn't require one to
# futz with overriding to_param on user; a user instance gets passed to the url builder
# ala url(:edit_password_reset, user) and this tells the router to use the
# reset_password_token method on user to supply the :id value for this one route
Identify User => :reset_password_token do
match("/reset-password/:id", :method => :get).to(:controller => "password_resets", :action => "edit").name(:edit_password_reset)
end
# and then later define more routes that use the user's id without a problem
# since to_param was not overridden on user; here I have already translated to
# Rails 3 and this works fine
controller :users do
get "/register", :action => "new", :as => "new_user"
get "/users", :action => "index", :as => "users"
get "/users/:id", :action => "show", :as => "show_user"
get "/users/:id/edit", :action => "edit", :as => "edit_user"
put "/users/:id", :action => "update", :as => "update_user"
post "/users", :action => "create", :as => "create_user"
end
In Rails, as in Merb, you can override to_param to provide an alternative id value for routes, but for a case where one time you want to use an id and another time you want to use a different method on the same object (as above), Identify is convenient. What is the Rails 3 equivalent? I looked through the Rails 3 source and tests and didn't see anything equivalent to Identify. Did I miss it?
I can refactor things and maybe should to not need it in this case, but still I would like to know if I missed something.
Thanks.
I came across the same problem; turns out the best way is to skip to_param entirely when calling a url or path. For instance:
# This will set params[:id] to #user.to_param
edit_password_reset_url(#user)
# This will set params[:id] to #user.reset_password_token
edit_password_reset_url(#user.reset_password_token)
In other words, to_param is only called when passing a record to the url helpers; if you pass it a string instead, it will just parse the string.