繁体   English   中英

以管理员用户身份登录时,Rails验证失败

[英]Rails validations failing when logged in as admin user

我的Rails 3控制器中具有以下创建操作:

def create
    @registration = Registration.new(params[:registration])

    respond_to do |format|
      if @registration.save
        if @registration.orientation != nil #TODO replace this with an online? method
          format.html { render "scheduling_text.html.erb" }
          format.json { render json: @registration, status: :created, location: @registration }
          NsoMailer.registration_email(@registration).deliver
        else
          format.html { render "online_scheduling_text.html.erb" }
          format.json { render json: @registration, status: :created, location: @registration }
          NsoMailer.online_registration_email(@registration).deliver
        end
      else
        format.html { render action: "new" } 
        format.json { render json: @registration.errors, status: :unprocessable_entity } 
      end
    end
  end

我有自己的身份验证,当我未登录该应用程序时,我的验证会按预期进行。 那就是说我在上面代码的response_to块内。 但是,作为经过身份验证的用户,如果我尝试提交此表单,则验证失败,并显示此错误...

Started POST "/registrations" for 127.0.0.1 at 2014-06-11 09:52:13 -0400
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"PJMFr2FYTYszVndofuZZTaFTmwN8C/41oc6h7ldqfKA=", "registration"=>{"orientation_id"=>"3", "first_name"=>"", "last_name"=>"", "email"=>"", "student_id"=>"", "phone"=>"", "program"=>""}, "commit"=>"Register"}
   (0.1ms)  begin transaction
   (0.0ms)  rollback transaction
  Rendered registrations/_form.html.erb (6.4ms)
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered registrations/new.html.erb within layouts/application (8.4ms)
Completed 500 Internal Server Error in 16ms

ActionController::RoutingError (No route matches {:controller=>"registrations"}):
  app/views/registrations/new.html.erb:9:in `_app_views_registrations_new_html_erb__773571217_97879480'
  app/controllers/registrations_controller.rb:64:in `block (2 levels) in create'
  app/controllers/registrations_controller.rb:52:in `create'


  Rendered /home/johnmlocklear/.rvm/gems/ruby-1.9.3-p374/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms)

这似乎与路由有关吗? 顺便说一句,我的路线看起来像...

/orientations/3/registrations/new

当表单最初加载时,并且验证失败后,路由为...

/registrations

...具有direction_id的隐藏值。 不确定登录后将与用户有什么关系。还请注意,在错误中有一行会重新加载用户。 我没有登录时没有看到该消息,所以这也可能是问题的一部分。

任何帮助表示赞赏!

编辑

这是app / views / registrations / new.html.erb

<h1>Register for New Student Orientation</h1>

<%= render 'form' %>

<%= link_to 'Back', orientations_path %>

<% if current_user %>
  </br>
  <%= link_to 'View Registrations', orientation_registrations_path %>
<%end%>

这是direction_registrations路线...

orientation_registrations GET    /orientations/:orientation_id/registrations(.:format)          registrations#index
                          POST   /orientations/:orientation_id/registrations(.:format)          registrations#create

您需要输入方向ID。 路径中的:orientation_id是rails期望的参数

而不是

<%= link_to 'View Registrations', orientation_registrations_path %>

你需要类似的东西

<%= link_to 'View Registrations', orientation_registrations_path( @registration.orientation) if @registration.orientation %>

您的控制器处理的方向可能为零,但是您拥有当前用户的视图代码假定该方向在那里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM