简体   繁体   English

我需要为Ruby on Rails Controller调用生成控制器吗?

[英]Do I need to generate a controller for a Ruby on Rails Controller call?

I'm setting a session variable to a user's geographical state. 我正在将会话变量设置为用户的地理状态。 I have to use a session variable because I run code on the server specific to that user on page load and I need to know where they are. 我必须使用一个会话变量,因为我在页面加载时在特定于该用户的服务器上运行代码,我需要知道它们的位置。 This code is set up to just update the session variable. 设置此代码只是为了更新会话变量。

states_controller.rb States_controller.rb

class StatesController < ApplicationController
  def loc
    session[:location] = params[:location]
  end
end

routes.rb routes.rb

post "states/loc" => "states#loc"

The code routes properly and the session variable is updated. 该代码正确路由,并更新了会话变量。

However, when the process is complete I get a 500 error in the console "Missing Template" in the views directory. 但是,该过程完成后,在views目录的控制台“ Missing Template”中收到500错误。 I haven't seen any tutorials tell users to call the command "rails generate controller" and I'm in the unique situation where I can't call this command. 我还没有看到任何教程告诉用户调用命令“ rails generate controller”,而且我处于无法调用此命令的特殊情况。

What possible side effect are there to ignoring this 500 error? 忽略此500错误有什么可能的副作用?

*I'm running an older version of ruby and rails. *我运行的是旧版本的红宝石和铁轨。

What possible side effect are there to ignoring this 500 error? 忽略此500错误有什么可能的副作用?

Each request is crashing your rails server. 每个请求都使您的Rails服务器崩溃。 Thats not good. 这不好。 Since it means that some cases it may have to restart after every failed request - that eats resources like Homer Simpson at a buffet. 因为这意味着在某些情况下,它可能必须在每个失败的请求之后重新启动-这样会像自助餐般吃掉Homer Simpson之类的资源。

Your app should not be raising uncaught exceptions that cause 500 errors - thats just decent professional practice. 您的应用程序不应引发未捕获的异常,该异常会导致500个错误-那只是体面的专业实践。

So how do I fix it? 那么我该如何解决呢?

Simple, if you don't want the default behavior of rendering a view tell rails to do something else: 很简单,如果您不希望呈现视图的默认行为告诉Rails做其他事情:

class StatesController < ApplicationController
  def loc
    session[:location] = params[:location]
    head :created
  end
end

This sends an empty response with the 201 - CREATED http header. 这将发送带有201 - CREATED http标头的空响应。

See Rails Guides - Layouts and Rendering in Rails 请参见《 Rails指南-Rails中的布局和渲染》

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

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