Rails AJAX: My partial needs a FormBuilder instance - ruby-on-rails

So I've got a form in my Rails app which uses a custom FormBuilder to give me some custom field tags
<% form_for :staff_member, #staff_member, :builder => MyFormBuilder do |f| %>
[...]
<%= render :partial => "staff_members/forms/personal_details", :locals => {:f => f, :skill_groups => #skill_groups, :staff_member => #staff_member} %>
[...]
<% end %>
Now, this partial is in an area of the form which gets replaces by an AJAX callback. What I end up doing from the controller in response to the AJAX request is:
render :partial => "staff_members/forms/personal_details", :locals => {:skill_groups => #skill_groups, :staff_member => #staff_member}
However, if I do that then the form breaks, as the FormBuilder object I used in the form_for is no longer available. Is there any way for me to use my custom FormBuilder object inside a partial used for an AJAX callback?

Use fields_for inside your partial. It performs a similar task but without wrapping the form tags. See the API docs.

how about this?
#template.with_output_buffer do
#template.form_for #model_object do |f|
f.fields_for :some_nested_attributes do |ff|
render :partial => 'nested_attributes', :object => #model_object, :locals => {:form => ff}
end
end
end
this would be especially useful is you need to use the nested fields_for in the partial

You could instantiate a new instance of your form builder in the controller, though it feels sort of lousy to me:
# in the controller
render :partial => {
:f => MyFormBuilder.new(:staff_member, #staff_member, template),
:skill_groups => #skill_groups,
:staff_member => #staff_member
}
Alternatively, you could move more of the update logic to be client side which wouldn't require you to worry about rendering anything at all. You could just update the values via JS. Not sure if that works for your project though.

Maybe I'm a little late in the game here, and maybe I don't understand the question properly, but in ApplicationHelper.rb I think you can just add the line:
ActionView::Base.default_form_builder = MyFormBuilder

You can submit within your ajax call the content of f.object_name (it's also works with partials) and use it to render tags defined in http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html passing it as the first argument.

Related

layouts vs. shared vs. helpers folders (and when to use partials)

I'm trying to build out an app and I started getting confused when to use a partial as opposed to simply refactoring code. In other words, when should I use 'render' and 'render partial: ........"
And if I put something in the "shared" folder under "views" does that make it a partial? Not sure when to use these different folders. Thanks a whole bunch!
Normally you use 'render' for just move some html codes, like "footer".
If you want the partial with its own layout or pass variables in it. We will use 'render :partial'
<%= render "footer" %> # Basic usage
<%= render "shared/footer" %> # _footer.html will be placed in "shared/_footer.html"
<%= render :partial => "sidebar", :layout => "sidebar_layout" %>
# It will using "_sidebar_layout" as a layout template for "_sidebar.html"
<%= render :partial => "form", :locals => { :post => #post } %>
# Passing #post variable as post in form partial
Reference: http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

Javascript inside view in rails 3

I need to put some javascript inside a view. Basically I am having a play with the Recurly.js library.
Here is an example form: http://pastie.org/3142536
I have tried adding the JS to a partial: filename _recurly.js.erb
<%= render :partial => 'recurly.js', :locals => { :company => #company } %>
But its just outputting the JS to the page.
Is there a better way of doing this passing vars to JS for output and how can I get it to render the JS ?
Hope you can advise.
You need to use the function escape_javascript.
so your code should be:
<%= escape_javascript(render(:partial => 'recurly.js', :locals => { :company => #company }) %>
have you tried:
<%= raw(render :partial => 'recurly.js', ...) %>
Normally ERB will escape the output. Not sure if this is a good solution in the long run, even if it works, though.
why don't you use normal way of including javascript in your ERB view.
<%= javascript_include_tag "filename_from_public_javascripts_folder" %>

Using scope with a view partial

I have been using a scope to present some information to show todos that have been completed and are 24 hours old
scope :completed, joins(:user).where(:todos => { :complete => true })
scope :logged, completed.where("todos.updated_at <= ?", 1.day.ago)
Using the regular todo partial
<%= render #user.todos.logged =>
However I want to present those logged items in a different partial _logged.html.erb. I just can't figure out the proper way to pass scope results to a specific partial.
Thanks
Well, if you want to render partial for each item, yo can do:
<%=render :partial=> 'logged', :collection=>#user.todos.logged %>
Or if you want to pass the whole array to one instance then you can do
<%=render :partial=> 'logged', :object=>#user.todos.logged %>
In both cases I guess your object will be called logged.
Assuming that your partial contains <%= logged.title %> you want to render for each item, so you can use the first version.
First, to keep my conscience clean, let me say that that passing model code to your views is never a good idea. But if you insist :
<%= render :partial => 'some_partial',
:locals => {:some_variable => "somevalue",
:some_other_variable => some_other_variable} %>

Accessing nested model attributes inside a fields_for without using FormBuilder

I have a Rails form that is being used for creating and editing a model with a has_many relationship. I'm very familiar with typical forms with nested models, but my current problem requires accessing rendering some form elements using typical FormBuilder helpers and other HTML elements using data from the model itself. For example, my top level form has something like:
<% customer_form.fields_for :customer_images do |images_form| %>
<%= render :partial => 'customer_image_show', :locals => { :f => images_form } %>
<% end %>
Then, in the form partial, I need to do something like:
<dd><%= f.text_field :image_description %></dd>
... but also access attributes from the customer_images model (for example, the ID of the customer_image record).
I feel like this should be pretty straightforward and I'm just missing something basic. Any help is appreciated. This is a Rails 2.3.8 application.
You can call
f.object
to get to the object that the form is associated with.
Not sure if this is what you need, but couldn't you include the model data as a local? like:
<%= render :partial => 'customer_image_show', :locals => { :f => images_form, :customer_image => #customer_image } %>

Rails partial locals not persisting when sent to another partial as its own local

I render a partial like so:
<%= render :partial => 'widgets/some_partial, :locals => {:foo => 'bar'} %>
So inside of _some_partial.html.erb I render two more partials like so:
<% #foo.nil? #=> false %>
<%= render :partial => 'widgets/another_partial', :locals => {:foo => foo} %>
`<%= render :partial => 'widgets/another_partial_again', :locals => {:foo => foo} %>`
The foo local variable renders fine in some_partial.html.erb and even in another_partial_again.html.erb. However, the foo variable is inaccessible in another_partial.html.erb even though I explicitly passed it in the render call.
What is happening here?
Thanks for the help.
I had the undefined local variable or method error come up for me too when I was rendering a partial with :locals defined.
However, I had a different issue causing my problem, so I thought I would share my solution in case it helps anyone else. (This page was the first result when I googled this error after all)
Basically just make sure you use :partial => 'path/to/partial' in your call to render.
I.e.
<%= render :partial => 'widgets/some_partial', :locals => {:foo => 'bar'} %>
NOT like I was doing:
<%= render 'widgets/some_partial', :locals => {:foo => 'bar'} %>
Easy for a rails/ruby newbie like me to miss.
Solved. Turns out I was also rendering the same partial from the controller without sending the proper local variables. Thanks anyways!!!
Bumped into this very old question cause I faced the same issue.
Turned out that with Rails 4+ if you are not using collections or layout the correct way is:
# Instead of <%= render partial: "account", locals: { account: #buyer } %>
<%= render "account", account: #buyer %>
As documented here.

Resources