Error following Michael Hartl's tutorial: - ruby-on-rails

I've been following Michael Hartl's rails tutorial but without testing (bad practice I know - I'm completely new to web programming having only dabbled in HTML and CSS before). I've reached the last chapter but I'm having problems with the user signup form. It's rendering properly in the browser but on submit I get the message
No route matches "/users/new"
Everything seems to be as it should be in the routes.rb file, and by entering users/new directly into the browser I can navigate to the correct page (the signup form) - but can't create new users.
There doesn't seem to be anything missed out from Michael Hartl's code
I've checked out the users controller as I figured it must be something to do with the 'new' or 'create' actions. It might also have to do with the number of "swap" files that seem to be being created every time I edit a file with Vim. I'm completely ignorant about what this means, but perhaps it's screwing things up. I've left these intact in my github push so you can see my incompetence.
Thanks for any help you can give me!
Here's the routes.rb file (everything else is on github at https://github.com/jnwb2/the_app):
TheApp::Application.routes.draw do
resources :users do
member do
get :following, :followers
end
end
resources :sessions, :only => [:new, :create, :destroy]
resources :microposts, :only => [:create, :destroy]
resources :relationships, :only => [:create, :destroy]
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
root :to => 'pages#home'
end

Found the answer. For one, i think you should stop following this tutorial. It seems to have a lot of bad practices going on. Try going to guides.rubyonrails.org instead. To fix your problem however, change line #8 in the users controller like so:
def new
#title = "Sign up"
#user = User.new
end
Sorry for all my extra comments from before, just ended up cloning your source to find the issue.

Related

No route error for edit helper that uses a token as an :id (From Ruby on rails tutorial third edition)

A url helper I am using does not work, even if it shows at rake routes. Not sure why, hopefully someone could give me a suggestion or point out errors. Something to do with using a token as an id or configuration?
Clicking on this:
<%= link_to "Reset password", edit_password_reset_url(#member.reset_token, email: #member.email) %>
Gives this error message (email parameter does pass through):
No route matches [GET] "/password_resets/$2a$12$Be6H4xiPjqlBtLxAozB7EujVy.2nZSLFJzL3LYDugvDmnn6aoGJ0G/edit"
The :new, :create paths work but not the :edit.
routes.rb (The last line):
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'members#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :members
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
This is from the Ruby on Rails Tutorial Third Edition by Michael Hartl (chapter 10)
Thank you for your time reading this and all answers will be appreciated.
The issue here is that -
Your edit_password_reset route is
/password_resets/:id/edit
but while calling it you are not passing the id but calling it on the member token like this -
<%= link_to "Reset password", edit_password_reset_url(#member.reset_token, email: #member.email) %>
So for the above call to work your edit route should be something like
/password_resets/<token>/edit
The new and create paths work because they don't require this token. Try to follow the tutorial properly and you will find where you have gone wrong.

Template is missing ruby-on-rails

localhost says that template is missing. routes.rb:
Rails.application.routes.draw do
root 'home#index'
resources :sessions, only: [:new, :create, :destroy]
get 'signup' => 'users#new'
get 'signin' => 'sessions#new'
delete 'signout' => 'sessions#destroy'
get 'about' => 'static_pages#about'
match '*path' => 'application#routing_error', via: :all
end
nothing in my home_controller.rb
any idea to fix this?
You probably already have a home folder under view. If not, create the folder. Then add a index.html.erb with a heading like this:
<h1>Welcome!</h1>
hope this helps
edit
You should define and index method too! (not necessary)
In addition to #YahsHef's answer.. You state that
Nothing in my home_controller.rb
You'll also need the index method to exist.
class HomeController < ApplicationController
def index
end
end
in order to render the view.

rails 3 routing keep custom url for post

i am fairly new to rails and want to keep the url the same for a user signing in if there is an error and the 'new' template is rendered
here are my routes
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create, :destroy]
root to: 'pages#home'
match '/signin', to: 'sessions#new'
#match '/signin', to: 'sessions#create', via: :post, as: :post_session
match '/logout', to: 'sessions#destroy'
and here is the sessions controller code
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to root_url
else
flash.now[:error] = 'Invalid email or password'
render 'new'
end
end
as you can see,i have a custom route commented out to catch the post so that the render 'new' call keeps the /signin url, but when i do this, the flash messaging of an error does not render in the page (it does without that route though). i tried to use flash without the now method and still was not seeing my message show up. any ideas?
EDIT:
i tried the suggestions below and was still seeing the issue. after looking at the access logs, the application was routing to the first signin route because it was defined with match and not get.
my updated and working routes file now looks like this
resources :users, only: [:new, :create]
#resources :sessions, only: [:new, :create, :destroy]
root to: 'pages#home'
match '/signin', to: 'sessions#new', via: :get
match '/signin', to: 'sessions#create', via: :post, as: :post_session
match '/logout', to: 'sessions#destroy', via: :delete
Take out the now, that shouldn't be needed. Your routes are probably conflicting. Based on how you have the match lines setup, you can probably just remove the resources :sessions altogether, and then uncomment the match line. That should take care of what you need.
Also, make sure to go back to your other questions and accept some answers. A 0% acceptance rate isnt as likely to attract answers.
Edit
Based on your comments, it might just not know what to render at this time when you removed the resources call. Try changing to:
render "sessions/new"

uninitialized constant error in rails 3 on newly created controller

I added a Session controller to my application for user sign-in / sign-out, using
rails g controller Session new create destroy
then add the following lines to my route file:
resources :sessions, :only => [:new, :create, :destroy]
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
when I do rake routes in the console, the routes do show up, but when I launch the app in the browser, I got this error:
uninitialized constant SessionsController
Thanks in advance!
You created a Session controller, not a Sessions controller. Since it's singular, you want a singular route:
resource :session, :only => [:new, :create, :destroy]
I was running into this today, and found I had to do three things, 1) use resource (not resources); 2) supply the controller manually, and 3) manually set the url in form_for tags using the resource (may not apply for your case)...
# routes.rb
resource :session, :only => [:new, :create, :destroy], :controller => 'session'
#.../new.html.erb
<% form_for #session, :url => session_path do |f| %>
Specifying the controller matters if, like me, your controller name, file names, etc, are all singular.
This is apparently related to a bug in rails

In Rails Routes redirect controller action to root?

I have a root path in my routes file:
root 'games#index'
The problem is that if anyone goes to: http://domain.com/games it doesn't show the root, thus we're creating two urls for the same page.
Is there a way to change any hit to http://domain.com/games to http://domain.com?
I'd rather not fiddle around with a before_filter in the application controller if there's a nice way to do it in the routes folder.
Thanks!
The easiest way is to just set up a redirect.
map.redirect('/games','/')
Although, your problem is really that the /games route shouldn't be there in in the first place.
Remove the catchall route at the bottom of your routes.rb file, and this won't even be a problem.
I had the same issue with sessions. I wanted to expose /sessions for login and logout, but since an invalid password leaves you at /sessions, I wanted a graceful way to deal with a user re-submitting that URL as a GET (e.g. by typing Ctrl-L, ). This works for me on Rails 3:
resources :sessions, :only => [:new, :create, :destroy]
match '/sessions' => redirect('/login')
match '/login', :to => 'sessions#new'
I think your code would look something like this:
resources :games, :only => [:new, :edit, :create, :destroy]
match '/games' => redirect('/')
routes.rb:
root 'games#index'
In controller:
def index
redirect_to root_path if request.env['PATH_INFO'] === '/games'
#games = Game.all
end
As of the current version of Rails (3.2.9), this is how I achieved the same result:
MyApp::Application.routes.draw do
# ...
# declare the redirect first so it isn't caught by the default
# resource routing
match '/games' => redirect('/')
# ...
resources :games
# ...
root :to => 'games#index'
end

Resources