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

查看:107
本文介绍了如何使用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在模型中不起作用.我知道其他所有内容都可以正常工作,因为如果在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.

推荐答案

您正在努力解决的概念是

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对象或它们如何工作.它所知道的只是得到了citystate.

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天全站免登陆