Ignore fields when submitting a form

Currently, I have a form that creates a car. Initially, the vehicle model is as follows:

class Vehicle < ActiveRecord::Base attr_accessible :trim_id belongs_to :trim end 

You will notice that there are no make_id or model_id ; they are not required, since the decoration refers to the model, and the model belongs to the brand, so they do not need to be stored in the car model.

The problem arises in the shape of a car - I have some related selections where I can select make, then model, then crop. When I create a new car, the following code works fine:

 <%= select("", "", Make.all.collect {|p| [ p.value, p.id ] }, {:prompt => 'Select Make'}, {} %> <%= f.select("", "", options_for_select([]), {:prompt => 'Select Model'}, {} %> <%= f.select(:trim_id, options_for_select([]), {:prompt => 'Select Trim'} %> 

As you noticed, the choice of model and model has no shape parameters and, therefore, is ignored. This works great and the car is saved correctly.

The difficulty arises when I want to edit this vehicle; because make and model just dummy selects the chain to trim, they are initially set to the default value, and not the make and model of the edited car. As a result, I updated my model to look like this:

 class Vehicle < ActiveRecord::Base attr_accessible :trim_id belongs_to :trim has_one :model, :through => :trim has_one :make, :through => :model end 

And updated the form to:

 <%= f.select(:make, Make.all.collect {|p| [ p.value, p.id ] }, {:prompt => 'Select Make'}, {} %> <%= f.select(:model, options_for_select([]), {:prompt => 'Select Model'}, {} %> <%= f.select(:trim_id, options_for_select([]), {:prompt => 'Select Trim'} %> 

But, as expected, this leads to Can't mass-assign protected attributes: make, model , since make and model are not attr_accessible (and make and model columns do not exist anyway).

My question is this: is there a way to ignore these fields when submitting a new vehicle form, but still they have the correct values ​​when editing the vehicle?

Thanks!

EDIT: Based on answers from Burlington, I updated the create action as follows:

 def create @sale = Sale.new current_ability.attributes_for(:new, Sale).each do |key, value| @sale.send("#{key}=", value) end @sale.update_attributes(params[:sale].except("date")) authorize! :create, @sale if @sale.save redirect_to @sale, :notice => "Successfully created sale." else render :action => 'new' end end 

You will notice that I am trying to exclude the "date" here (also tried: date) - this should have been tested using a non-nested attribute.

I find, however, that the date is still being sent - can anyone help with the full controller action using Hash # except?

EDIT 2: I answered Burlington's question correctly, as he answered the original question. I asked the following question: Excluding nested form fields in controller action using Hash #exclude before generating mass_assignment error?

+6
source share
1 answer

You can remove keys that are not needed using Hash # except :

 @vehicle.update_attributes(params[:vehicle].except(:make, :model)) 
+12
source

Source: https://habr.com/ru/post/924271/


All Articles