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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:44:45+00:00 2026-06-10T03:44:45+00:00

When I pass the JSON Parameters: {friend=>{username=>example}, auth_token=>n2A1Sk85MZSupXEiE1nG} , I get the following error:

  • 0

When I pass the JSON Parameters: {"friend"=>{"username"=>"example"}, "auth_token"=>"n2A1Sk85MZSupXEiE1nG"}, I get the following error:

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: username)

FriendsController
def create
    @user = User.find(current_user)
    @friend = Friend.new(params[:friend])
    @friend.user_id = current_user.id
    @friend.friend_id = User.find(user.username)
    respond_to do |format|
        if @friend.save
            #flash[:notice] = 'Game was successfully created.'
            #format.html { redirect_to(@game) }
            format.json  { render :json => @friend }
        else
            #format.html { render :action => "new" }
            format.json  { render :json => @friend.errors, :status => :unprocessable_entity }
        end
    end
end

Friend Model
class Friend < ActiveRecord::Base
   attr_accessible :friend_id, :user_id
     belongs_to :user
     validates_presence_of :friend_id
     validates_presence_of :user_id
end

User Model
  class User < ActiveRecord::Base
 # Include default devise modules. Others available are:
 # :token_authenticatable, :confirmable,
 # :lockable, :timeoutable and :omniauthable
 devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable

 before_save :ensure_authentication_token

 # Setup accessible (or protected) attributes for your model
 attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :friends_attributes, :friends
 # attr_accessible :title, :body

 has_many :games#, :dependent => :destroy
 has_many :friends, :dependent => :destroy

 validates_presence_of :username
 validates_uniqueness_of :username
end

How can I accept a username from JSON and then in turn find the corresponding user_id? I am trying to allow a user to friend another by searching for the username.

  • 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-10T03:44:47+00:00Added an answer on June 10, 2026 at 3:44 am

    You need to add username to the list of attributes that are accessible on the Friend model. So change:

    attr_accessible :friend_id, :user_id
    

    to

    attr_accessible :friend_id, :user_id, :username
    

    I believe that should do it.

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

Sidebar

Related Questions

I have following code and i want to pass additional parameter in JSON.getJSONfromURL() method
How can I pass Json Rpc data to a specified callback function, just like
I am following the accepted answer of ASP.NET MVC How to pass JSON object
How can I pass my service endpoint parameters? (pagesize in this case) My .asmx
I have following json data which I want to pass it to server using
What is better way to pass some parameters(mbe post, get, cookie?) from one page
How can I pass parameters to the XMLHttpRequest Object? function setGUID(aGUID) { var xhReq
When inside a function can I get it's parameters name/value? I'm looking to turn
How do I use JSON in java servlet? Using the URL, to pass parameters
What is the right code to pass a json with nested parameters in this

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.