2011-01-11 82 views
2

我的红宝石版本是1.9.1。这是我尝试开发的红宝石中的第一个项目。 我收到错误 “NameError(未定义的局部变量或方法request' for #<User:0x3ab7220>): app/models/user.rb:36:in登录 应用程序/控制器/ user_controller.rb:7:'注册'”未定义的本地变量或方法'请求'

当我尝试输入一些细节并保存记录。这里是控制器类

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 

我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> 
+0

给我们提供确切的错误信息... – Kunday 2011-01-11 15:09:53

回答

2

正如评论者所指出的那样,你的错误是在user.rb。您正在尝试访问控制器的request方法,但不能,因为您在此时正在User类中。通常,模型层(在这种情况下为User)不应该了解其他图层(控制器和视图图层)的任何内容。控制器旨在处理与请求和响应相关的事情。您可能需要阅读MVC in RailsMVC in general

相关问题