Rails 4 URLGeneration错误问题

时间:2016-06-29 08:18:28

标签: ruby-on-rails ruby-on-rails-4

我正在构建一个应用程序,我决定使用Devise和CanCanCan ..我已经设置了应用程序,只有管理员或超级用户才能添加新用户。

我的用户是从位于application.html.erb页面上的模式创建的。

我可以创建新用户没问题,一切正常。然而,现在我试图设置它,用户可以再次从模态编辑他们的个人资料..这是我“撞墙”的地方。

This is the github repo for this project feel free to take a look if you want to.

我一遍又一遍地经历过代码,但无济于事!

  

编辑#1 - ScreenShot   enter image description here   这是我收到的错误消息。

No route matches {:action=>"update", :controller=>"admins"} missing required keys: [:id]

在你因为没有正确设置id而跳过我之前,我正在使用虚荣网址的SecureRandom.hex(6)字符串。除了这里,它们似乎在其他地方都很好。

表示编辑用户的rake路由输出。

       edit_user GET    /admins/:id/edit(.:format)        admins#edit
  push_edit_user PATCH  /admins/:id(.:format)             admins#update

这是routes.rb中的自定义路由

  as :user do
    get "admins/new_user" => "admins#new_user", as: :admins_new_user
    post "admins/create_user" => "admins#create_user", as: :admins_create_user
    get "admins/:id/edit" => "admins#edit", as: :edit_user
    patch "admins/:id" => "admins#update", as: :push_user_edit
  end

这是我的用户(管理员)控制器,我有我的创建和更新操作。

class AdminsController < ApplicationController
  before_action :authenticate_user!

  def new_user
    authorize! :manage, User
    @user = User.new
  end

  def create_user
    @user = User.find_by_user_ident(params[:user_id])
    authorize! :manage, User
    @user = User.new(sign_up_params)
    if @user.save
      format.html { redirect_to root_path, notice: 'User has been successfully created.' }
    else

    end
  end

  def edit
    @user = User.find_by_user_ident(params[:user_id])
  end

  def update
    @user = User.find_by_user_ident(params[:user_id])

    if @user.update(account_update_params)
      format.html { redirect_to @user, notice: 'Address was successfully updated.' }

    else

    end
  end

  private

  def sign_up_params
    params.require(:user).permit(:email, :password, :password_confirmation, :user_ident, :f_name, :m_name, :l_name, :dob, :role)
  end

  def account_update_params
    params.require(:user).permit(:email, :password, :password_confirmation, :user_ident, :f_name, :m_name, :l_name, :dob, :role)
  end

  # Sets user to be found by User_Ident #
  def set_user
    @user = User.find_by_user_ident(params[:id])
  end

  # Allow Profile Update w/o Current p/w Confirmation
  def update_resource(resource, params)
    resource.update_without_password(params)
  end

end

并且为了安全起见,这里是错误的完整堆栈跟踪。

ActionController::UrlGenerationError - No route matches {:action=>"update", :controller=>"admins"} missing required keys: [:id]:
  actionpack (5.0.0.rc1) lib/action_dispatch/journey/formatter.rb:50:in `generate'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:629:in `generate'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:660:in `generate'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:707:in `url_for'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:236:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:177:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:295:in `block (2 levels) in define_url_helper'
  app/views/layouts/application.html.erb:168:in `_app_views_layouts_application_html_erb__1028780063709367977_70323361979180'
  actionview (5.0.0.rc1) lib/action_view/template.rb:158:in `block in render'
  activesupport (5.0.0.rc1) lib/active_support/notifications.rb:166:in `instrument'
  actionview (5.0.0.rc1) lib/action_view/template.rb:348:in `instrument'
  actionview (5.0.0.rc1) lib/action_view/template.rb:156:in `render'
  actionview (5.0.0.rc1) lib/action_view/renderer/template_renderer.rb:66:in `render_with_layout'
  actionview (5.0.0.rc1) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
  actionview (5.0.0.rc1) lib/action_view/renderer/template_renderer.rb:14:in `render'
  actionview (5.0.0.rc1) lib/action_view/renderer/renderer.rb:42:in `render_template'
  actionview (5.0.0.rc1) lib/action_view/renderer/renderer.rb:23:in `render'
  actionview (5.0.0.rc1) lib/action_view/rendering.rb:103:in `_render_template'
  actionpack (5.0.0.rc1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
  actionview (5.0.0.rc1) lib/action_view/rendering.rb:83:in `render_to_body'
  actionpack (5.0.0.rc1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
  actionpack (5.0.0.rc1) lib/action_controller/metal/renderers.rb:144:in `render_to_body'
  actionpack (5.0.0.rc1) lib/abstract_controller/rendering.rb:26:in `render'
  actionpack (5.0.0.rc1) lib/action_controller/metal/rendering.rb:36:in `render'
  actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
  activesupport (5.0.0.rc1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
  activesupport (5.0.0.rc1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
  actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
  activerecord (5.0.0.rc1) lib/active_record/railties/controller_runtime.rb:30:in `cleanup_view_runtime'
  actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:43:in `render'
  actionpack (5.0.0.rc1) lib/action_controller/metal/implicit_render.rb:36:in `default_render'
  actionpack (5.0.0.rc1) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'
  actionpack (5.0.0.rc1) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'
  actionpack (5.0.0.rc1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
  actionpack (5.0.0.rc1) lib/abstract_controller/base.rb:181:in `process_action'
  actionpack (5.0.0.rc1) lib/action_controller/metal/rendering.rb:30:in `process_action'
  actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:126:in `call'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:455:in `call'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
  actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (5.0.0.rc1) lib/action_controller/metal/rescue.rb:31:in `process_action'
  actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (5.0.0.rc1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (5.0.0.rc1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
  activerecord (5.0.0.rc1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (5.0.0.rc1) lib/abstract_controller/base.rb:126:in `process'
  actionview (5.0.0.rc1) lib/action_view/rendering.rb:30:in `process'
  actionpack (5.0.0.rc1) lib/action_controller/metal.rb:190:in `dispatch'
  actionpack (5.0.0.rc1) lib/action_controller/metal.rb:262:in `dispatch'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
  actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
  actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `each'
  actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `serve'
  actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:725:in `call'
  actionview (5.0.0.rc1) lib/action_view/digestor.rb:12:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `catch'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (2.0.0.rc1) lib/rack/etag.rb:25:in `call'
  rack (2.0.0.rc1) lib/rack/conditional_get.rb:25:in `call'
  rack (2.0.0.rc1) lib/rack/head.rb:12:in `call'
  rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:222:in `context'
  rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:216:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
  activerecord (5.0.0.rc1) lib/active_record/migration.rb:552:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
  activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
  web-console (3.3.0) lib/web_console/middleware.rb:131:in `call_app'
  web-console (3.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (3.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (3.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
  railties (5.0.0.rc1) lib/rails/rack/logger.rb:36:in `call_app'
  railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `block in call'
  activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
  activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `tagged'
  railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
  rack (2.0.0.rc1) lib/rack/method_override.rb:22:in `call'
  rack (2.0.0.rc1) lib/rack/runtime.rb:22:in `call'
  activesupport (5.0.0.rc1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
  actionpack (5.0.0.rc1) lib/action_dispatch/middleware/static.rb:136:in `call'
  rack (2.0.0.rc1) lib/rack/sendfile.rb:111:in `call'
  railties (5.0.0.rc1) lib/rails/engine.rb:522:in `call'
  puma (3.4.0) lib/puma/configuration.rb:224:in `call'
  puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
  puma (3.4.0) lib/puma/server.rb:406:in `process_client'
  puma (3.4.0) lib/puma/server.rb:271:in `block in run'
  puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'

预先感谢您的协助!如果您还有其他需要,请告诉我们!

  

编辑#2 - 应用程序控制器和模态

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :set_user

  def set_user
    @user = User.find_by_user_ident(params[:user_id])
    @user = User.new
  end

end

<%= form_for(@user, :url => push_user_edit_path) do |f| %>
  <div class="modal fade" id="userEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Create New System User</h4>
        </div>
        <div class="modal-body">
          <div class="field">
            <%= f.label :email %><br />
            <%= f.email_field :email, autofocus: true %>
          </div>

          <div class="field">
            <%= f.label :password %>
            <% if @minimum_password_length %>
            <em>(<%= @minimum_password_length %> characters minimum)</em>
            <% end %><br />
            <%= f.password_field :password, autocomplete: "off" %>
          </div>

          <div class="field">
            <%= f.label :password_confirmation %><br />
            <%= f.password_field :password_confirmation, autocomplete: "off" %>
          </div>

          <div class="field">
            <%= f.label :first_name %><br />
            <%= f.text_field :f_name %>
          </div>

          <div class="field">
            <%= f.label :middle_name %><br />
            <%= f.text_field :m_name %>
          </div>

          <div class="field">
            <%= f.label :last_name %><br />
            <%= f.text_field :l_name %>
          </div>

          <div class="field">
            <%= f.label :date_of_birth %><br />
            <%= f.date_field :dob %>
          </div>

          <br />
          <%= f.collection_select(:role, User::ROLES, :to_s, lambda{|i| i.to_s.humanize}) %>
          <br />
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <%= f.submit "Create User", :class => 'btn btn-primary' %>
        </div>
      </div>
    </div>
  </div>
<% end %>
  

编辑#3 - ActionController服务器输出

ActionController::UrlGenerationError - No route matches {:action=>"update", :controller=>"admins"} missing required keys: [:id]:

这就是输出,因为我甚至无法进入编辑屏幕或应用程序,只要我登录时就会发生这种情况,因为表单在application.html.erb中的模式中

1 个答案:

答案 0 :(得分:0)

我认为您的错误来自Dim index As Integer = 0 Do Dim fileReader As String = My.Computer.FileSystem.ReadAllText(TextBox1.Text).Replace(index & " ", index) My.Computer.FileSystem.WriteAllText(TextBox1.Text, fileReader, False) index += 1 Loop Until index = 50

应该是:

form_for