0

当我在 Rails 4.0 中运行我的 rails show 页面时,我注意到每次无论 id 是否为 nil,它都会重定向到 404.html 页面。虽然这对 nil 来说很好,但确实有数据的那些似乎会重定向到相同的东西。因此,如果我说页面 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>
4

2 回答 2

0

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

于 2013-09-25T06:10:40.253 回答
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
于 2013-08-11T12:25:08.753 回答