简体   繁体   English

获取在控制器/视图中工作的方法在模型中运行,Ruby on Rails

[英]Getting methods that work in controller/views to function in a model, Ruby on Rails

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. 我已经完成了所有设置,我知道@ city + @ state的值被添加到正确模型中的相应列中。 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. 问题是,似乎request.location.city和request.location.state在控制器和视图中正常运行,但在模型中却没有。

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

When a user is created, rather than creating a string such as "losangelescalifornia", nothing is created. 创建用户时,不会创建诸如“losangelescalifornia”之类的字符串,而是创建任何内容。 When I define @city = "bob" and @state = "cat", all users created have "bobcat" in the appropriate place. 当我定义@city =“bob”和@state =“cat”时,所有创建的用户都在适当的位置有“bobcat”。 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? 所以我的问题是,如何在模型中运行这些方法(请纠正我,如果它不是它们),即request.location.city和request.location.state? Many thanks in advance :) 提前谢谢了 :)

I agree with Rudi's approach, mostly, but I'll offer a little more explanation. 我主要赞同Rudi的做法,但我会提供更多解释。 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_location as follows: 我会按如下方式定义你的add_location

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

And then in your controller: 然后在你的控制器中:

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

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. 希望有所帮助。

request variable is not available in the model since it depends on the current HTTP request. 请求变量在模型中不可用,因为它取决于当前的HTTP请求。 You have to pass to model as param. 你必须传递给模型作为参数。

def your_action
  ...
  @model.add_location(request)
  ...
end

def add_location(request)
  .. 
end

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM