gpt4 book ai didi

ruby-on-rails - Rails 路径在更新时显示 .id not/id

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:36 25 4
gpt4 key购买 nike

我遇到了路由问题。当我在事件的编辑表单上单击提交时,我被重新路由到带有 .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'

最佳答案

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

关于ruby-on-rails - Rails 路径在更新时显示 .id not/id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34112764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com