I have a few hours with something that is probably very easy.
I have a nested model
resources :grades do
resources :students
end
So I defined
before_action :set_grade, except: [:mass_input]
to my students_controller
def set_grade
@grade = Grade.find(params[:grade_id])
end
I'm very good with this, the problem is that now I'm using another action that takes :grade_id from another source, so I cant use set_grade, instead I'm passing the id with javascript. Works.
My problem appears here, when I try to call to create method, I'm probably doing it wrong ..
def mass_input
@grade = Grade.find(@data['grade'])
@data = JSON.parse(params[:form_data])
#is this create way ok or I'm overriding???
Student.create(:rut => @data['mass_students'][1][0], :nombre => @data['mass_students'][1][1], :apellido => @data['mass_students'][1][2])
end
This is my create action
def create
@student = Student.new(student_params)
@grade.students << @student
respond_to do |format|
if @student.save
format.html { redirect_to school_grade_path(@grade.school,@grade), notice: 'Alumno creado con éxito.' }
format.json { render :show, status: :created, location: @student }
else
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
By this way code works but this line is not working
@grade.students << @student
@grade is not passing from mass_input to create. I think I'm not calling create properly but I cant find how to do it , because is not redirecting neither
My mass_input action is working by this way
def mass_input
@grade = Grade.find(@data['grade'])
@data = JSON.parse(params[:form_data])
Student.create(:rut => @data['mass_students'][1][0], :nombre => @data['mass_students'][1][1], :apellido => @data['mass_students'][1][2])
grade.students << student
respond_to do |format|
if student.save
format.html { redirect_to school_grade_path(grade.school,grade), notice: 'Alumno creado con éxito.' }
format.json { render :show, status: :created, location: student }
else
format.html { render :new }
format.json { render json: student.errors, status: :unprocessable_entity }
end
end
end
but I think is AWFUL, I must use my own create action
Thanks!!
Oh... From my point of view you are doing smth strange... The fast solution for your issue would be smth like this:
1) Rewrite before action in a new way:
before_action :set_grade
And method set_grade:
def set_grade
@grade = Grade.find(params[:grade_id].presence || @data['grade'])
end
2) Set method for student params
def student_params
data = JSON.parse(params[:form_data])['mass_students']
#Transform data to be student params. For ex:
data.map{|_key, info| {:rut => info[0], :nombre => info[1], :apellido => info[2]}}
end
3) Rewrite mass_input method
def mass_input
respond_to do |format|
if (@students = @grade.students.create(student_params).all?(&:persisted?)
#some actions when everything is great.
else
#some actions if not of them valid (maybe redirect & show info about not created students)
end
end
end
But you should definetly read more rails guides... http://guides.rubyonrails.org/
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments