Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7815365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:30:03+00:00 2026-06-02T05:30:03+00:00

How do you pass data from a controller to a model? In my application_controller

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:30:04+00:00Added an answer on June 2, 2026 at 5:30 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my php script for fetching data from a db. <?php mysql_connect(host,user,pass); mysql_select_db(db);
I am using Codeigniter to pass an array of data from a controller into
When I try and pass data to my view from a controller using ViewData
I have a model that I'm using to pass data from my view to
Many tutorials say that when i have to pass data from controller to view
The question is simple: How can i pass data from model to view(or back
I know how to pass data from a worker thread to the main thread
I want to pass data from database to JSF page as a table. I
I have a server script that I need to pass data to from the
i have two flex applications and i want to pass the data from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.