locals with partial

My partial code: view/clubs_registration/clubs/_dropzone_pictures.html.erb

<%= hidden_field_tag :club_id, name: :club_id, value: club_id %>

My initial view code: view/clubs_registration/clubs/test_dropzone_pictures.html.erb

<%= render partial: 'dropzone_pictures', :locals => { :club_id => @club.id } %>

What I really want to do is retrieve the value of club_id from my jquery:

console.log($("#club_id").val()); //-> {:name=>:club_id, :value=>11500}

I just want the value 11500. How should I do that please?

My partial code: view/clubs_registration/clubs/_dropzone_pictures.html.erb

<%= hidden_field_tag :club_id, name: :club_id, value: club_id %>

snip...

What I really want to do is retrieve the value of club_id from my jquery:

console.log($("#club_id").val()); //-> {:name=>:club_id, :value=>11500}

I just want the value 11500. How should I do that please?

What is the id on the hidden field? If it's "club_id" then your javascript should work.

$("#club_id").val() in my javascript gives me {:name=>:club_id, :value=>11500}

how can I get the value 11500?

I still don"t have a solution for this problem.

This appears to be a JavaScript issue, or maybe an HTML issue. In a browser, looking at the page, Control-click on the element you are trying to read (you may need to change it to a text input if it is a hidden element at the moment) and choose Inspect Element from the contextual menu. See what the ID of that field really is.

The jQuery code you posted should work. Try it old-school with console.log(document.getElementById('club_id').value) and see what that gives you. If the ID is not exactly club_id, or if there is more than one such element in the page, then you may get an instructive error.

Walter

My partial code:

view/clubs_registration/clubs/_dropzone_pictures.html.erb

<%= hidden_field_tag :club_id, name: :club_id, value: club_id %>

hidden_field_tag takes three arguments - name, value, and options. ActionView::Helpers::FormTagHelper

In your case, it would look like:

<%= hidden_field_tag :club_id, club_id %>

The third argument is omitted, since you’re only looking to set name, id and value on the resulting tag.

–Matt Jones

Please notice that the <%= … %> expression for the partial is in an erb file and is evaluated on the server before it is sent to the user’s browser. It is therefore not possible to have javascript variables be part of the expression. You need to rethink the partial and instead of using ruby variables it will have to use javascript variables.