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 6827983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:19:47+00:00 2026-05-26T22:19:47+00:00

Say for example I pass the following two HTTP POST parameters to my Rails

  • 0

Say for example I pass the following two HTTP POST parameters to my Rails application:

fname=john&lname=doe

And within my application a controller will pass the params hash to the ‘Person’ model class defined as:

Class Person
  attr_accessor :fname, :lname

  def initialize(params)
    @fname = params[:fname]
    @lname = params[:lname]
  end
end

As you can see I initialise a Person object with the values contained in the params hash. Note the name of properties and parameters are identical.

Obviously, in a real-world example I would have many more properties in my object, and so to use the above initialisation method could become tedious.

Therefore, I was wondering if there is a way to dynamically populate properties of an object with HTTP parameters of the same name?

  • 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-05-26T22:19:48+00:00Added an answer on May 26, 2026 at 10:19 pm

    You can do what you’re asking for in both controllers and models. First, an example of how this would work with a controller:

    class PeopleController < ActionController::Base
      attr_accessor :fname, :lname, :phone, :email
    
      def some_action
        params.each_pair do |key, value|
          send(:"#{key}=", value) if respond_to?(:"#{key}=")
        end
      end
    end
    

    However, this is not a very practical application; there is very little reason to have accessor instance variables on a controller, much less enough that you would need to use this method.

    Instead, what you seem to want to do is to create an instance of the Person model and mass-assign attributes from params. Rails already does this, hence the default create action:

    class PeopleController < ActionController::Base
      def create
        @person = Person.new(params[:person])
        if @person.save
          redirect_to @person, :flash => "Person created successfully."
        else
          render :action => :new
        end
      end
    end
    

    Note that by default, all model attributes are mass assignable. This can be a security issue when it becomes possible for users to manipulate certain fields (like associations). In this case, you can either whitelist or blacklist attributes on your models to prevent this type of security breach:

    class Person < ActiveRecord::Base
      attr_accessible :fname, :lname, :phone, :email
      # or
      attr_protected :non_user_changeable_attribute
    end
    

    You can read more about mass assignment security here.


    If you want to add this behavior to a group of non-ActiveRecord classes (without the protector attributes), you can snag this module and just do an include MassAssignment in your class definition (doesn’t rely on Rails):

    module MassAssignment
      def initialize(attributes = {})
        mass_assign(attributes)
      end
    
      def mass_assign(attributes)
        attributes.each do |attribute, value|
          respond_to?(:"#{attribute}=") && send(:"#{attribute}=", value)
        end
      end
    end
    

    This avoids duplicating somewhat “ugly” code in every initializer.

    Via: https://github.com/coreyward/typekit/blob/master/lib/typekit/base.rb

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say for example, I have the following two tables: TableA { _id } TableB
I have two websites: http://example.com http://example.com/sub I have a page, let's say: http://example.com/sub/page1 Is
Say for example I have the following string: var testString = Hello, world; And
Lets say for example: $(.button).click(function() { $.post(commandrunner.php, { param1: 'value', param2: 'value2', param3: 'value3'
Say for example this is my C ( & Objective-C ) method as follows.
I am following this example by Microsoft to register an application with a web
Say for example I have the following code: <h3>My very long title</h3> <h3>Another long
Let's say for example that I have two functions with random code inside and
How would one pass a C# object via com? For example, say I have
Lets say I have the following jQuery plugin (this is just an example to

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.