问题描述
如何将数据从控制器传递到模型?
How do you pass data from a controller to a model?
在我的application_controller
中,我抓住用户的位置(州和城市),并包含一个before_filter
,以使其可以通过
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
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
方法request.location.city
和request.location.state
在模型中不起作用.我知道其他所有内容都可以正常工作,因为如果在def_community
下将@city
和@state
定义为字符串,则一切正常,除了我没有动态变量,只是在模型中放置了一个字符串.另外,我知道请求正在控制器/视图中运行,因为我可以让它们显示正确的动态信息.问题只是将数据从控制器传递到模型.非常感谢您的宝贵时间.
The methods request.location.city
and request.location.state
do not function from the model. 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. 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.
推荐答案
您正在努力解决的概念是 MVC体系结构,它与职责分离有关.模型应该处理与数据库(或其他后端)的交互,而无需任何关于它们所使用的上下文的知识(无论是HTTP请求还是其他),视图不需要了解后端和控制器处理两者之间的互动.
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.
因此,对于您的Rails应用而言,视图和控制器可以访问request
对象,而模型则不能.如果要将信息从当前请求传递到模型,则取决于您的控制器.我将按如下方式定义您的add_community
:
So in the case of your Rails app, the views and controllers have access to the request
object, 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_community
as follows:
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
然后在您的控制器中:
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
我不想直接传递request
对象,因为这确实保持了模型与当前请求的分离. User
模型不需要了解request
对象或它们如何工作.它所知道的只是得到了city
和state
.
I prefer not to pass the request
object directly, because that really maintains the separation of the model from the current request. The User
model doesn't need to know about request
objects or how they work. All it knows is it's getting a city
and a state
.
希望有帮助.
这篇关于如何使用Ruby on Rails将数据从控制器传递到模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!