User Authentication in rails 3.0 - ruby-on-rails-3

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.

Related

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.

Error No route matches [GET] "/root_path" in rails 3.1.3

I am using Rails tutorial with example book by Michael Hartl as a reference for this question.
Here i am using rails 3.1.3 . What is the best way to use Named Routes
routes.rb
root :to => "pages#home"
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
When I access these routes from a view using about I find no error but when I am access it by "about_path" I get an error. But in the book they use about_path. Have the concept of named routes changed in rails 3.1 ?
<li><%= link_to "About", '*about*' %></li>
<li><%= link_to "Contact", 'contact' %></li>
<li><%= link_to "Home", 'root_path' %></li>
If i use "about_path" in the above code I get an error 'route not found'
Question 1. What is the best way to use named routes inside the views? (Best way means I only need to change the route path at single place )
Question 2. How can i access root with concept of named routes?
(I get an error message when i try to access it using 'root_path').
You have to omit the "'" arround ..._path, I think.
make sure the root is first.
at the command line type rake routes to see what routes and _path variables you have.
remove the :to's but leave ther hashrocket =>'s (EXCEPT for root! - leave the :do there)
(1) In routes.rb
match '/about' => 'pages#about'
will automatically create a variable about_path that stores your path name - Your path name, by the way, is distinct from your URL. The routing statement literally instructs rails that whenever someone types /about as URL, rails is to take the action about as defined in the controller pages. In addition, that instruction is stored in shorthand in the implicit named route about_path, which rails creates by concatenating the method name about to the string _path without any intervention on your part.
(2) You will be using that variable in ... app/views/layouts/_footer.html.erb
<%= link_to "About", about_path %>
and ditto in ... app/views/layouts/_header.html.erb
(3) In ... spec/requests/static_pages_spec.rb, you will use
describe "About page" do
before { visit about_path }
And yes, if you really understand routing, you understand 90% of the design of rails so make sure that you understand the contents of routes.rb inside out.
Remove quotes from path variables, i.e. use this in the .erb file:
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li>
<li><%= link_to "Home", root_path %></li>
JP:guard2 jayparteek$ rake routes
root / {:controller=>"pages", :action=>"home"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
Accessing Named routes from views
<li><%= link_to "About", 'about' %></li>
<li><%= link_to "Contact", 'contact' %></li>
<li><%= link_to "News-Home", '/' %></li>
Routes
match '/contact' => 'pages#contact'
match '/about' => 'pages#about'
match '/help' => 'pages#help'
root :to => "pages#home"

Rails Routing issues

I have a website in people can browse artists and their albums. I have setup my routes like so:
match 'albums/[:id]/[:album_id]' => 'albums#show', :as => 'artist_album'
I tried setting up a nested route like:
resources :artists do
resources :albums
end
but I can't figure out how to achieve the routing like in the first example... but thats a different question... This is my code when trying to render artist_album_path
<%= link_to image_tag("#{album["Images"]["Album150x150"]}", width: "122", alt: "#{#term}", class: "float-left"), artist_album_path("/#{CGI::escape(album["Artist"]["Name"])}/#{CGI::escape(album["Title"])}") %>
I keep getting this error:
No route matches {:controller=>"albums", :action=>"show", :id=>"/Beastie+Boys/Licensed+To+Ill"}
Any idea on what I am doing wrong?
In routes.rb:
match 'albums/:id/:album_id' => 'albums#show', :as => 'artist_album'
In your view:
<%= link_to image_tag(...), artist_album_path(:id => ..., :album_id => ...) %>

Routing error 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