0

I am trying to follow a ruby on rails scaffold example.

I am using ruby version: 2.1.5 with rails version : 4.1.8

I have used this command:

rails generate scaffold User name:string email:string

This worked fine - I can run the example which shows crud functionality to my generated scaffold. The examples I have been following advise to look at the model file generated. The ones in the examples seemed to have a value called attr_accessible - but for me this was not generated - I do not know why - it seems to be quite useful so you can easily see what is inside your model? My model looks like this:

class User < ActiveRecord::Base

end

I altered it to look like this :

class User < ActiveRecord::Base
    attr_accessible :name, :email
    validates :name, :presence=>true
end

and when I now visit localhost/users I get the error:

Error

Can somebody please tell me how a generated model can be created with this attr_accessible line, and how I can add an example of validation to the model.

RenegadeAndy
  • 5,440
  • 18
  • 70
  • 130

3 Answers3

4

Rails 4 doesn't use attr_accessible; it uses Strong Parameters.

They both serve as a guard for mass assignment of params, often times used during form submissions.

The scaffold doesn't support automatically setting strong params as far as I'm aware, in part because people implement strong params white listing very differently.

A couple more links on Strong Params

To me this suggests whatever guide you're following is out of date.

Community
  • 1
  • 1
deefour
  • 34,974
  • 7
  • 97
  • 90
  • Can you give me a scaffolding on rails 4 guide which is in date!? – RenegadeAndy Dec 02 '14 at 15:32
  • Sorry, I'd just be using Google like you can. I don't use scaffolding, and personally think it's not the best way to learn Rails. I suggest working through the http://edgeguides.rubyonrails.org/. Scaffolding is a little too magical for a beginner, IMO. – deefour Dec 02 '14 at 16:16
1

I think the problem is that the scaffolding that you have used is not compatible with how Rails works in later versions. In earlier versions of Rails, attr_accessible was used for preventing security problems related to mass assignment. In later versions, the countermeasure changed and moved to the controller instead of the model.

If you still want to use the attr_accessible macro, you can add gem 'protected_attributes' to your Gemfile and install with bundler.

Jakob W
  • 3,347
  • 19
  • 27
-2

You shouldn't add them in the model. The stuff you wanna access goes into the controller, like

def index
  @users = User.all
end

def show
  @user = User.find(params[id])
end

...

private

def user_params
  # It's mandatory to specify the nested attributes that should be whitelisted.
  # If you use `permit` with just the key that points to the nested attributes hash,
  # it will return an empty hash.
  params.require(:user).permit(:name, :mail)
end

so you can use them afterwards in your views.

e.g. in app/views/users/show...

<h1>@user.name</h1>
<p>@user.email</p>
antani
  • 5
  • 5