Dennis Nedry
Dennis Nedry

Reputation: 4748

How to build a nested params hash with a custom route in Ruby-on-Rails?

I've got a route that looks like this:

get ':q', to: 'foo#bar'

and I can access it inside my foo_controller through:

params[:q].

Is it somehow possible to nest the params-hash so that it can access it through:

params[:namespace][:q]

Upvotes: 0

Views: 133

Answers (1)

Ravi Prakash
Ravi Prakash

Reputation: 121

yes, its possible and very easy to do so. 1. Declare the routes properly for the nested resources. refer to the link1 below.

  1. allow nested attributes in the strong parameters. like this:

    params.require(:abc).permit(:name, :email, custome_attribute: { :id, :_delete, :name})

Remember :id and :_delete is neceesary if you want to delete the nested resources. Now construct your form using rails form builder. follow the 2nd link below.

Please go through the following tutorial:

http://guides.rubyonrails.org/routing.html#nested-resources

http://guides.rubyonrails.org/form_helpers.html#nested-forms

Upvotes: 1

Related Questions