Chris Wissmach
Chris Wissmach

Reputation: 505

Getting params from URL in model's create method

Is there a way to get the params from URL in the create method?

My URL for the "new" view is:

/model_name/new?other_model_id=100

I would like to be able to alter the model with ID 100 when I create a new model. I've tried calling params[:other_model_id] in my "create" method, which returned nil and I tried setting the following variable in my "new" method:

@other_model = Model.find(params[:other_model_id])

I have a field called "other_versions" in my model, which is an array of model IDs. When I create a new model I want to add the new model's ID to the array of model IDs in the old model.

Upvotes: 2

Views: 527

Answers (1)

josetapadas
josetapadas

Reputation: 2637

Why don't you use the after_createfilter (http://guides.rubyonrails.org/active_record_callbacks.html) and just add the "other_model" id on an hidden field on your create/edit form?

Please make sure, if you are using Rails >4, to add that parameter on your strong parameters (http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html) otherwise it will always be empty when you check on params.

Upvotes: 3

Related Questions