Routing error on Rails 3 - ruby-on-rails-3

I'm working on a profile page, so the config is like this:
routes.rb
=> match "user/:login(*path)" => 'users#profile', :as => :profile
rake routes
=> profile /user/:login(*path)(.:format) {:action=>"profile", :controller=>"users"}
on console
> Rails.application.routes.recognize_path("/user/example/whatever")
=> {:action=>"profile", :login=>"example", :controller=>"users", :path=>"/whatever"}
And I have a profile action in UsersControllers.
But when I use
&lt%= link_to user.name, profile_path(user.login) %>
in a view I get the error
No route matches {:login=>"example", :controller=>"users", :action=>"profile"}
What am I missing?
Thanks
Update:
Thanks for the answer and attention, Steve!
After a lot of time trying, a coworker find what I was missing: the problem was only with some logins that are emails too, with "#", ".", etc. The solution was adding to_url at params[:login] in link_to:
&lt%= link_to 'name', profile_path(params[:login].to_url) %>
Again, thanks for the attention!

Using your configuration, the route and helper seem to work fine in a Rails 3.0.5 sample app.
I verified the route helper profile_path in the Rails console:
>> app.profile_path('example')
=> "/user/example"
and checked that it worked in the view as well:
<%= link_to 'name', profile_path(params[:login]) %>
No route errors in either place. And putting <%= debug(params) %> in the view shows the path '/user/example/whatever' is being parsed correctly:
--- !map:ActiveSupport::HashWithIndifferentAccess
controller: users
action: profile
login: example
path: /whatever

Related

User Authentication in rails 3.0

When trying to use user authentication I get the following error: "NoMethodError in Viewer#show". And it addresses the error to <%= #page.body.html_safe%> in app/views/viewer/show.html.erb:1:in '_app_views_viewer_show_html_erb__685858346_34780128', which is only one line code by now.
But, when I call login page on browser address bar like: :3000/session/new, it comes Up. Which is not happening with :3000/session/destroy.
It seems that something related to the route is not working properly because, on the other hand, when I call a page on views/layouts/application.htm.erb like <li><%= link_to 'Home', {:controller => 'viewer', :action => 'show', :name => 'home'} %></li> it works, and if I switch to <li><%= link_to 'Home', view_page_path('home') %></li> it gives a similar error.
How can I solve that?
Your use of view_page_path('home') assumes that there is a named path view_page. Changing
get "/:name" => 'viewer#show'
to
get '/:name' => 'viewer#show', :as => :view_page
should fix that.
Secondly when using route helpers with named parameters you need to specify the name so Rails knows what parameters should be used. Change view_page_path('home') to view_page_path(:name => 'home').
And finally a NoMethodError for <%= #page.body.html_safe%> suggests to me that either #page or #page.body is nil.

Rails 3 Search Action Redirects To Show?

I've ran into a weird problem in Rails. When I try and submit a search query with the following form on my Uploads controller:
<%= form_tag ({:controller => "uploads", :action => 'find_uploads'}), :method => "get" do %>
<%=h text_field_tag :search, params[:search], :id => 'search_field' %>
<br />
<%= submit_tag "Search", :style=>"display:inline;" %>
<% end %>
I get redirected to the following url and error page:
/uploads/find_uploads?utf8=✓&search=bot&commit=Search
ActiveRecord::RecordNotFound in UploadsController#show
Couldn't find Upload with id=0
My find_uploads route: get 'uploads/find_uploads' => "uploads#find_uploads"
And when I did I rake routes this is what I got:
uploads_find_uploads GET /uploads/find_uploads(.:format) {:controller=>"uploads", :action=>"find_uploads"}
Everything seems to be in order... not sure why it's not working out as expected. For debugging purposes I dropped breakpoints in both my find_uploads and show actions and neither of them were reached so this error message must be lying to me as the UploadsController show action is never called!
This form is being rendered on my index page if it counts for anything.
I think it's taking find_uploads for an id.
Declare your routes like that:
resources :uploads do
collection do
get :find_uploads
end
end
ps: currently, when you do rake routes, /uploads/find_uploads is after /uploads/:id right?

url_for adding controller and action to querystring in rails 3.2

I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.

Destroy action in a Rails 3.0 app with jquery mobile is producing a routing error

I'm using jquery mobile in a rails 3.0 app. I am getting a routing error when attempting to delete an item from a user's list. The code and error message are as follows.
index.mobile.erb:
<% #items.each do |i| %>
...
<div data-role="button">
<%= link_to 'Delete', i, :confirm => 'Are you sure?', :method => :delete %>
</div>
<% end %>
routes file:
resources :items, :except => [:show]
rake routes:
DELETE /items/:id(.:format) {:action=>"destroy", :controller=>"items"}
error message:
Started GET "/items/2" for 127.0.0.1 at 2012-02-26 13:31:37 -0500
ActionController::RoutingError (No route matches "/items/2"):
What am I missing?
Thanks!
UPDATE - Firebug:
#http://localhost:3000/javascripts/jquery-1.6.1.js?1310343332:641 #http://localhost:3000/javascripts/jquery.mobile-1.0.1.js?1329380575:2737 #http://localhost:3000/javascripts/jquery.mobile-1.0.1.js?1329380575:3030 #http://localhost:3000/javascripts/jquery.mobile-1.0.1.js?1329380575:3393 #http://localhost:3000/javascripts/jquery-1.6.1.js?1310343332:2926 #http://localhost:3000/javascripts/jquery-1.6.1.js?1310343332:2560
jquery...0343332 (line 641)
"NetworkError: 404 Not Found - http://localhost:3000/items/2"
Seems like the problem you're having is the same as mentioned # Rails' link_to method: GETing when it should DELETE.
Can you paste rest of the request parameters (chrome developer console or firebug)? Have to see if rails is trying to do magic to accomodate the HTTP delete verb.

Simple Rails 3 Route is Failing - No route matches

In my config/routes.rb I have:
post "portal_plan_document/update"
rake routes confirms this:
$ rake routes
portal_plan_document_update POST /portal_plan_document/update(.:format) {:controller=>"portal_plan_document", :action=>"update"}
....
In my code I have:
<%= form_for #plan_doc,
:url => portal_plan_document_update_path,
:method => "POST", :remote => true do |f| %>
In my log file I see:
Started POST "/portal_plan_document/update" for 127.0.0.1 at 2011-03-31 18:04:37 -0400
ActionController::RoutingError (No route matches "/portal_plan_document/update"):
I am lost as what to do from here. Any help would be greatly appreciated!
I should state I am using Ruby 1.9.2 and Rails 3.0.5. Oh, I have restarted the server (WebBrick w/rails server) after updating routes.rb.
Jeremy
Figured it out! :)
if you have a non-empty object, rails assumes you want to update that object. i.e., use a 'PUT' instead of a 'POST'
to accomplish 'PUTs', rails will put a hidden input in the form, with "_method" = "put". so, it LOOKS like it's a POST, but rails is treating it as a PUT.
if you actually want to just update an object (what it looks like you're doing), a PUT is better, and you should just switch your routes over to PUT.
if (like I was), you're doing something that really requires a POST (i.e., it can't be sent more than once safely), you can write your form_for like this:
<%= form_for #plan_doc,
:url => portal_plan_document_update_path,
:html=>{:method => "POST"}, :remote => true do |f| %>
to confirm, look at the generated HTML source, and make sure the hidden "_method" field is not set to "put"
Try using that instead please :
:method => :post
If this does not still work, please lose the remote attribute and give it a try. Does it work without it ?
I had the same problem while upgrading a simple app from Rails 2 to Rails 3.
As you may guess I was updating all "remote_form_for(#item) (..)" helpers to "form_for :item remote => true (..)" syntax.
In my case this code from a items/_new.html.erb partial:
<%= form_for :item, :remote => true do |f| %>
<!--FIELDS-->
<% end %>
Gave me this error:
Started POST "/items/new" for 127.0.0.1 at Fri Aug 12 18:19:23
+0200 2011
ActionController::RoutingError (No route matches "/items/new")
As you can notice the method was a correct "POST", not a "PUT". The problem lied in the routing... I don't know why but when a remote POST method is sent by a partial, Rails routes the POST request to "/items/new" instead of "/items" route. Even if the purpose is to create a new "item" so the POST request should be correctly (and RESTfully) routed to "/items".
This code, with explicit action and controller, solved the problem:
<%= form_for :item, :remote => true, :url => { :controller => "items", :action => "create" } do |f| %>
<!--FIELDS-->
<% end %>