How do you pass data from a controller to a model with Ruby on Rails?

前端 未结 2 1124
南笙
南笙 2020-12-09 06:26

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_

相关标签:
2条回答
  • 2020-12-09 06:49

    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.

    0 讨论(0)
  • 2020-12-09 06:49

    The class instance variables (those that start with @) in the controllers are separate from those in the models. This is the Model vs the Controller in MVC architecture. The Model and Controller (and view) are separated.

    You move info from a controller to a model explicitly. In Rails and other object oriented systems, you have several options:

    Use function parameters

    # In the controller
    user = User.new(:community => @community)
    
    # In this example, :community is a database field/column of the 
    # User model    
    

    Docs

    Use instance variables attribute setters

    # In the controller
    user = User.new
    user.community = @community
    # same as above, :community is a database field
    

    Passing data to models when the data is not a database field

    # In the model
    class User < ActiveRecord::Base
      attr_accessor :community
      # In this example, :community is NOT a database attribute of the 
      # User model. It is an instance variable that can be used
      # by the model's calculations. It is not automatically stored in the db
    
    # In the controller -- Note, same as above -- the controller 
    # doesn't know if the field is a database attribute or not. 
    # (This is a good thing)
    user = User.new
    user.community = @community
    

    Docs

    0 讨论(0)
提交回复
热议问题