locals with partial

51 views
Skip to first unread message

Pierre Caserta

unread,
Nov 23, 2014, 8:38:39 AM11/23/14
to rubyonra...@googlegroups.com
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?

--
Posted via http://www.ruby-forum.com/.

Greg Akins

unread,
Nov 23, 2014, 11:56:46 AM11/23/14
to rubyonra...@googlegroups.com
On Sun, Nov 23, 2014 at 8:37 AM, Pierre Caserta <li...@ruby-forum.com> wrote:
> 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.

Pierre Caserta

unread,
Nov 23, 2014, 1:32:15 PM11/23/14
to rubyonra...@googlegroups.com
$("#club_id").val() in my javascript gives me {:name=>:club_id,
:value=>11500}

how can I get the value 11500?

Pi Caz

unread,
Nov 24, 2014, 3:15:45 AM11/24/14
to rubyonra...@googlegroups.com
I still don"t have a solution for this problem.

Walter Lee Davis

unread,
Nov 24, 2014, 9:45:28 AM11/24/14
to rubyonra...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/cb436bb7eb617a209131b49439ae42e2%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Matt Jones

unread,
Nov 24, 2014, 10:19:59 AM11/24/14
to rubyonra...@googlegroups.com


On Sunday, 23 November 2014 08:38:39 UTC-5, Ruby-Forum.com User wrote:
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. http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag

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

ferna

unread,
Nov 25, 2014, 5:05:17 AM11/25/14
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages