“新建”渲染重定向到意外的URL

时间:2013-02-15 18:37:17

标签: ruby-on-rails ruby-on-rails-3

我的应用程序有一个页面,允许用户通过表单向我发送电子邮件(不会发布我的地址)。它与电子邮件控制器相关联,并且是#new动作,我将其重新路由到'/ contact'。

奇怪的是,如果电子邮件因任何原因没有发送(用户将字段留空或者我输入的数学验证码失败),当控制器再次呈现“新”时,网址变为'/电子邮件。我完全无法理解这一点。一些相关的代码:

#emails_controller.rb

require 'math_captcha'

class EmailsController < ApplicationController

    def new
    @captcha = MathCaptcha.new
    @email = Email.new
  end

    def create
    @captcha = MathCaptcha.decrypt(params[:captcha_secret])
    @email = Email.new(params[:email])

    unless @captcha.correct?(params[:captcha]) || params[:captcha] == "21261"
        flash.now[:error] = "Please make sure you answered the math question correctly."
        render :new
    else
      if @email.save
        Contact.contact_message(@email).deliver
        flash[:success] = "Your email has sent! I'll try to get back to you shortly."
        redirect_to root_path
      else
        flash.now[:error] = "Please correct the highlighted errors and try again."
        render :new
      end
    end

    end

end

#routes.rb

resources :users, :posts, :sessions, :emails

#...

match '/contact', to: 'emails#new', as: 'contact'

在我尝试修复它之前添加“as contact”之前,存在同样的问题。有什么想法吗?

编辑 - 每个垃圾收集的请求(抱歉延迟;我的计算机附近没有),这里是导航到/联系并提交不成功的电子邮件尝试的开发日志(刷新和更改URL到/电子邮件):

Started GET "/contact" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Processing by EmailsController#new as HTML
  Rendered emails/new.html.erb within layouts/application (1.5ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  [1m[35mUser Load (0.1ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  Rendered layouts/_header.html.erb (2.0ms)
Completed 200 OK in 20ms (Views: 19.5ms | ActiveRecord: 0.1ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /application.css - 304 Not Modified (2ms)


Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /custom.css - 304 Not Modified (2ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /jquery_ujs.js - 304 Not Modified (0ms)


Started GET "/assets/angular.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /angular.js - 304 Not Modified (0ms)


Started GET "/assets/pygments.css?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /pygments.css - 304 Not Modified (0ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /jquery.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-transition.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-affix.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-alert.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-button.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-carousel.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-collapse.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-modal.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-dropdown.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-scrollspy.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-popover.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-tab.js - 304 Not Modified (46ms)


Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-typeahead.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /bootstrap-tooltip.js - 304 Not Modified (0ms)


Started GET "/assets/emails.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /emails.js - 304 Not Modified (0ms)


Started GET "/assets/posts.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /posts.js - 304 Not Modified (0ms)


Started GET "/assets/expand.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /expand.js - 304 Not Modified (0ms)


Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /sessions.js - 304 Not Modified (0ms)


Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /users.js - 304 Not Modified (0ms)


Started GET "/assets/statics.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /statics.js - 304 Not Modified (0ms)


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:40 -1000
Served asset /application.js - 304 Not Modified (0ms)


Started GET "/posts.json" for 127.0.0.1 at 2013-02-16 08:46:41 -1000
Processing by PostsController#index as JSON
  [1m[36mUser Load (0.1ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m  [["id", 2]]
  [1m[35mPost Load (0.2ms)[0m  SELECT "posts".* FROM "posts" 
Completed 200 OK in 7ms (Views: 3.3ms | ActiveRecord: 0.4ms)


Started GET "/assets/sign_out.png" for 127.0.0.1 at 2013-02-16 08:46:41 -1000
Served asset /sign_out.png - 304 Not Modified (0ms)


Started POST "/emails" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Processing by EmailsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hjd4q/tvu6IhqpdC7rPMkS21s0GTiqILqOA8ZkLKXsk=", "email"=>{"name"=>"", "address"=>"", "subject"=>"", "content"=>""}, "captcha_secret"=>"xmuKN1l8HVyPlbsfymoVWTieuFYYO3qcXoUUawh8vyw=\r\n", "captcha"=>"", "commit"=>"Send"}
  Rendered emails/new.html.erb within layouts/application (1.7ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  [1m[36mUser Load (0.1ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m  [["id", 2]]
  Rendered layouts/_header.html.erb (2.5ms)
Completed 200 OK in 25ms (Views: 22.0ms | ActiveRecord: 0.1ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /application.css - 304 Not Modified (2ms)


Started GET "/assets/pygments.css?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /pygments.css - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-transition.js - 304 Not Modified (0ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /jquery_ujs.js - 304 Not Modified (0ms)


Started GET "/assets/angular.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /angular.js - 304 Not Modified (0ms)


Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /custom.css - 304 Not Modified (1ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /jquery.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-affix.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-button.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-alert.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-carousel.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-modal.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-collapse.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-dropdown.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-scrollspy.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-tooltip.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-tab.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-typeahead.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap.js - 304 Not Modified (0ms)


Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /bootstrap-popover.js - 304 Not Modified (0ms)


Started GET "/assets/expand.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /expand.js - 304 Not Modified (0ms)


Started GET "/assets/emails.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /emails.js - 304 Not Modified (0ms)


Started GET "/assets/posts.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /posts.js - 304 Not Modified (0ms)


Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /users.js - 304 Not Modified (0ms)


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /application.js - 304 Not Modified (1ms)


Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /sessions.js - 304 Not Modified (0ms)


Started GET "/assets/statics.js?body=1" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /statics.js - 304 Not Modified (0ms)


Started GET "/posts.json" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Processing by PostsController#index as JSON
  [1m[35mUser Load (0.1ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
  [1m[36mPost Load (0.2ms)[0m  [1mSELECT "posts".* FROM "posts" [0m
Completed 200 OK in 5ms (Views: 2.3ms | ActiveRecord: 0.3ms)


Started GET "/assets/sign_out.png" for 127.0.0.1 at 2013-02-16 08:46:44 -1000
Served asset /sign_out.png - 304 Not Modified (0ms)

1 个答案:

答案 0 :(得分:1)

预计会在地址栏中显示“/ email”,因为您刚刚发布到该网址。如果你真的想要显示'/ contact',你应该设置一个POST路线,如:

获取'/ contact',以:'电子邮件#new' 发布'/ contact',以:'发送电子邮件#create'

也许还有另一种更简洁的方法,但这应该有效。