简体   繁体   English

Ruby on Rails - 没有视图的控制器

[英]Ruby on Rails - Controller without Views

Iam new in Ruby on Rails. 我是Ruby on Rails的新手。 Normally I work with other web languages and now (of course) I try to compare it with other languages using on web. 通常我使用其他网络语言,现在(当然)我尝试将其与在网络上使用的其他语言进行比较。

But sometimes i have some problem to understand the philosophy and the character of Ruby on Rails. 但有时我有一些问题需要理解Ruby on Rails的哲学和特性。 Of course i understand the concept of MVC. 当然我理解MVC的概念。

But now Iam not absolutely sure: Is ist OK to create and use a controller without views? 但是现在我不能完全确定:创建和使用没有视图的控制器是否可行? In some cases you need a "class" for some usefull functionality used by other controllers they have views. 在某些情况下,您需要一个“类”来获取其他具有视图的控制器所使用的有用功能。 Or is it a better style to use a module? 或者使用模块是一种更好的风格?

I try to find it out by reading a lot of articles and examples, but didnt find detailed information about this content. 我试着通过阅读很多文章和例子找到它,但没有找到关于这个内容的详细信息。

在开发Ruby On Rails应用程序时,建议将大多数业务逻辑放在模型中 ,因此对于控制器,逻辑需要最小化才能为视图提供信息,因此如果它不适用于查看机会,那么需要一个控制器真的很低。

Is it OK to create and use a controller without views 是否可以创建和使用没有视图的控制器

Yep it's okay. 是的,没关系。

The key thing to keep in mind is that Ruby/Rails is object orientated . 要记住的关键是Ruby / Rails是面向对象的

This means that every single you do with your controllers/models etc should have the "object" you're manipulating at its core. 这意味着您使用控制器/模型等执行的每一个操作都应该具有您正在操作的“对象”。

With this in mind, it means you can have a controller without corresponding views, as sometimes, you just need to manipulate an object and return a simple response (for example with Ajax). 考虑到这一点,这意味着您可以拥有一个没有相应视图的控制器,因为有时候,您只需要操作一个对象并返回一个简单的响应(例如使用Ajax)。

-- -

We often re-use views for different actions (does that count as not having a view): 我们经常重复使用不同操作的视图(这算不算视图):

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   def search
      render :index, layout: false
   end
end

The notion of skinny controller, fat model is sound in principle, you have to account for the times when you may need small pieces of functionality that can only be handled by a controller: skinny controller, fat model的概念原则上是合理的,您必须考虑到您可能需要只能由控制器处理的小功能的时间:

#app/controllers/users_controller.rb
class UsersController < ApplicationController
   def update
      @user = User.find params[:id]
      @user.update
      respond_to do |format|
        format.js {render nothing: true}
        format.html
      end
   end
end

A very rudimentary example of Rails is a drive-thru: 一个非常基本的Rails示例是一个驱动器:

  • view = input interface view =输入界面
  • controller = accepts order & delivers controller =接受订单和交付
  • model = gets order packaged etc in backend model =在后端获取订单打包等

There are times when the controller may not need a view (for example, if you update your order with specific dietry requirements), and thus the notion that every controller action has to have a view is false. 有时控制器可能不需要视图(例如,如果您使用特定的节食要求更新订单),因此每个控制器操作必须具有视图的概念都是错误的。

It's really about making your controller versatile enough to manage your objects correctly. 这真的是让您的控制器具有足够的通用性来正确管理您的对象。

Shared controller methods can be placed in ApplicationController. 共享控制器方法可以放在ApplicationController中。 Also, in Rails 4 there are concerns (app/controllers/concerns/) where you can put the modules with methods that can be used by multiple controllers. 此外,在Rails 4中存在一些问题(app / controllers / concerns /),您可以在其中放置可以由多个控制器使用的方法的模块。

Controller handles request and renders corresponding view template. 控制器处理请求并呈现相应的视图模板。 So controller without view is absolutely nonsense. 所以没有视图的控制器绝对是无稽之谈。

Every time request will execute this controller, it will just end with missing template error, so you will need to create view folder and put empty files with action name inside of it, which is obviously stupid. 每次请求都会执行这个控制器,它只会以缺少模板错误结束,所以你需要创建视图文件夹并在其中放入带有动作名称的空文件,这显然是愚蠢的。

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

相关问题 是否可以在没有 controller 或视图的轨道上使用 ruby 构建应用程序? - Is it possible to build an app with ruby on rails without controller nor views? Ruby on Rails视图,无需混合ruby和html - Ruby on rails views without mixing ruby and html 在Ruby on Rails中如何在没有控制器的情况下使用模型? - How to use model without a controller in Ruby on Rails? Ruby on Rails。 将变量从视图传递到事务控制器 - Ruby on Rails. Pass variable from views to transaction controller 如何在lib文件夹中生成controller / models / views? (Ruby on Rails) - How to generate controller/models/views inside lib folder? (Ruby on Rails) 在Ruby On Rails中,如何删除与特定控制器相关的视图 - In Ruby On Rails, how can I remove views related to a specific controller 控制器上的一个功能有不同的视图 - ruby on rails one function in controller have different views helper_method中的控制器未显示Ruby on Rails视图 - Controller in helper_method not showing views Ruby on Rails [Ruby on Rails]如何通过不同的控制器视图维护URL参数? - [Ruby on rails]How to maintain URL params trough different controller views? 获取在控制器/视图中工作的方法在模型中运行,Ruby on Rails - Getting methods that work in controller/views to function in a model, Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM