如何使用 Ruby on Rails 将数据从控制器传递到模型? [英] How do you pass data from a controller to a model with Ruby on Rails?

查看:24
本文介绍了如何使用 Ruby on Rails 将数据从控制器传递到模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.cityrequest.location.state 在模型中不起作用.我知道其他一切都在工作,因为如果我将 @city@state 定义为字符串,在 def_community 下,那么一切正常,除了我不'没有动态变量,只有一个放在模型中的字符串.另外,我知道请求在控制器/视图中工作,因为我可以让它们显示正确的动态信息.问题只是将数据从控制器获取到模型.非常感谢您抽出宝贵时间.

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 将数据从控制器传递到模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆