获取在控制器/视图中工作的方法以在模型中运行,Ruby on Rails [英] Getting methods that work in controller/views to function in a model, Ruby on Rails

查看:53
本文介绍了获取在控制器/视图中工作的方法以在模型中运行,Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户的位置在位置列下的用户模型中添加字符串.我已经进行了所有设置,以至于我知道@ city + @ state的值已添加到正确模型中的适当列中.问题在于,request.location.city和request.location.state似乎在控制器和视图中正常运行,但在模型中却不正常.

I'm trying to add a string to the user model under a location column, based on the user's location. I have everything setup to the point that I know the value of @city+@state is added to the appropriate column in the correct model. The problem is, it appears that request.location.city and request.location.state function properly in the controller and views, but not in the model.

def add_location
      @city = request.location.city
      @state = request.location.state
      @location = @city+@state
      self.location = @location
    end

创建用户时,不会创建诸如"losangelescalifornia"之类的字符串,而不会创建任何内容.当我定义@city ="bob"和@state ="cat"时,所有创建的用户在适当的位置都有"bobcat".然后我知道,除了这些基于地理位置的方法外,其他所有功能都在起作用.所以我的问题是,如何将这些方法(如果不是这些方法,请更正我)在模型中运行,即request.location.city和request.location.state?提前非常感谢:)

When a user is created, rather than creating a string such as "losangelescalifornia", nothing is created. When I define @city = "bob" and @state = "cat", all users created have "bobcat" in the appropriate place. I know then that everything is functioning except these geolocation based methods. So my question is, how would I get these methods (correct me please if that is not what they are) to function in the model, being request.location.city and request.location.state? Many thanks in advance :)

推荐答案

在大多数情况下,我都同意Rudi的方法,但我将提供更多解释.您正在尝试的概念是 MVC架构,这是关于职责分离的.模型应该处理与数据库(或其他后端)的交互,而无需任何关于它们所使用的上下文的知识(无论是HTTP请求还是其他),视图不需要了解后端和控制器处理两者之间的互动.

I agree with Rudi's approach, mostly, but I'll offer a little more explanation. 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_location定义如下:

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_location as follows:

class User < ActiveRecord::Base

  def add_location(city, state)
    self.location = 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_location(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天全站免登陆