获取"没有路线匹配[PATCH]"即使方法和路线存在,也会出错

时间:2016-03-07 18:07:07

标签: ruby-on-rails forms ruby-on-rails-4 configuration routes

我正在使用Rails 4.2.5。我在“./app/views/user_objects/_form.html.erb”页面中有此内容

<%= form_for(@user_object, :url => url_for(:controller => 'user_objects', :action => 'create')) do |f| %>
    …
    <%= button_to "Save", { :action => "create" }, :method => :post, :class => 'button' %>
  </div>
<% end %>

但是,当我点击此页面上的“保存”按钮时,我收到以下错误...

F, [2016-03-07T12:01:15.816006 #1335] FATAL -- : 
ActionController::RoutingError (No route matches [PATCH] "/user_objects"):
  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5) lib/rails/rack/logger.rb:22:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5) lib/rails/engine.rb:518:in `call'
  railties (4.2.5) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  puma (3.0.2) lib/puma/configuration.rb:221:in `call'
  puma (3.0.2) lib/puma/server.rb:561:in `handle_request'
  puma (3.0.2) lib/puma/server.rb:406:in `process_client'
  puma (3.0.2) lib/puma/server.rb:271:in `block in run'
  puma (3.0.2) lib/puma/thread_pool.rb:111:in `block in spawn_thread'

我不知道为什么我会这样做。我在我的控制器中定义了这个方法...

  # POST /user_objects
  # POST /user_objects.json
  def create
    @current_user = User.find(session["user_id"])
    @date = Date.strptime(user_object_params[:day], "%m/%d/%Y")
    @mysql_formatted_date = @date.strftime('%Y-%m-%d')
    @user_object = Userobject.find_by_user_id_and_object_id_and_day(@current_user.id, user_object_params[:object_id], @mysql_formatted_date)
    if (@user_object.nil?) then
      @user_object = Userobject.new(user_object_params)
      @user_object.day = @date
      @user_object.user_id = @current_user.id
    else
      @user_object.total = user_object_params[:total]
    end
    # Render the response
    @objects = object.all
    respond_to do |format|
      if @user_object.save
        puts "Saving data" 
        format.html { render :action => 'index', :id => @user_object, notice: 'Information saved successfully.' }
        format.json { render :index, status: :created, location: @user_object }
      else
        format.html { render :index }
        format.json { render json: @user_object.errors, status: :unprocessable_entity }
      end
    end
  end

那是什么给出的?我在config / routes.rb文件中定义了这个

Rails.application.routes.draw do
  resources :user_objects do
    get :find_by_user_object_and_day, on: :collection
    get :find_totals, on: :collection
  end

3 个答案:

答案 0 :(得分:0)

您指定它在表单中使用create,然后尝试使用该表单进行更新。

看看这一行:

<%= form_for(@user_object, :url => url_for(:controller => 'user_objects', :action => 'create')) do |f| %>

答案 1 :(得分:0)

问题是

<%= form_for(@user_object, :url => url_for(:controller => 'user_objects', :action => 'create')) do |f| %>

在您最初创建对象时效果很好,但在您想要更新对象时却无效。

将其更改为

<%= form_for @user_object do |f| %>

并添加

resources :user_objects, except: :update do
  member do
    patch :create
    put :create
  end
end

到routes.rb

并在user_objects控制器的#create操作中找出区分创建和更新操作。

同样,你正在做的是反对Rails惯例,不可取。

答案 2 :(得分:0)

由于您在路径文件中使用资源,因此rails会自动将请求类型分配给操作(CRUD)。因此,create是一个帖子,更新是放置(或补丁)。如果要在同一个操作中同时执行这两个操作,则必须在控制器中创建一个处理这两种情况的新方法,然后将表单提交到需要在routes文件中定义的路由。