如何使用Rails 3解决模板丢失错误?

时间:2015-03-16 09:53:03

标签: ruby ruby-on-rails-3

当我的页面未能进入时,我收到以下错误。如果我输入了错误的密码/电子邮件并点击了提交,则会出现以下错误。

错误:

Template is missing

Missing template sessions/member, application/member with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/Site/library_management1/app/views" * "C:/Ruby193/lib/ruby/gems/1.9.1/gems/devise-3.4.1/app/views"

我的代码如下。

的观点/舍/ member.html.erb

<% if current_user %>
<div class="totaldiv">
  <div class="navdiv"><span>STUDENT INFORMATION</span><span>Logged in as <%= current_user.email %></span></div>
  <div class="wrapper">
  <div id="leftsidebtn">
 <ul>
  <li><a href="/homes/issuebooks">Book issue</a></li>
   <li><a href="/homes/availablebooks">Books Available</a></li>
<li><a href="/sessions/removeuser">Log Out</a></li>
 </ul>  
</div>
  </div>
  <div class="restdiv" id="ex3" >
    <center>


</center>
  </div>
</div>
<% else %>
<div class="totaldiv">
  <div class="navdiv"><span>STUDENT INFORMATION</span></div>
  <div class="wrapper">
  <div id="leftsidebtn">
 <ul>
<li><a href="/homes/registration">Registration</a></li>
<li><a href="/homes/index">Back</a></li>
 </ul>  
</div>
  </div>
  <div class="restdiv" id="ex3" >
    <center>

  <div class="studentlogin">
    <h1>Login Here</h1>
<section class="studentloginloginform cf">
      <%= form_for :users,:url => {:action => 'loginuser',:controller => 'sessions'} do |f| %>
      <% if @users.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@users.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @users.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
      <ul>
        <li>
          <label for="usermail">Email</label>
          <%= f.email_field :email,placeholder:"yourname@email.com" %>
        </li>
        <li>
          <label for="password">Password</label>
           <%= f.password_field :password,placeholder:"password" %>
          </li>
        <li>
          <%= f.submit 'LogIn',:class => 'studentsubmit' %>
        </li>
        <li class="reg_member">
          Not a member ? <%= link_to 'Register Here',homes_registration_path %>
        </li>
      </ul>
      <% end %>
  </section>
</div>
</center>
  </div>
</div>
 <% end %>

控制器/ sessions_controller.rb

class SessionsController < ApplicationController
    def loginuser
        @users=User.authenticate(params[:users][:email], params[:users][:password])
        if @users
            session[:user_id]=@users.id
            cookies.signed[:user_id]=@users.id
            flash[:notice]="login successfull"
            flash[:color]="valid"
            redirect_to :action => 'member',:controller => 'homes'
        else
            flash[:notice]="could not Logged in"
            flash[:color]="invalid"
            render 'member', :controller => 'homes'
        end
    end
    def removeuser
        session[:user_id] = nil
        cookies.delete :user_id
        flash[:notice]="user logged out successfully"
        flash[:color]="valid"
        redirect_to :action => 'member', :controller => 'homes'
    end
end

模型/ user.rb

class User < ActiveRecord::Base
  attr_accessible :address, :email, :first_name, :last_name, :password, :password_hash, :password_salt, :tel_no ,:password_confirmation
  attr_accessor :password
  before_save :encrypt_password
 EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
 validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
 validates :first_name, :presence => true, :length => {:in => 3..10}
 validates :last_name , :presence => true , :length => {:in => 3..10}
 validates :tel_no , :presence => true , :length => {:in => 1..10}
 validates :password, :confirmation => true
 validates_length_of :password, :in => 6..20, :on => :create
 def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
  has_many :issue
  has_many :book
end

请检查代码并尝试解决此错误。

2 个答案:

答案 0 :(得分:1)

在您的控制器更改行render 'member', :controller => 'homes'中,如下所示。

render 'homes/member'

答案 1 :(得分:0)

&#34; sessions / member&#34;路径

我想,在以下路径中创建一个模板来解决它

 views/sessions/member.html.erb