ActionController :: RoutingError(无路由匹配[PATCH]“/event.3”):

时间:2018-01-23 13:38:54

标签: ruby-on-rails routes ruby-on-rails-5 patch form-for

我正在尝试使用我的应用程序的bootstrap模式UI执行更新操作。但是,在更新表单中单击“提交”按钮后我获得的路径始终发送到event.3而不是事件/ 3。

我的rake路由显示PATCH操作定向到events /:id。我不确定为什么在单击表单中的提交按钮后,它会将/event.3 accoridng发送到日志。有人可以向我解释我做错了什么吗?

我的完整日历JS代码:

select: function(start,end){
    $.getScript('/event',function(){})

    calendar.fullCalendar('unselect')
},

eventClick: function(event){
    var url = 'events/' + event.id + '/edit';
    $.getScript(url,function(){});
    console.log(url);
}

途径:

resources :events, only:[:create, :index]
get '/event', to: 'events#new', as: '/event'
get 'events/:id/edit', to: 'events#edit'
patch 'events/:id', to: 'events#update'

RAKE ROUTES:

Prefix Verb   URI Pattern                Controller#Action              
events GET    /events(.:format)          events#index
       POST   /events(.:format)          events#create
 event GET    /event(.:format)           events#new
       GET    /events/:id/edit(.:format) events#edit
       PATCH  /events/:id(.:format)      events#update

CONTROLLER:

def edit
  @event = current_user.events.find(params[:id])   
end

def update    
  @event = current_user.events.find(params[:id])
  @event.update_attributes(event_params)   
end

HTML表格

<div class="modal fade" id='edit_event'>
  <div class="modal-dialog">
    <div class ="modal-content custom" id="edit_event">
      <div class="modal-header">
          <i class="fa fa-envelope-open-o fa-3x" aria-hidden="true"></i>
          <h1 class="modal_header_font"> Update Event </h1>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <div class="modal-body">
          <%= form_for @event, :remote => true do |f| %>
            <% if @event.errors.any? %>

              <div id="error_explanation">
              <div class="alert alert-error">The form contains <%= pluralize(@event.errors.count, "error") %></div>
                <ul>
                  <% @event.errors.full_messages.each do |msg| %>
                    <li>* <%= msg %></li>
                  <% end %>
                </ul>
              </div>
            <% end %>

          <div class="form-group row">
            <label class="col-3 col-form-label"><b>Title</b></label>
            <div class="col-9">
              <%= f.text_field :title, :class =>"form-control" %>
            </div>
          </div>

          <div class="form-group row">
            <label class="col-3 col-form-label"><b>Location</b></label>
            <div class="col-9">
              <%= f.text_field :location, :class =>"form-control" %>
            </div>
          </div>

          <div class="form-group row">
            <label class="col-3 col-form-label"><b>Start</b></label>
            <div class="col-9">
              <%= f.date_select :start, :class =>"form-control" %>
            </div>
          </div>

          <div class="form-group row">
            <label class="col-3 col-form-label"><b>End</b></label>
            <div class="col-9">
              <%= f.date_select :end, :class =>"form-control" %>
            </div>
          </div>

          <div class="form-group row">
            <label class="col-3 col-form-label"><b>Remarks</b></label>
            <div class="col-9">
              <%= f.text_area :description, :class =>"form-control" %>
            </div>
          </div>

          <div class="form-group row">
            <%= f.submit "Send", :class =>"col align-self-center btn custom_primary" %>
          </div>

          <% end %>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

问题

当你使用form_for(@event)时,最终计算网址的是polymorphic_url。正如您可以从“功能”部分中看到的那样:

form_for @event

在您的情况下,由于您使用的是event_url(@event)这意味着您致电get '/event', to: 'events#new', as: '/event'

在你的路线中你有这个:

/event

因此,实际调用以计算网址表单的是event/:id而不是resources :events

如何修复

我建议您使用rails约定,然后使用get '/event', to: 'events#new', as: '/event' 。但快速修复应该是这样的:

1)更新

get '/event', to: 'events#new', as: 'new_event'

patch 'events/:id', to: 'events#update' 

2)更新

patch 'events/:id', to: 'events#update', as: 'event'

'events#new'

如果您这样做,请记住将旧的引用更新为{{1}}。

答案 1 :(得分:0)

这一行...

<%= form_for @event, :remote => true do |f| %>

正在修补或投放到@event节目路径。

您可以更改路线以使编辑显式名称...

get 'events/:id/edit', to: 'events#edit', as: 'edit_event'

然后

<%= form_for edit_event_path(@event), :remote => true do |f| %>