Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:46:48+00:00 2026-06-03T18:46:48+00:00

I am new to Ruby on Rails and have been helped immensely by Michael

  • 0

I am new to Ruby on Rails and have been helped immensely by Michael Hartl’s excellent book: Ruby on Rails Tutorial. I have gotten to Chapter 8 and am now on the exercises in that chapter. I am having ( I assume a typical “newbie”) problem with exercise 1. In this exercise it is asked “1.Refactor the signin form to use form_tag in place of form_for.” I have tried to searching for assistance with this in Stackoverflow, Google, Railscast, and many other “web searches” for two days now and I do not seem to find the assistance that I need to answer this problem. The file I am trying to modify with form_tag is below:

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
     <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

I am using Rails 3.2.3 in this application. Can anybody point me in the correct direction? Can anyone help me with this problem? I would be most appreciative.

This is the implementation that uses form_tag:

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_tag( url: sessions_path ) do  %>

      <%= label_tag :email %>
      <%= text_field_tag :email %>

      <%= label_tag :password %>
      <%= password_field_tag :password %>

      <%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>

    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>

I am using Rspec 2.9.0 and below are the failing tests:

describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end 

and

describe "with invalid information" do
            before { click_button "Sign in" }

            it { should have_selector('title', text: 'Sign in') }
            it { should have_selector('div.alert.alert-error', text: 'Invalid') }

            describe "after visiting another page" do
              before { click_link "Home" }
              it { should_not have_selector('div.alert.alert-error') }
            end
      end

and

describe "with valid information" do
            let(:user) { FactoryGirl.create(:user) }
            before do
              fill_in "Email",    with: user.email
              fill_in "Password", with: user.password
              click_button "Sign in"
            end

            it { should have_selector('title', text: user.name) }
            it { should have_link('Profile', href: user_path(user)) }
            it { should have_link('Sign out', href: signout_path) }
            it { should_not have_link('Sign in', href: signin_path) }

            describe "followed by signout" do
                    before { click_link "Sign out" }
                    it { should have_link('Sign in') }
            end
      end

Here’s my routes file:

SampleApp::Application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]


  get "users/new"

  root to: 'static_pages#home'

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:46:50+00:00Added an answer on June 3, 2026 at 6:46 pm

    I have just completed this exercise as well, so I am by no means an expert; however, this is the code that has worked for me and passed all the tests:

    ../app/views/sessions/new.html.erb

    <% provide(:title, "Sign in") %>
    <h1>Sign in</h1>
    
    <div class="row">
      <div class="span 6 offset 3">
        <%= form_tag sessions_path do %>
    
          <%= label_tag :email %>
          <%= text_field_tag :email, params[:email] %>
    
          <%= label_tag :password %>
          <%= password_field_tag :password %>
    
          <%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
        <% end %>
    
        <p>New User?<%= link_to "Sign Up Now", signup_path %> </p>
      </div>
    </div>
    

    I also needed to change the ../app/controllers/sessions_contoller

    class SessionsController < ApplicationController
    
      def new
      end
    
      def create
        user = User.find_by_email(params[:email])
        if user && user.authenticate(params[:password])
          session[:user] = user.id
          sign_in user
          redirect_to user
        else
          flash.now[:error] = 'Invalid email/password combination'
          render 'new'
        end  
      end
    
      def destroy
        sign_out
        redirect_to root_path
      end
    end
    

    Whilst this works, I’m not entirely sure why it does; if someone could explain why the changes in the controller are required it would be much appreciated. I know that this could be posed a s a separate question but it is closely related to OP and I’m sure we would both find it extremely useful in understanding not just how to get this to work but why it works this way. The following are the original view and controller files:

    Original ‘form_for’ new.html.erb:

    <% provide(:title, "Sign in") %>
    <h1>Sign in</h1>
    
    <div class="row">
      <div class="span6 offset3">
        <%= form_for(:session, url: sessions_path) do |f| %>
    
          <%= f.label :email %>
          <%= f.text_field :email %>
    
          <%= f.label :password %>
          <%= f.password_field :password %>
    
          <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
        <% end %>
    
        <p>New user? <%= link_to "Sign up now!", signup_path %></p>
      </div>
    </div>
    

    and the original sessions_controller:

    class SessionsController < ApplicationController
    
      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 user
        else
          flash.now[:error] = 'Invalid email/password combination'
          render 'new'
        end  
      end
    
      def destroy
        sign_out
        redirect_to root_path
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Rails & Ruby and have been following the http://ruby.railstutorial.org/ruby-on-rails-tutorial-book and
New to Ruby on Rails and having a problem when following Michael Hartl's tutorial.I'm
I'm new with ruby and rails and have been struggling with this issue for
I'm new to Ruby on Rails, I know things but now I have to
I have been working on Ruby on rails project for a while now, when
I'm very new to Ruby on Rails, and have been getting the following error
Im new to ruby on rails and i have been following The Rails Space
New to both Ruby and Rails but I'm book educated by now (which apparently
I'm pretty new to Ruby on Rails, forms lately have been giving me all
I am new to Ruby and Rails so bear with me please. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.