rjsruby-1.8.7

dynamically set value of textfield rjs rails


I am trying to set the value of the hidden text field through rjs. I have a form.

<% form_tag :action => "upload" do %>
    <%= file_field_tag :zipfile %>
    <%= submit_tag "Upload" %>
    <%= hidden_field_tag "progress" %>
<% end %>

I want to set the value of the hidden text field according to the id of the progress that will be created when the browse button is clicked.

<script>
    $(document).ready(function() {
        $("input#zipfile").change(function() {
        $.ajax({
            url: "/progress_create",
            type: "GET"
            })
        });

    })
</script>

progress_create is as follows:

def progress_create
    @progress = Progress.create(:value => 0)
    respond_to do |format|
        format.js {}
    end
end

I think I am doing something in progress_create.js.rjs file which is as follows:

page<< %{
        $('#progress').val("<%= @progress.id %>");
        }

I want to set the value of the hidden field as id of the @progress. But, when I do the above, the value of the hidden field is "<%= @progress.id %>" string, not the exact id. Can anyone please correct me!


Solution

  • page << "$('#progress').val(#{@progress.id});"
    

    This should work. cheers