Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined local variable or method `request'

My ruby version is 1.9.1. this is first project in ruby i am trying to develop. i am getting the error "NameError (undefined local variable or method request' for #<User:0x3ab7220>): app/models/user.rb:36:inlogin' app/controllers/user_controller.rb:7:in `signup'"

when i try to enter some details and save the records. Here is the controller class

class UserController < ApplicationController   
  def signup 
    if request.post? and params[:user]
      @user = User.new(params[:user])
      if @user.save
        session[:user] = @user
        flash[:notice] = "User #(@user.login) created !"
        redirect_to :action =>"home"
      else
        flash[:error]="Signup unsuccessful"
        @user.clear_password!
      end
    end
  end

  def home
    @title = "Bookshelf - User Home" 
  end

My signup.html

<div id="signup_content">
<span class="title">Sign-up for a BookShelf account...</span>
<% form_for :user, @user, :url => {:action => "signup" } do |f| %>  
    <%= error_messages_for 'user' %><br/>

    <div class="signup_field">
        <label for="user_login">Login:</label>
        <%= f.text_field :login %><br/>
      </div>

    <div class="signup_field">
        <label for="user_first_name">First Name:</label>
        <%= f.text_field :first_name %><br/>
    </div>

    <div class="signup_field">
        <label for="user_password_confirmation">Password Confirmation:</label>
        <%= f.password_field :password_confirmation %>
    </div>

    <%= submit_tag "Signup" %>
<% end %>

application.html

<html>
  <head>
    <title><%= @title %></title>
    <%= stylesheet_link_tag "style" %>
    <%= javascript_include_tag :defaults %>
  </head>
  <body>
    <div id="header">
        <div id ="logo_image">
            <%= link_to image_tag('main_logo.png'), 
            :controller=>'home', :action=>'index' %>
        </div>

        <% if !session[:user] %>
            <%= render :partial=>"user/signin" %>
        <% else %>  
            <div id ="user_menu"><%= link_to 'Logout',:controller=>'user',:action=>'logout'%>
            </div>
        <% end %>
        <div style="clear: both;height:0px;"></div>
    </div>          
    <%= render :partial=> "shared/sidebar" %>
    <div id="Content">
        <% if flash[:notice] -%>
        <div id="notice"><%= flash[:notice] %></div>
        <% end -%>
        <% if flash[:error] -%>
        <div id="error"><%= flash[:error] %></div>
        <% end -%>
        <!-- <%= yield %> uncommenting solved the double rendering problem-->                       
    </div>
  </body>
</html>
like image 608
harshit Avatar asked Sep 10 '25 08:09

harshit


1 Answers

As the commenters have pointed out, your error is in user.rb. You're trying to access the request method of your controller, but you can't because you're in the User class at that point. Typically the model layer (User, in this case) should not know anything about your other layers (the controller and view layers). The controller is intended to handle things related to the request and response. You may want to read up on MVC in Rails or MVC in general.

like image 133
Brian Donovan Avatar answered Sep 12 '25 23:09

Brian Donovan