代码之家  ›  专栏  ›  技术社区  ›  coppola_f

ruby on rails-将错误从3.x移植到4.x

  •  0
  • coppola_f  · 技术社区  · 9 年前

    我已经开始将我的一个应用程序从rails3.x移植到rails4.x。。。。

    当我在开发中启动应用程序时,我收到一个与路线定义相关的错误:

    => Booting WEBrick
    => Rails 4.2.5 application starting in development on http://localhost:3000
    => Run `rails server -h` for more startup options
    => Ctrl-C to shutdown server
    Exiting
    /home/francesco/.rvm/gems/ruby-2.2.2@Best-i-gest_v2/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:549:in `add_route': Invalid route name, already in use: 'app_settings'  (ArgumentError)
    You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 
    http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
    .....
    

    这是我的一部分路线。包含标记为双重定义的路由的rb文件:

    .....
    get 'app_settings' => 'admin/app_settings#index', :as => 'app_settings'
    put 'app_settings' => 'admin/app_settings#index', :as => 'app_settings'
    post 'app_settings' => 'admin/app_settings#index', :as => 'app_settings'
    post 'app_settings/upload' => 'admin/app_settings#upload_logo', :as => 'app_settings/upload'
    .....
    

    我定义了这些路由,因为我将只使用“索引”操作来管理与应用程序设置相关的所有操作(应用程序有一个存储所有设置的数据库记录,该记录在用户第一次加载页面时自动创建,然后在保存时更新),如下所示:

    # only one record here! it will store all the application settings
    def index
      # manages all the controller actions inside the index...
      if request.get?
        # this is a get request... returns the first settings record or a new one if none exists!
        @app_settings = !AppSettings.all.first.nil? ? AppSettings.all.first : AppSettings.new
      elsif request.post?
        # this is a post request, the settings record will be created
        @app_settings = AppSettings.new(params[:app_settings])
        @app_settings.save
      elsif request.put?
        # this will update the existing app_settings record
        @app_settings = AppSettings.find_by_id(params[:app_settings][:id].to_i)
        @app_settings.update_attributes(params[:app_settings])
      end
    
      # renders the index page
      render "index"
    end
    

    我正在寻找一种方法来纠正路线。rb文件(保持我的控制器和视图原样!!)或管理此问题的替代方法!!

    等待您的建议, 非常感谢您的宝贵时间,

    弗朗西斯科

    2 回复  |  直到 9 年前
        1
  •  1
  •   Alexandre Voyer    9 年前

    看起来你在一个方法中有太多的逻辑。我很确定,即使在轨道3上,这也是不可接受的。

    如何使用休息路线(如 rails routing guide )

    resources :app_settings, only: [:index, :update, :create]
    

    这将为索引(geT)、更新(patch)和创建(post)创建三条路径。

    您的控制器现在如下所示:

    def index
      @app_settings = !AppSettings.all.first.nil? ? AppSettings.all.first : AppSettings.new
    end 
    
    def create
      @app_settings = AppSettings.new(params[:app_settings])
      @app_settings.save
    end
    
    def update
      @app_settings = AppSettings.find_by_id(params[:app_settings][:id].to_i)
      @app_settings.update_attributes(params[:app_settings])
    end
    

    也不需要使用渲染“索引”…rails将自动查找/app/app_settings/index.html erb

        2
  •  0
  •   coppola_f    9 年前

    好的,伙计们, 我已经解决了。。。 非常感谢指导我解决这个问题的Alexandre和Ryan。。。。。

    好 Alexandre肯定你是对的!!单个动作中的逻辑太多,所以。。。 这是我的文件的新版本:

    routes.rb:

    ....
    post 'app_settings/upload' => 'admin/app_settings#upload_logo', :as => 'app_settings/upload'
    
    # site admin area
    namespace :admin do
      resources :app_settings, only: [:index, :update, :create]
      resources :users
      ......
    
    end
    .....
    

    如您所见,我已经在admin命名空间中插入了appsettings路由。。

    app_settings_controller.rb:

    # app_settings security settings - used for declarative authorization
    filter_access_to [:index, :create, :update], :require => :manage
    filter_access_to :upload_logo, :require => :manage
    
    # app_settings index method (this replaces the show method so all actions will be managed in the index view page)
    def index
      # this is a get request... returns the first settings record or a new one if none exists!
      @app_settings = !AppSettings.first.nil? ? AppSettings.first : AppSettings.new(id: 1)
      @app_settings.save
    end 
    
    # app_settings create method
    def create
      # this is a post request, the settings record will be created
      @app_settings = AppSettings.new(app_settings_params)
      @app_settings.save
      # renders the index page
      render "index"
    end
    
    # app_settings update method
    def update
      # this will update the existing app_settings record
      @app_settings = AppSettings.find_by_id(params[:app_settings][:id].to_i)
      @app_settings.update_attributes(app_settings_params)
      # renders the index page
      render "index"
    end
    ......
    

    正如您所看到的,我已经实现了您的解决方案,将索引视图保持为应用程序使用的唯一视图,只是为了在应用程序中不做太多更改。。。。 我已经根据您建议的修改相应地修复了declarative_authorization部分,并按照Ryan的建议简化了新记录的创建,但添加了一点额外的内容(我已经指定了记录id,然后保存了记录本身),以防在我看来form_for声明如下的另一个问题:

    <!-- application settings edit form -->
    <%= form_for [:admin, @app_settings] do |app_sett| %>
    .....
    
    .....
    <% end %>
    

    再次感谢大家!!

    希望能一直向你学习,我读了这个网站的一页!!

    当做

    弗朗西斯科