简体   繁体   English

Rails-删除路线会破坏动作

[英]Rails - delete route goes to destroy action

I have a Passenger and Driver model with their corresponding controllers (I can't setup a sessions controller to encapsulate both of their logins and logout). 我有一个带有相应控制器的乘客和驾驶员模型(我无法设置会话控制器来封装他们的登录名和注销名)。 I am trying to delete the session when logging out but the link for logging out goes to the destroy action because it has a delete method, but I want it to go the logout action in the passengers controller since the destroy action is for destroying the passenger, not its session. 我试图在注销时删除会话,但是注销链接转到destroy动作,因为它具有delete方法,但是我希望它在乘客控制器中退出注销动作,因为destroy动作用于消灭乘客。 ,而不是会话。

Heres the rails error: 这是rails错误:

Missing template passengers/destroy, application/destroy with {:locale=>[:en], :formats=
>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. 
Searched in: * "/Users/hamed/Coding/BikeMe/app/views"

Here are my passengers routes (I have a duplicate of the same code for drivers as well): 这是我的乘客路线(我也为驾驶员提供了相同的代码):

resources :passengers do
  get '/dashboard' => 'passengers#dashboard'
end

post '/passengers/login'   => 'passengers#login'
delete '/passengers/logout'  => 'passengers#logout'

Heres the link: 这是链接:

<a class="btn btn-primary" data-method="delete" href="/passengers/logout">Logout as 
passenger</a>

Heres the actions in the passengers_controller: 这是passenger_controller中的动作:

def login
  @passenger = Passenger.find_by_email(params[:passenger][:email])
  if @passenger && @passenger.authenticate(params[:passenger][:password])
    session[:passenger_id] = @passenger.id
    render :dashboard
  else
    redirect_to '/passengers/new', :notice => "Invalid login. Try again"
  end
end

def logout
  session[:passenger_id].clear
  redirect_to '/passengers/login'
end

def destroy
end

First of all, what you're describing does make it sound like you'd be better off with two separate controllers. 首先,您所描述的确实听起来好像最好使用两个单独的控制器。 Even the way you describe it, you have to use two different nouns ("delete the session" versus "destroy the passenger"); 即使是描述它的方式,也必须使用两个不同的名词(“删除会话”与“消灭乘客”); consider whether it's possible to move the session-related actions out of this controller. 考虑是否有可能将与会话相关的动作移出此控制器。 I think it would be saner. 我认为这会更明智。

Other than that, I don't see any immediate problems in your code. 除此之外,我在您的代码中看不到任何直接问题。 It's worth asking, have you restarted all relevant services? 值得一问,您是否重新启动了所有相关服务? I often make a small change, freak out when I don't see it reflected in the browser, then realize that I had to touch tmp/restart or something (depending on your web server). 我经常做一个小小的改动,当我发现浏览器中没有反映出它时会发疯,然后意识到我不得不touch tmp/restart或其他东西(取决于您的Web服务器)。

What URL are you loading in order to get the error message you pasted at top? 您要加载什么URL才能获得粘贴在顶部的错误消息? And can you paste in the log or console output for that URL request? 您可以粘贴该URL请求的日志或控制台输出吗? (the stuff that appears in the console when you run rails s ) (运行rails s时控制台中显示rails s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM