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

  • Home
  • SEARCH
  • 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 7560125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:49:02+00:00 2026-05-30T12:49:02+00:00

In my new Rails application (it’s my first one!) Users can manage their Clients

  • 0

In my new Rails application (it’s my first one!) Users can manage their Clients and also create Projects for each Client.

Users -----< Clients -----< Projects

In order to create a new Project for a Client, a user has to select the Client from a select box like this:

<%= f.label :client_id %>
<% options = current_user.clients.all.map { |client| [client.name, client.id] } %>
<%= f.select(:client_id, options) %>

My controller action looks like this:

client = current_user.clients.find(params[:project][:client_id])
@project = client.projects.build(params[:project])

This works quite well but I don’t feel particularly good about putting the client_id into my form directly. In my Project model it even says:

attr_accessible :name, :client_id

So is this a security flaw? I don’t want anyone to tamper with these foreign keys. Is there a more secure way to set foreign keys in Rails?

  • 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-30T12:49:04+00:00Added an answer on May 30, 2026 at 12:49 pm

    So Rails has a bunch of great form helpers for this sort of thing. Check this out:

    http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_from_collection_for_select

    This method will generate what you are looking for. To answer your question, no one can really tamper with these ids.

    If you are comfortable allowing a user to select a Client for the Project, then the worst thing that can really happen is that you have an extra Project with a Client who isn’t associated with that project.

    Here’s how I’d do what you were going for:

    <%= f.select :client_id, options_from_collection_for_select(current_user.clients.all, 'id', 'name') %>
    

    The methods takes the colleciton first (In your case its current_user.clients.all), then the attribute for the value of the option (in your case the id), then it takes the label attribute (in your case the name of the client)

    You are correct that you need attr_accesible :client_id but like I said, this really isn’t a security concern.

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

Sidebar

Related Questions

I am new in rails,and I create the first rails application (Blog) follow the
One of my clients wants his new Rails application to look more like his
I'm new to Rails and making application where college members (teachers and students) can
I want to create a new rails application and fire up the rails server
I'm just trying to figure out why when I create a new rails application,
I'm developing a new rails application which is supposed to be installed several times
I am new to rails and have developed a simple rails application on my
I'm starting a new Rails 3.2.6 application. My nested route is failing when I
I just created a new rails app in Rails 3.1.1, and my application layout
I'm currently setting up a new production environment for a Rails application which includes

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.