1

Is it possible to change the value in the Params hash when a Javascript function is called? I have a hidden Div, say DIV1 that becomes visible based on the selected value in a select field, within DIV1, I have a readonly textfield whose value is set to a value returned by a helper method.

This helper method uses a dynamic find_by that depends on the value of Params,but I guess the param Hash doesn't change when the value of the select Changes (since it isn't a full page refresh?). Please, how do I Achieve updating this so that when the select Value changes, the new value is reflected in the params hash. I have :remote=>true in the form_for tag. Is there a better approach than mine?

The Select field in a rails view

#finance_year
<%=f.select :financeyear, options_for_select(finance_year),{ :include_blank => 'Select a
Financial Year' } %>

and a an onchange event for that select

jQuery ->
$('#finance').hide()
value = "Select a Financial Year"
$('#finance_financeyear').change ->
selected = $('#finance_financeyear :selected').text()
$('#finance').show()
$('#finance').hide() if selected is value

the helper Method

def amount_owed(student)
financeyear = params[:financeyear]
@thisstudent = Finance.find_last_by_user_id(@student.user_id,
:conditions => {:financeyear => financeyear } )
if(@thisstudent)
  @amount_owed= @thisstudent.amount_owed
else
  student.department.amount
end
end

I appreciate any help and I hope I've been able to ask the question intelligently.

2 Answers 2

3

The answer is AJAX.

First, we'll need to add a new route to config/routes.rb to make amount_owed() a true action:

get '/finance_years/amount_owed/:student_id/:financeyear' => "finance_years#amount_owed"

Next, we'll create a default view to be returned whenever the amount_owed() action is called:

/app/views/finance_years/amount_owed.html.erb

<%= @amount_owed %>

So, that part was easy. Now we need to edit the amount_owed action so it will work with our parameters:

/app/controllers/finance_years_controller.rb

def amount_owed(student)
    financeyear = params[:financeyear]
    @thisstudent = Finance.find_last_by_user_id(params[:student_id],
      :conditions => {:financeyear => financeyear } )

    if(@thisstudent)
       @amount_owed= @thisstudent.amount_owed
    else
       @amount_owed = student.department.amount
    end
end

This way, we can pass in the finance year and the student id from the params hash and get an amount_owed every time. Now, to give our coffeeScript access to the current student_id and finance_year variables, I'd add a couple hidden fields to the form in the view file:

/app/views/finance_years/_form.html.erb

<%= hidden_field_tag :student_id, @student_id %>
<%= hidden_field_tag :finance_year, @finance_year %>

The last trick is to edit the coffeeScript, firing an asynchronous GET request whenever the select box changes.

/app/assets/javascripts/finance_years.js.coffee

$('#finance_financeyear').change ->
   student_id = $("#student_id").val()
   finance_year = $("#finance_year").val()
   selected = $('#finance_financeyear :selected').text()
   $('#finance').show() unless selected == value
   $.get "/finance_years/amount_owed/#{student_id}/#{finance_year}", (response)->
      $('#finance input[type=text]').load(response)

And that's about all I can do being away from my rails development machine. Let me know if additional problems arise.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad I participated in your solution.
1

I'm not a rails expert, but you're not going to be able to modify server side code directly from javascript. You will need to make a call down to the server (either on a form submit or through an ajax request) to tell the server to update itself.

To clarify, the server code is responsible for the initial rendering of the page, but once the template has been rendered it doesn't exist on the client page. So you can't directly modify it from coffeescript/javascript. You need to send a request back to the server to handle that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.