problem with mass asignment

17 views
Skip to first unread message

Andrew Dig

unread,
Jan 26, 2016, 3:21:21 PM1/26/16
to rubyonra...@googlegroups.com
Hi everyone. I'm new in ruby and recently faced with mass assignment
problem. Below I attached a sample code similar to mine. Could you
explain me what should I use in require method??? is it a name of
model(person) or a variable name which I assign result(person =
current_account.people.find(params[:id])) or maybe it should be an
object attribute like @person??? Because I used various types but always
have the same error.
The second question - is it necessary to give the same name for private
method def person_params as the name of (person_params) for db update?
thx

class PeopleController < ActionController::Base

def update
person = current_account.people.find(params[:id])
person.update_attributes!(person_params)
redirect_to person
end

private

def person_params
params.require(:person).permit(:name, :age)
end
end

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jan 26, 2016, 5:01:28 PM1/26/16
to Ruby on Rails: Talk
On 26 January 2016 at 20:20, Andrew Dig <li...@ruby-forum.com> wrote:
> Hi everyone. I'm new in ruby and recently faced with mass assignment
> problem. Below I attached a sample code similar to mine. Could you
> explain me what should I use in require method??? is it a name of
> model(person)

Normally it is the name of the model, as in the example shown.
If you can't get that to work show us some code (just the relevant
bits, should not need more than a dozen lines or so) and the error you
are getting.

Colin

Andrew Dig

unread,
Jan 27, 2016, 12:48:57 AM1/27/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1180931:
def contact
if params.present?
item_params = params[:data]
item = Page.create(item_params)
end
render 'index/contact'
end

private

def item_params
params.require(:item).permit(:name,:description)
end

Attachments:
http://www.ruby-forum.com/attachment/11243/1453873666654.jpg

Frederick Cheung

unread,
Jan 27, 2016, 2:10:11 AM1/27/16
to Ruby on Rails: Talk


On Tuesday, January 26, 2016 at 8:21:21 PM UTC, Ruby-Forum.com User wrote:
Hi everyone. I'm new in ruby and recently faced with mass assignment
problem. Below I attached a sample code similar to mine. Could you
explain me what should I use in require method??? is it a name of
model(person) or a variable name which I assign result(person =
current_account.people.find(params[:id])) or maybe it should be an
object attribute like @person??? Because I used various types but always
have the same error.

it is the name of a key in the params hash (check your development.log to see what params you are receiving). In your example below you are checking that the params hash contains a key "person" (an error will be raised if not) and that value is a hash where the keys name and age are permitted. If you use the standard rails methods for building your form (i.e. form_for and f.text_field etc rather than text_field_tag) then rails uses the name of the model class so the argument to require will also be the name of the class

 
The second question - is it necessary to give the same name for private
method def person_params as the name of (person_params) for db update? 

not quite sure what you mean by that. 

Fred 

Andrew Dig

unread,
Jan 27, 2016, 2:54:14 AM1/27/16
to rubyonra...@googlegroups.com
Thank you very much. Now it is clear
Reply all
Reply to author
Forward
0 new messages