路由错误没有路由匹配[PATCH]“/locations"; [英] Routing Error No route matches [PATCH] "/locations"

查看:44
本文介绍了路由错误没有路由匹配[PATCH]“/locations";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试更新位置项后遇到路线错误

我正在使用 before_action 来设置位置 ID

before_action :set_location, only: [:show, :edit, :update, :destroy]私人的def set_location@location = Location.find(params[:id])结尾

这是我的控制器我只有更新新的和删除的工作有问题

def 创建@location = Location.new(location_params)response_to do |格式|如果@location.saveformat.html { redirect_to @location,注意:'位置已成功创建.'}format.json { 渲染动作:'show',状态::已创建,位置:@location }别的format.html { 渲染动作:'新' }format.json { 渲染 json: @location.errors, 状态: :unprocessable_entity }结尾结尾

结束

def 更新response_to do |格式|如果@location.update(location_params)format.html { redirect_to @location,注意:'位置已成功更新.'}format.json { 头:no_content }别的format.html { 渲染动作:'编辑' }format.json { 渲染 json: @location.errors, 状态: :unprocessable_entity }结尾结尾结尾

这里是我的路由是如何定义的

路线路由从上到下优先匹配辅助 HTTP 动词路径控制器#Action路径/网址static_pages_home_path GET/static_pages/home(.:format) static_pages#homestatic_pages_help_path GET/static_pages/help(.:format) static_pages#helpstatic_pages_about_path GET/static_pages/about(.:format) static_pages#abo utroot_path GET/static_pages#homelocation_path GET/locations(.:format)locations#indexPOST/locations(.:format) 位置#createnew_location_path GET/locations/new(.:format) 位置#newedit_location_path GET/locations/:id/edit(.:format) 位置#editlocation_path GET/locations/:id(.:format) 位置#showPATCH/locations/:id(.:format)locations#updatePUT/locations/:id(.:format)locations#update删除/locations/:id(.:format) 位置#destroy

不确定是否需要添加其他内容

我想我可以添加跟踪

actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'机架 (1.5.2) lib/rack/runtime.rb:17:in `call'activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'rack (1.5.2) lib/rack/lock.rb:17:in `call'actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'rack (1.5.2) lib/rack/sendfile.rb:112:in `call'railties (4.0.2) lib/rails/engine.rb:511:in `call'railties (4.0.2) lib/rails/application.rb:97:in `call'rack (1.5.2) lib/rack/lock.rb:17:in `call'rack (1.5.2) lib/rack/content_length.rb:14:in `call'机架 (1.5.2) lib/rack/handler/webrick.rb:60:in `service'/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

_表单文件

<%= form_for @location,:url =>location_path, :html =>{ :multipart =>真 , :class =>'form-horizo​​ntal' } 做 |f|%><div class="control-group"><%= f.label :latitude, :class =>'控制标签'%><div class="控件"><%= f.text_field :latitude, :class =>'text_field' %>

<div class="control-group"><%= f.label :longitude, :class =>'控制标签'%><div class="控件"><%= f.text_field :longitude, :class =>'text_field' %>

<div class="control-group"><%= f.label :address, :class =>'控制标签'%><div class="控件"><%= f.text_field :address, :class =>'text_field' %>

<div class="control-group"><%= f.label :description, :class =>'控制标签'%><div class="控件"><%= f.text_field :description, :class =>'text_field' %>

<div class="control-group"><%= f.label :title, :class =>'控制标签'%><div class="控件"><%= f.text_field :title, :class =>'text_field' %>

<div class="control-group"><%= f.label :user_id, :class =>'控制标签'%><div class="控件"><%= f.text_field :user_id, :class =>'text_field' %>

<div class="control-group"><%= f.label :rating, :class =>'控制标签'%><div class="控件"><%= f.text_field :rating, :class =>'text_field' %>

<div class="control-group"><%= f.label :no_rates, :class =>'控制标签'%><div class="控件"><%= f.text_field :no_rates, :class =>'text_field' %>

<%= f.file_field :picture %><div class="form-actions"><%= f.submit nil , :class =>'btn btn-primary'%><%= link_to t('.cancel', :default => t("helpers.links.cancel")),location_path, :class =>'btn'%>

<%结束%>

解决方案

您为表单标签指定了一个目标 URL,而不是让 Rails 为您选择一个.因此,您的表单不是向 /locations/:id 发出 PATCH 请求,而是尝试向您指定的 URL 发出 PATCH 请求,该 URL 没有附加 PATCH 路由.

只需从表单标签中删除 :url 选项,如下所示:

<%= form_for @location, :html =>{ :multipart =>真 , :class =>'form-horizo​​ntal' } 做 |f|%>

I'm getting a route error after trying to update a location item

I'm using before_action to set location id

before_action :set_location, only: [:show, :edit, :update, :destroy]

private
def set_location
  @location = Location.find(params[:id])
end

here is my controller I only have trouble with update new and delete work fine

def create
@location = Location.new(location_params)
respond_to do |format|
  if @location.save
    format.html { redirect_to @location, notice: 'Location was successfully created.' }
    format.json { render action: 'show', status: :created, location: @location }
  else
    format.html { render action: 'new' }
    format.json { render json: @location.errors, status: :unprocessable_entity }
  end
end

end

def update
respond_to do |format|
  if @location.update(location_params)
    format.html { redirect_to @location, notice: 'Location was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @location.errors, status: :unprocessable_entity }
  end
 end
end

here is how my routs are defined

Routes

Routes match in priority from top to bottom

Helper  HTTP Verb   Path    Controller#Action
Path / Url          
static_pages_home_path   GET     /static_pages/home(.:format)    static_pages#home
static_pages_help_path   GET     /static_pages/help(.:format)    static_pages#help
static_pages_about_path  GET     /static_pages/about(.:format)   static_pages#abo ut
root_path                    GET     /   static_pages#home  
locations_path           GET     /locations(.:format)    locations#index
                             POST    /locations(.:format)    locations#create
new_location_path            GET     /locations/new(.:format)    locations#new
 edit_location_path          GET     /locations/:id/edit(.:format)   locations#edit
location_path             GET    /locations/:id(.:format)    locations#show
                             PATCH   /locations/:id(.:format)    locations#update
                             PUT     /locations/:id(.:format)    locations#update
                             DELETE  /locations/:id(.:format)    locations#destroy

not sure if i need to add anything else

I guess i can add the trace

actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.2) lib/rails/engine.rb:511:in `call'
railties (4.0.2) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/home/johnk/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

_form file

<%= form_for @location,:url => locations_path, :html => { :multipart => true , :class => 'form-horizontal' } do |f| %>
  <div class="control-group">
    <%= f.label :latitude, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :latitude, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :longitude, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :longitude, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :address, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :address, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :description, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :description, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :title, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :title, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :user_id, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :user_id, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :rating, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :rating, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :no_rates, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :no_rates, :class => 'text_field' %>
    </div>
  </div>

   <%= f.file_field :picture %>



  <div class="form-actions">
    <%= f.submit nil , :class => 'btn btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                locations_path, :class => 'btn' %>
  </div>


<% end %>

解决方案

You specified a target URL for your form tag instead of letting Rails pick one for you. Because of that, instead of making a PATCH request to /locations/:id your form is trying to make one to the URL you specified, which does not have a PATCH route attached to it.

Simply remove the :url option from your form tag, like so:

<%= form_for @location, :html => { :multipart => true , :class => 'form-horizontal' } do |f| %>

这篇关于路由错误没有路由匹配[PATCH]“/locations";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆