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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:46:58+00:00 2026-06-12T16:46:58+00:00

I need to create a CRUD that allow create and update projects and one

  • 0

I need to create a CRUD that allow create and update projects and one of the attributes is the client that owns the project. When the project is created or edited, the client is selected using a select tag.

I have this model:

class Cliente < ActiveRecord::Base
  attr_accessible :nombre
  has_many :proyectos
end

class Proyecto < ActiveRecord::Base
  attr_accessible :nombre, :cliente_id
  belongs_to :cliente
end

this controller:

class ProyectosController < ApplicationController
  def new
    @proyecto = Proyecto.new
    @clientes = Cliente.order(:nombre)
  end

  def edit
    @proyecto = Proyecto.find(params[:id])
    @clientes = Cliente.order(:nombre)
  end

  def create
    @proyecto = Proyecto.new(params[:proyecto])
      if @proyecto.save
        redirect_to @proyecto, notice: 'Proyecto was successfully created.'
      else
        render action: "new"
      end
    end
  end

  def update
    @proyecto = Proyecto.find(params[:id])
    if @proyecto.update_attributes(params[:proyecto])
        redirect_to @proyecto, notice: 'Proyecto was successfully updated.'
      else
        render action: "edit"
    end
  end
end

and this form on the view (in haml):

= form_for @proyecto do |f|
  = f.label :cliente
  = f.collection_select :cliente_id, @clientes, :id, :nombre
  = f.label :nombre
  = f.text_field :nombre
  = f.submit 'Save'

The code was generated with scaffold, I just removed the unnecesary parts and added the code to create the select.

Initially, on the model Proyecto I had this:

attr_accessible :nombre

but get the error “Can’t mass-assign protected attributes: cliente_id”. Searching here on stackoverflow for similar problems, I found that I must add cliente_id to attr_accessible, but searching on google also found that I must not add foreing keys to attr_accessible due to security issues, which is contradictory.

Is this the correct way to code my create and update methods, adding cliente_id to attr_accessible? If not, what is the correct way?

I’m working with rails 3.2.8 and ruby 1.9.3p194

  • 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-12T16:46:59+00:00Added an answer on June 12, 2026 at 4:46 pm

    In this case you must have the client_id as attr_accessible. The reason is because you are allowing the user to set the client through the select box on the form.

    However, this poses a security concern. Imagine that you are only showing a specific user 3 different clients he can pick from (IDs: 1, 2, 3). If the user modifies the form manually, he can assign a client with ID #4 to his project which can be a security issue or just a bug.

    To close the security issue, add a validation to your Project model that makes sure that the client ID is valid.

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

Sidebar

Related Questions

I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I am looking for a messaging/eventing framework that will allow changes in one application
I need to create project in which there are two databases local and remote.
I need to create an application that does the following: The application is going
You often need View in View. For example, a client that has many phone
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass

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.