1

I'm having a problem accessing the :prepend_id variable in my shared/_time_select.html.erb. The variable seems to be empty. The idea is to be able to call the partial and pass in a string (to prepend the id's) so there aren't conflicts with the post variables when there are 2 or more time input fields on the page.

I am calling the following line from another _form.html.erb partial.

<%= render 'shared/time_select', :locals => { :prepend_id => "starttime" } %>

Here is a piece of my shared/_time_select.html.erb

<div id="<%= @prepend_id.to_s %>_select_container time_select_container">
    <select name="<%= @prepend_id %>_hour">
        ...
    </select> : 
    <select name="<%= @prepend_id %>_minute">
        ...
    </select> 
    <select name="<%= @prepend_id %>_ampm">
        ... </select>
</div>

That will make posting them to the controller a lot easier. Then in the controller I could easily append the times to the datetime that is being submitted in the same form. Also, a side question is I'm wondering if this would be a good time to use a helper or not?

botbot
  • 7,299
  • 14
  • 58
  • 96

1 Answers1

4

First, drop the @ inside the partial. Just use prepend_id instead of @prepend_id

Also, you need to either use :partial AND :locals keys, or neither:

render :partial => 'shared/time_select', :locals => { :prepend_id => "starttime" }
#OR
render 'shared/time_select', :prepend_id => "starttime"

Looks like the same problem here: passing values to partial in rails 3

Community
  • 1
  • 1
tybro0103
  • 48,327
  • 33
  • 144
  • 170
  • thanks for the suggestion. i thought that was the issue as well but i get: undefined local variable or method `prepend_id' – botbot Mar 11 '12 at 04:24
  • perhaps something else is going wrong as well, but I assure you @prepend_id inside the partial is not the variable passed in via :locals – tybro0103 Mar 11 '12 at 04:26
  • 2
    You could also use the more concise version: `render 'shared/time_select', prepend_id: 'starttime'` – James Mar 11 '12 at 04:30
  • ok, that makes sense, rails must not be complaining when i use @prepend_id because it's just empty. i guess then i need to figure out why prepend_id (without the '@') would come up as undefined. partial is being called correctly, and it seems like i'm passing in the string correctly. i've called many other partials like this, just not sure why now it won't work. – botbot Mar 11 '12 at 04:31
  • same problem with the concise version. – botbot Mar 11 '12 at 04:32
  • explicitly telling it its a partial yields the same result... i heard it's not necessary in rails 3 but not sure about that. – botbot Mar 11 '12 at 04:35
  • any undefined variable prepended with @ in a view will not throw an error when accessed, but rather evaluate to nil – tybro0103 Mar 11 '12 at 04:35
  • "explicitly telling it its a partial yields the same result" ...you need to do both that and drop the @ – tybro0103 Mar 11 '12 at 04:37
  • yeah, i understand that, i've called it like this '<%= render :partial => 'shared/time_select', :locals => { :prepend_id => "blah" } %>' and dropped all the '@' symbols. – botbot Mar 11 '12 at 04:40
  • could it be that i'm calling the partial from another partial, or can i can as many levels of a partial as i want? – botbot Mar 11 '12 at 04:40
  • Nested partials isn't a problem. Perhaps you edit the value of prepend_id inside the partial before you output it? Can you post the whole partial code? – tybro0103 Mar 11 '12 at 04:42
  • this is the only place where, i am mentioning :prepend_id, it is not being modified anywhere else. i'm realizing that my other partials have the same problem. i think i'm missing something fundamental here. i will post the partial to my initial post. – botbot Mar 11 '12 at 05:35
  • 1
    In your _form partial: <% end> should be <% end %> – tybro0103 Mar 11 '12 at 05:54
  • I think a combination of adding :partial, and restarting the rails server fixed it. – botbot Mar 11 '12 at 07:20