0
Expensive!
I’m having a hard time because I’m new in Rails.
I am trying to generate a form with two models through Nested Form Rails. It is a simple model.
"Motor" has one or more "parts", and "part" has only one "engine". In this way I made the models:
class Motor < ActiveRecord::Base
has_many :pecas
accepts_nested_attributes_for :pecas
end
class Peca < ActiveRecord::Base
belongs_to :motor
end
The "Motors" controller is :
def new
@motor = Motor.new
end
def create
@motor = Motor.new(motor_params)
@motor.pecas.build(params[:id])
respond_to do |format|
if @motor.save
format.html { redirect_to @motor, notice: 'Motor was successfully created.' }
format.json { render action: 'show', status: :created, location: @motor }
else
format.html { render action: 'new' }
format.json { render json: @motor.errors, status: :unprocessable_entity }
end
end
end
private
def set_motor
@motor = Motor.find(params[:id])
end
def motor_params
params.require(:motor).permit(:nome, {:peca_attributes => [:item]})
end
Everything works fine (ALMOST). "Engine" has an attribute that is name:string and Peca has an attribute called item:string.
I am able to generate a record of pieces when I write an "engine" through the modified "engine" view but I cannot record the "item" attribute name of the "PECA" model".
It seems to me that it is a new STRONG PARAMETERS guideline in Rails 4. Can anyone help me?
Grateful!
Could you mark your answer as accepted?
– utluiz