我的红宝石版本是1.9.1。这是我想要开发的ruby中的第一个项目。
我收到了错误
“NameError(未定义的局部变量或方法request' for #<User:0x3ab7220>):
app/models/user.rb:36:in
login'
app / controllers / user_controller.rb:7:在`signup'“
当我尝试输入一些细节并保存记录时。这是控制器类
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 :(得分:3)
正如评论者指出的那样,您的错误位于user.rb
。您正在尝试访问控制器的request
方法,但您不能,因为此时您已进入User
类。通常,模型层(在这种情况下为User
)不应该知道有关其他层(控制器和视图层)的任何信息。控制器旨在处理与请求和响应相关的事务。您可能需要阅读MVC in Rails或MVC in general。