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

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

当我在 Rails 4.0 中运行 Rails Show 页面时,我注意到每次无论 id 是否为零,它都会重定向到 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

账户 Controller

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

显示 View

<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 %>

索引 View

<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>

最佳答案

您似乎已覆盖 to_param,因此您可以在参数中使用电子邮件而不是 ID。尝试在 show 操作中使用 put 语句来检查参数。我的猜测是您需要使用 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

关于mysql - 即使数据存在,Rails 4.0 也会重定向到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18168249/

相关文章:

php - 如何在 mysql 中查找 Sage 50 Database v22 中的关系(已转换为 mysql)

php - 显示一天 mysqli 查询每小时的点击次数

php - SQL 查询 -> 将分数(字符串)转换为小数

mysql - 为什么这个sql请求不起作用?

php - redirectToRoute() 方法可以有像 render() 这样的参数吗?

java - tomcat 7,应用程序正在运行但无法访问

authentication - Nginx 身份验证请求意外状态 404

url - 如何在 Dart 中以正确的方式重定向和重新加载?

redirect - CQ5 - 虚 URL 的 Google 分析问题

html - Angular 6 无法从 Assets 加载图像