即使数据存在,Rails 4.0也会重定向到404

时间:2013-08-11 02:44:41

标签: mysql redirect http-status-code-404 ruby-on-rails-4 null

当我在Rails 4.0中运行我的rails show页面时,我注意到每次无论id是否为n,它都会重定向到404.html页面。虽然这是好的,并且都是针对零的,但是那些确实有数据的人似乎重定向到同一个东西。因此,如果我说localhost/accounts/gmills@gome.com页面,它会重定向到404页面,即使它存在于我的数据库中。我希望使用电子邮件或用户名来区分用户,而不是仅使用id字段。

我可以使用你的建议来解决这个问题并让它工作,这样它只能在Rails 4.0中的nil页面上达到404。

MYSQL数据库

MySQL Table account

id      first_name     last_name    email
1       gill       mills    gmills@gome.com
2       jib        jab          jjab@morris.com

帐户控制器

class AccountsController < ApplicationController
   respond_to :html
      before_filter :load_findaccount, :only => [:show, :edit, :update]

   def index #Displays all of the names that are stored in the database
      @accounts = Account.all
   end

   def new #allows the creation of new Name accounts
      @account = Account.new
      #new_account_path(:first_name
   end

   def create
      @account =Account.new(account_params)

      #@account = Account.new(params[:account])

      if @account.save
         redirect_to @account
      else
         render "new"
      end
   end

   def show
      @account = Account.find_by_email(params[:id])
      if @account.nil?
         render "shared/404"
      else
         respond_with(@account)
      end
   end


   private
      # Using a private method to encapsulate the permissible parameters is
      # just a good pattern since you'll be able to reuse the same permit
      # list between create and update. Also, you can specialize this method
      # with per-user checking of permissible attributes.
      def account_params
         params.require(:account).permit(:first_name, :last_name, :email)
      end

      def load_findaccount
         #params[:id] remains fixed but find_by_id changes to username
         #@account = Account.find_by(params[:id])
         #user_path(user)
      end
end


#   def create
#      @groupee = Groupee.new(params[:groupee])

 #     if @groupee.save
 #        session[:groupee_id] = @groupee.id
 #        flash[:success] = "Welcome #{@groupee.username} to MusicBand"
 #        redirect_to @groupee, :notice => "Groupee Account successfully created." #Changing users to user redirects to a specific user page
 #     else
 #        render "new"
 #     end
 #  end

帐户模型

class Account < ActiveRecord::Base

       def to_param  # overridden
          email
       end


   validates :first_name, :presence => true
   validates :last_name, :presence => true
   validates :email, :presence => true, :uniqueness =>{:case_sensitive => false}
end

显示视图

<h1>Displays a specific name_account on the server</h1>

<h1>First_name</h1>
<%= @account.first_name %>
<h1>Last_name</h1>
<%= @account.last_name %>
<h1>Email</h1>
<%= @account.email %>

索引视图

<h1>Displays all of the name_accounts on the server</h1>
<table>
   <tr>
      <td>First_name</td>
      <td>Last_name</td>
      <td>Email</td>
   </tr>
<% @accounts.each do |account| %>
   <tr>
      <td><%= account.first_name %></td>
      <td><%= account.last_name %></td>
      <td><%= account.email %></td>
   </tr>
<% end %>
</table>

404页

<!DOCTYPE html>
<html>
<head>
  <title>The page you were looking for doesn't exist (404)</title>
  <style>
  body {
    background-color: #EFEFEF;
    color: #2E2F30;
    text-align: center;
    font-family: arial, sans-serif;
  }

  div.dialog {
    width: 25em;
    margin: 4em auto 0 auto;
    border: 1px solid #CCC;
    border-right-color: #999;
    border-left-color: #999;
    border-bottom-color: #BBB;
    border-top: #B00100 solid 4px;
    border-top-left-radius: 9px;
    border-top-right-radius: 9px;
    background-color: white;
    padding: 7px 4em 0 4em;
  }

  h1 {
    font-size: 100%;
    color: #730E15;
    line-height: 1.5em;
  }

  body > p {
    width: 33em;
    margin: 0 auto 1em;
    padding: 1em 0;
    background-color: #F7F7F7;
    border: 1px solid #CCC;
    border-right-color: #999;
    border-bottom-color: #999;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    border-top-color: #DADADA;
    color: #666;
    box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
  }
  </style>
</head>

<body>
  <!-- This file lives in public/404.html -->
  <div class="dialog">
    <h1>The page you were looking for doesn't exist.</h1>
    <p>You may have mistyped the address or the page may have moved.</p>
  </div>
  <p>If you are the application owner check the logs for more information.</p>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

看起来您已覆盖to_param,因此您可以在参数中使用电子邮件而不是ID。尝试在show动作中使用puts语句来检查参数。我的猜测是你需要使用params[:email],而不是params[:id]

   def show
      puts "*************************"
      puts params
      @account = Account.find_by_email(params[:email])
      if @account.nil?
         render "shared/404"
      else
         respond_with(@account)
      end
   end

答案 1 :(得分:0)

我通过回到之前版本的rails解决了这个问题。 Rails 4.0不够强大,仍然处于测试阶段,所以我现在正在使用Rails 3.2.13。