Rails路径在更新时显示.id not / id

时间:2015-12-06 00:54:47

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

我遇到路由问题。当我在编辑表单上单击提交事件时,我被重新路由到一个带有.id而不是/ id的页面,这显然不存在并产生错误。任何见解将不胜感激

error: No route matches [PATCH] "/event.2"
    Rails.application.routes.draw do

     root 'welcome#welcome'
     get '/user/:id/' => 'users#main'
     get '/event' => 'events#index'
     get '/event/new/' => 'events#new'
     get '/event/:id/' => 'events#main'
     post 'event' => 'events#create'
     delete '/logout' => 'sessions#destroy'
     post '/submit' => 'welcome#submit'


     resources :events do
         member do
             get :coming, :going
             post :destroy, :update
        end
    end

     resources :users do
         member do
             get :following, :followers
         end
     end
     resources :friends,       only: [:create, :destroy]
     resources :comings,       only: [:create, :destroy]
     resources :relationships,  only: [:create, :destroy]

     get 'auth/:provider/callback', to: 'sessions#create'
     get 'logout', to: 'sessions#destroy'

        enter code here

    end

#events controller methods#
def edit
     @event = Event.find(params[:id])

end


    def update
     @event = Event.find(params[:id])
     respond_to do |format|
         if @event.update(event_params)
             format.html { redirect_to @event }
             else
             format.html { render :edit }
         end
     end
    end

#edit view#
<!DOCTYPE html>
<div class="edit-event">
  <div class="container">

    <h2>Tell us about your dog!</h2>
    <div class="edit-area">
        <%= form_for(@event) do |u| %>
        <div class="field">
            Event Name:
            <%= u.text_field :event_name %>
            Event Place:
            <%= u.text_field :place %>
            <%= u.submit "Submit"%>
        </div>
        <% end %>
    </div>

</div>

更新了路由文件

Rails.application.routes.draw do

 root 'welcome#welcome'
 get '/user/:id/' => 'users#main'
 get '/event' => 'events#index'
 get '/event/new/' => 'events#new'
 get '/event/:id/' => 'events#main'
 post 'event' => 'events#create'
 delete '/logout' => 'sessions#destroy'
 post '/submit' => 'welcome#submit'


resources :events, only: [:edit, :update]

 resources :users do
     member do
         get :following, :followers
     end
 end
 resources :friends,       only: [:create, :destroy]
 resources :comings,       only: [:create, :destroy]
 resources :relationships,  only: [:create, :destroy]

 get 'auth/:provider/callback', to: 'sessions#create'
 get 'logout', to: 'sessions#destroy'

1 个答案:

答案 0 :(得分:1)

我认为在这种情况下,当您执行redirect_to时,它并不确切知道要使用的路线。我建议将resources :events更改为resources :events, only: [:edit, :update]并删除为类似路线创建的手动路径。另一个选项是命名手动路由并在重定向和link_tos中使用它。 get 'event/:id', to: 'events#edit', as: :edit_event