How do you pass data from a controller to a model with Ruby on Rails?
model, model-view-controller, ruby, ruby-on-rails, ruby-on-rails-3
Solution
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.
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
And then in your controller:
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
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`.
Hope that helps.
Problem
How do you pass data from a controller to a model? 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 ``` 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.