开发者

Rendering the result of a controller method to a partial in the view

开发者 https://www.devze.com 2023-02-17 05:12 出处:网络
I have this Rails code in a method in a controller. It takes four parameters, does a bit of math and I\'d like it to return a the value stored in the \'attendance\' variable to a partial in a view.

I have this Rails code in a method in a controller. It takes four parameters, does a bit of math and I'd like it to return a the value stored in the 'attendance' variable to a partial in a view.

def find_attendance(lesson_id, student_id, start_date, end_date)
    marks = Array.new

    all_registers = Register.where(:lesson_id => lesson_id)

    start_date.upto(end_date) do |date|
        all_registers.each do |register|
            if date == register.date
                this_mark = RegisterMark.where(:student_id => student_id, :register_id => register.id)
                this_mark.each do |mark|
                    marks << mark.mark
                end
            end
        end
    end

    attendance = ((Float(marks.count("O")) + Float(marks.count("N"))) / Float(marks.count)) * Float(100)
end

I have created a _find_attendance.ht开发者_如何学运维ml.erb file in the relevant view directory, and tried calling the partial by using <%= render :partial => "find_attendance" %> but I'm having trouble passing parameters and using the correct variable name within the partial.

I've tried playing around with using instance variables on 'attendance'.

Any pointers?


The first thing you need to do is put the values that you want to pass to your views (regular views and partial views) into a controller variable, like this:

@attendance = ((Float(marks.count("O")) + Float(marks.count("N"))) / Float(marks.count)) * Float(100)

Next, you'll need to rename your partial something other than find_attendance. That's the name of the view for the find_attendance action, and if I understand your question, you want that view (find_attendance.html.erb) to call a partial with the attendance. So, try something like this:

find_attendance.html.erb:
<%= render :partial => "attendance", :object => @attendance %>

Now, inside the partial _attendance.html.erb, you can refer to an instance variable attendance. (Partials assume the existence of an instance variable with the same name as the partial; that's the :object we passed through render.

_attendance.html.erb:
<p>Attenance: <%= attendance %></p>


You almost certainly want to add this method as a method to the model. Maybe in your case the lesson model?

class Lesson < ActiveRecord::Base

  has_many :registers

  def calculate_attendance_for_student(student, start_date, end_date)
    valid_dates = start_date.upto(end_date)   
    registers.each do |register|
    ...
  end
end

Then in your view you can get at the @lesson model to get attendance.

There's some area for improvement in your loops, I included a simple way to loop through all Register for a Lesson above. You can do this for both your Register.where and your RegisterMark.where for speed improvements

0

精彩评论

暂无评论...
验证码 换一张
取 消