undefined local variable or method `f' for #<#:0x000001080edfe0>
I am trying to render a form within a template page with:
<%= form_for #vehicle, html: { class: "f_grp" }, remote: true do |f| %>
<%= render "vehicles", locals: { f: f } %>
<% end %>
The file is loading. But I'm getting an undefined method on f error. Any ideas?
You only use :locals when you use :partial.
Either of these are correct:
<%= render partial: 'vehicles', locals: { f: f } %>
Or (as of Rails 2.3):
<%= render 'vehicles', f: f %>
Your version, which combines both, creates a local called locals and sets its value to a hash containing f: FormBuilder....
It's worth noting that the only reason to still use locals: is when you use render :collection.
Related
<%= render layout: "shared/some_template",
locals: {
variable_value: true
} do %>
<%= hidden_field_tag "ids[]", "ng-value": "sth.id" %>
<% end %>
The template on some template has some html and a yield call where the "hidden" field should go but it's not rendering it, it does render everything else inside the template, it just drops the hidden_field_tag part.
Any idea how can I solve this? it works on the HAML version when I tried it, but not on ERB, is that not available for ERB?
You are passing a &block to render, which is doing nothing with it.
To pass a callback to a partial, build a lambda and put it in a local variable:
<%=
lamb = lambda{ hidden_field_tag 'ids[]', 'ng-value' => 'sth.id' }
locals = { variable_value: true, callback: lamb }
render layout: 'shared/some_template', locals: locals
%>
Now inside the template call <%= callback.call %>.
(Note, BTW, that I used ' instead of ", because we are not using the special features of ". And note I introduced a local variable, locals, instead of creatively indenting the render call.)
How can I pass a variable to a partial using this code? :
<%= render #performance_indicator.improvement_actions.order("created_at DESC") %>
I want to pass "id=1" and then in _improvement_action, use that variable like:
<%= id %>
EDIT:
This is my improvement_action partial:
https://gist.github.com/luisamaro0/6597084f2de1dc33cde7c014ea9f23a5
You can pass a local variable like so:
render "a_partial", :a_local_variable => whatever, :another_variable => another
See this question for more details: Rails 3, passing local variable to partial
you can pass a variable like this
<%= render partial: 'partial_name', locals: {id: '1'} %>
Try this syntax out:
<%= render #performance_indicator.improvement_actions, locals: { local_variable: 1 %>
# then <%= local_variable %>
I have a simple_form for a Model. The form for the new action si basic, like this:
<%= simple_form_for #patient do |f| %>
.... fields
<% end %>
I want to use this form in the New view, but also in a bootstrap modal with AJAX. If I use this syntax:
<%= render partial: 'patients/form', locals: {patient: #patient, remote: true} %>
But when I do this I only can access the local variable with remote, not #remote, which will render an error in the new view.
EDIT
The call for the partial:
<%= render partial: 'patients/form', locals: {patient: #patient, remote: true } %>
And the form:
<%= simple_form_for #patient, remote: #remote do |f| %>
.... fields
<% end %>
Any way to accomplish this?
Thanks!
You can send the request to the modal as Ajax and in the form use remote: request.xhr? So that you can use the form for both Ajax and Non-Ajax calls.
Add new patient
And in the form
#form
<%= simple_form_for #patient, remote: request.xhr? do |f| %>
.... fields
<% end %>
request.xhr? returns true if the request is an Ajax request else it returns false. So it will be remote: true for Ajax call and remote: false for non-Ajax call.
Update:
Ok. Somehow the above approach didn't worked for you. Try the below approach
Define a local variable while calling a partial
<%= render partial: 'patients/form', locals: {patient: #patient, modal: true} %>
And in the form
<% modal ||= false %>
<% remote = modal ? true : false %>
<%= simple_form_for #patient, remote: remote do |f| %>
.... fields
<% end %>
I have this form (simplified)
<%= form_for #client do |client| %>
<%= render "some_partial", foo: 10, bar: 20 %>
<% end %>
I also try with: <%= render :partial => "some_partial", :locals => {foo: 10, bar: 20} %>
And in some_partial:
<%= foo %>
<%= bar %>
But, the variables are not available on partial:
undefined local variable or method `foo' for #<#<Class:0x007fb35027bbd0>:0x007fb350536e60>
If I put render out side of form_tag works fine
Thanks in advance
UPDATE1: At the end of form, I have this:
<%= link_to_add_association 'Add', client, :client_addresses, class: "ui mini green button add_client_address" %>
If I remove this part, works fine...
You have to mention parameters as locals, like this.
<%= render :partial => "some_partial", :locals => {foo: 10, bar: 20} %>
Is the "some_partial" the same :client_addresses you are trying to load with the link_to_add_association?
I believe that the error message you are experiencing is coming from within the link_to_add_association
Seems you are using the Cocoon gem. In order to pass locals into the partial that Cocoon uses (:client_addresses in your case) you need to pass the parameter render_options: { locals: {foo: bar} for the link_to_add_association
I would like to pass the form_for object to a partial:
<%= form_for #price do |f| %>
...
<%= render :partial => "price_page", :object => #price, :as => :f %>
...
<% end %>
When I call:
f.radio_button
Brings the error:
undefined method `radio_button' for #<Price:0x3cb1ed0>
How can I use f as I usually would in this partial?
Try passing form object as local
<%= render :partial => "price_page", :locals=>{:f=>f} %>
You can pass form builder object as a local variable like below,
<%= form_for #price do |f| %>
<%= render :partial => "price_page", :locals => { :f => f } %>
<% end %>
in your partial file you will be receiving form builder as a local variable "f", you can use like below,
<% f.radio_button, {} %>
I ran across this question trying to figure out how to get a form builder into a partial without an additional form tag. That's the primary use case I could think of for this question, so I'm adding this answer for future visitors.
To solve my problem, I have my form_for in my layout and I render my partial passing only the model. In my partial I use fields_for.
Looks (something) like this:
= form_for #price do |f|
...
= render partial: "price_page", object: #price, as: 'price %>
...
Then, my partial has this:
= fields_for price do |f|
...