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

时间:2014-06-11 13:59:36

标签: ruby-on-rails validation

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

我有自己的身份验证,当我没有登录应用程序时,我的验证工作正常。也就是说我在上面代码的respond_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

...使用orientation_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%>

这是orientation_registrations路线......

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

1 个答案:

答案 0 :(得分:0)

您需要传入一个方向ID。路径中的:orientation_id是rails期待的参数

而不是

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

你需要像

这样的东西
<%= link_to 'View Registrations', orientation_registrations_path( @registration.orientation) if @registration.orientation %>

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

相关问题