简体   繁体   English

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

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

How do you pass data from a controller to a model? 如何将数据从控制器传递到模型?

In my application_controller I grab the user's location (state and city) and include a before_filter to 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.city and request.location.state do not function from the model. request.location.cityrequest.location.state方法不能从模型中运行。 I know that everything else is working because if I define @city and @state as strings, under def_community , then everything works, except I don't have a dynamic variable, just a string placed in the model. 我知道其他一切都有效,因为如果我将@city@state定义为字符串,在def_community下,那么一切正常,除了我没有动态变量,只是放在模型中的字符串。 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. 非常感谢你的时间。

The concept you're wrestling with is MVC architecture , which is about separating responsibilities. 您正在努力解决的概念是MVC架构 ,即关于分离职责。 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. 模型应该处理与DB(或其他后端)的交互,而无需了解它们正在使用的上下文(无论是HTTP请求还是其他),视图不需要知道后端,以及控制器处理两者之间的相互作用。

So in the case of your Rails app, the views and controllers have access to the request object, while your models do not. 因此,对于您的Rails应用程序,视图和控制器可以访问request对象,而您的模型则不能访问。 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_community as follows: 我会定义你的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 request object directly, because that really maintains the separation of the model from the current request. 我不想直接传递request对象,因为这确实维持了模型与当前请求的分离。 The User model doesn't need to know about request objects or how they work. User模型不需要知道request对象或它们的工作方式。 All it knows is it's getting a city and a state . 它所知道的是它正在建立一个city和一个state

Hope that helps. 希望有所帮助。

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. 这是MVC架构中的模型与控制器。 The Model and Controller (and view) are separated. 模型和控制器(和视图)是分开的。

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 文件

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

相关问题 如何在Ruby on Rails中的模型之间传递数据 - How do you pass data between models in Ruby on Rails 您如何从Rails中的控制器引用另一个类? - How do you reference another class from a controller in ruby on rails? Rails:如何跨视图访问模型和控制器数据? - Rails: How do you access model and controller data across views? 如何将 json 数据从 ruby controller 传递到 javascript(导轨)? - How to pass json data from ruby controller to javascript (Rails 6.1)? 如何限制我从Ruby on Rails中的控制器传递的数据 - How to limit what data I pass in from the controller in Ruby on Rails Ruby on Rails如何将数据从控制器传递到html中的javascript - Ruby on Rails how to pass data from controller to javascript in html 如何在Rails中将其他参数从控制器传递到模型 - How Do I Pass Additional Parameters in Rails from Controller to Model 如何将我的Ruby on Rails模型中的参数(例如,ID,​​用户名,密码)传递给我的PHP Codeigniter控制器 - How do I pass parameters (eg. id, username, password) from my ruby on rails model to my php codeigniter controller 在Ruby on rails上将数据从一个控制器提交到另一个控制器模型 - Submit data from one controller to another controller model in Ruby on rails 如何在 Rails 上的 Ruby 中将两个参数从 controller 传递到 model? - How can I pass two params from controller to model in Ruby on Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM