Rails calling create method from same controller instance variable

llermaly

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!!

Zh Kostev

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Calling a method from another method in the same class

From

Ruby: Calling class method from instance

From Dev

Calling an instance of an object into another controller rails

From Dev

What is the scope of an instance variable in a Rails controller?

From Dev

Calling the same controller method again does not work

From Dev

Ruby on Rails Controller Instance Variable not shared

From Dev

Method break after calling function in same controller

From Dev

undefined local variable or method from &block when calling instance method

From Dev

Rails - calling an instance variable from anywhere in the app

From Dev

Calling a variable from a void method

From Dev

Getting params value in rails create method from controller

From Dev

calling a directive method from controller

From Dev

calling an instance variable from another controller

From Dev

Ruby: trouble calling class instance from method

From Dev

Looping through a controller instance variable in a view in Rails

From Dev

Accessing $scope variable from another method in the same controller Angularjs

From Dev

Instance variable from Rails model: undefined method `any?'

From Dev

Calling a method from another controller in rails

From Dev

Rails - Calling a model method in controller

From Dev

Springroo calling method from Controller or create menu item

From Dev

Rails update instance variable on ajax call to a method from the same controller

From Dev

Calling an instance method from a different class

From Dev

Rails can a class variable be assigned within a controller's instance method?

From Dev

Choosing a controller for a layout class instance variable in Rails

From Dev

Create instance of model from method

From Dev

Minitest - calling a mocked instance method from within another instance method

From Dev

Calling an instance method from a class method - Ruby

From Dev

Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller

From Dev

Calling a method in ruby from a method with the same name

Related Related

  1. 1

    Calling a method from another method in the same class

  2. 2

    Ruby: Calling class method from instance

  3. 3

    Calling an instance of an object into another controller rails

  4. 4

    What is the scope of an instance variable in a Rails controller?

  5. 5

    Calling the same controller method again does not work

  6. 6

    Ruby on Rails Controller Instance Variable not shared

  7. 7

    Method break after calling function in same controller

  8. 8

    undefined local variable or method from &block when calling instance method

  9. 9

    Rails - calling an instance variable from anywhere in the app

  10. 10

    Calling a variable from a void method

  11. 11

    Getting params value in rails create method from controller

  12. 12

    calling a directive method from controller

  13. 13

    calling an instance variable from another controller

  14. 14

    Ruby: trouble calling class instance from method

  15. 15

    Looping through a controller instance variable in a view in Rails

  16. 16

    Accessing $scope variable from another method in the same controller Angularjs

  17. 17

    Instance variable from Rails model: undefined method `any?'

  18. 18

    Calling a method from another controller in rails

  19. 19

    Rails - Calling a model method in controller

  20. 20

    Springroo calling method from Controller or create menu item

  21. 21

    Rails update instance variable on ajax call to a method from the same controller

  22. 22

    Calling an instance method from a different class

  23. 23

    Rails can a class variable be assigned within a controller's instance method?

  24. 24

    Choosing a controller for a layout class instance variable in Rails

  25. 25

    Create instance of model from method

  26. 26

    Minitest - calling a mocked instance method from within another instance method

  27. 27

    Calling an instance method from a class method - Ruby

  28. 28

    Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller

  29. 29

    Calling a method in ruby from a method with the same name

HotTag

Archive