如何使用 Ruby on Rails 将数据从控制器传递到模型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10237388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:15:43  来源:igfitidea点击:

How do you pass data from a controller to a model with Ruby on Rails?

ruby-on-railsrubyruby-on-rails-3model-view-controllermodel

提问by Laser

How do you pass data from a controller to a model?

如何将数据从控制器传递到模型?

In my application_controllerI grab the user's location (state and city) and include a before_filterto make it accesible in all my controllers via

在我的application_controller我获取用户的位置(州和城市)并包含一个before_filter以使其在我的所有控制器中都可以通过

before_filter :community

def community
    @city = request.location.city
    @state = request.location.state
    @community = @city+@state
  end

Then I try add the data retrieved in the controller to the model via:

然后我尝试通过以下方式将在控制器中检索到的数据添加到模型中:

before_save :add_community

def add_community
      self.community = @community
    end

The data, however, never makes its way from the controller to the model. If I use:

然而,数据永远不会从控制器传到模型。如果我使用:

def add_community
    @city = request.location.city
    @state = request.location.state
    @community = @city+@state
    self.community = @community
  end

The methods request.location.cityand request.location.statedo not function from the model. I know that everything else is working because if I define @cityand @stateas strings, under def_community, then everything works, except I don't have a dynamic variable, just a string placed in the model. Also, I know the requests are working in the controller/views, because I can get them to display the proper dynamic info. The issue is simply getting the data from the controller to the model. Thanks a lot for your time.

方法request.location.cityrequest.location.state不从模型中起作用。我知道其他一切都在工作,因为如果我将@city和定义@state为字符串,则在 下def_community,则一切正常,除了我没有动态变量,只是在模型中放置了一个字符串。另外,我知道请求在控制器/视图中工作,因为我可以让它们显示正确的动态信息。问题只是将数据从控制器获取到模型。非常感谢您的时间。

回答by tsherif

The concept you're wrestling with is MVC architecture, which is about separating responsibilities. The models should handle interaction with the DB (or other backend) without needing any knowledge of the context they're being used in (whether it be a an HTTP request or otherwise), views should not need to know about the backend, and controllers handle interactions between the two.

您正在讨论的概念是MVC 架构,它是关于分离职责的。模型应该处理与数据库(或其他后端)的交互,而无需了解它们正在使用的上下文(无论是 HTTP 请求还是其他),视图不应该需要了解后端和控制器处理两者之间的交互。

So in the case of your Rails app, the views and controllers have access to the requestobject, while your models do not. If you want to pass information from the current request to your model, it's up to your controller to do so. I would define your add_communityas follows:

因此,在您的 Rails 应用程序中,视图和控制器可以访问该request对象,而您的模型则不能。如果您想将当前请求中的信息传递给您的模型,则由您的控制器来执行。我会定义你add_community如下:

class User < ActiveRecord::Base

  def add_community(city, state)
    self.community = city.to_s + state.to_s  # to_s just in case you got nils
  end

end

And then in your controller:

然后在您的控制器中:

class UsersController < ApplicationController

  def create  # I'm assuming it's create you're dealing with
    ...
    @user.add_community(request.location.city, request.location.state)
    ...
  end
end

I prefer not to pass the requestobject directly, because that really maintains the separation of the model from the current request. The Usermodel doesn't need to know about requestobjects or how they work. All it knows is it's getting a cityand a state.

我不喜欢request直接传递对象,因为这确实保持了模型与当前请求的分离。该User模型不需要了解request对象或它们是如何工作的。它所知道的是它正在获取 acity和 a state

Hope that helps.

希望有帮助。

回答by Larry K

The class instance variables (those that start with @) in the controllers are separate from those in the models. This is the Model vs the Controller in MVC architecture. The Model and Controller (and view) are separated.

控制器中的类实例变量(以@ 开头的那些)与模型中的那些是分开的。这是MVC架构中的模型与控制器。模型和控制器(和视图)是分开的。

You move info from a controller to a model explicitly. In Rails and other object oriented systems, you have several options:

您明确地将信息从控制器移动到模型。在 Rails 和其他面向对象的系统中,您有多种选择:

Use function parameters

使用函数参数

# In the controller
user = User.new(:community => @community)

# In this example, :community is a database field/column of the 
# User model    

Docs

文档

Use instance variables attribute setters

使用实例变量属性设置器

# In the controller
user = User.new
user.community = @community
# same as above, :community is a database field

Passing data to models when the data is not a database field

当数据不是数据库字段时将数据传递给模型

# In the model
class User < ActiveRecord::Base
  attr_accessor :community
  # In this example, :community is NOT a database attribute of the?
  # User model. It is an instance variable that can be used
  # by the model's calculations. It is not automatically stored in the db

# In the controller -- Note, same as above -- the controller 
# doesn't know if the field is a database attribute or not. 
# (This is a good thing)
user = User.new
user.community = @community

Docs

文档