awesome_nested_set adding a new category

25 views
Skip to first unread message

fugee ohu

unread,
Nov 25, 2015, 6:29:45 PM11/25/15
to Ruby on Rails: Talk
in this example what if i didn't have a values for science, physics and gravity but had to get it from form what field would i use? my guess any of the 3 variables can be substituted with params[:id] ?

Create a root node:

science = Category.create!(:name => 'Science')

Put a new thing inside this root node:

physics = Category.create!(:name => 'Physics')
physics.move_to_child_of(science)

Put another thing inside the "physics" node:

gravity = Category.create!(:name => 'Gravity')
gravity.move_to_child_of(physics)

Colin Law

unread,
Nov 26, 2015, 3:59:17 AM11/26/15
to Ruby on Rails: Talk
Again, unfortunately, I have little idea of exactly what you are
asking. If the form is generated by
form_for @category
then 'name' would be one of the fields expected to be in the form, so
you should be able to do
Category.create!(params[:category])
though it would be more usual to use new and save and then check the
return value of save in case there were validation errors.

Colin

fugee ohu

unread,
Nov 27, 2015, 1:00:39 AM11/27/15
to Ruby on Rails: Talk
Why not Category.create!(category_params) instead of Category.create!(params[:category]) ? thanks

fugee ohu

unread,
Nov 27, 2015, 1:04:14 AM11/27/15
to Ruby on Rails: Talk
isn't params[:category] a reference to a single field and not all the params from the form


Message has been deleted

Colin Law

unread,
Nov 27, 2015, 3:49:57 AM11/27/15
to Ruby on Rails: Talk
On 27 November 2015 at 06:04, fugee ohu <fuge...@gmail.com> wrote:
> isn't params[:category] a reference to a single field and not all the params
> from the form

params[:category] is identical to category_params. As an old hand I
don't necessarily make use of later useful syntactical goodies in
rails. Look at the params structure in the controller and you will
see how the hash is organised. In fact I am not sure whether I like
using category_params as it is not obvious exactly what is being
referred to, whereas params[:category] can only refer to the category
hash within the params hash.

Colin
Reply all
Reply to author
Forward
0 new messages