Inserting Javascript (or text) inside a form - ruby-on-rails

I need to trigger some javascript to load up an extra part of a form in activeadmin. For the "create" action I've successfully triggered the javascript from the onchange event of a dropdown box, but for the edit action it needs to be triggered from the form load.
I've found I can trigger it with something similar to
javascript_tag "loadRecipeEdit(3)"
but I can't embed this into the form action for activeadmin without it breaking the structure of the form (formtastic issue)
form do |f|
f.inputs "Details" do
f.input :name
f.input :production_date
if f.object.new_record?
f.input :recipe, :input_html => { :onchange => "loadRecipe()" }
else
javascript_tag "loadRecipeEdit(3)"
end
end
f.actions
end
Any thoughts of other ways to trigger the javascript would be much appreciated!

Ah, found an alternative:
f.form_buffers.last << javascript_tag("loadRecipeEdit(3)")
Hope its useful!

Related

Managing the behavior of the "Cancel" link in Rails' ActiveAdmin forms

A typical form produced by the ActiveAdmin DSL might look like so:
ActiveAdmin.register Post do
...
form do |f|
f.inputs do
f.input :title
f.input :content
end
f.actions
end
...
end
The f.actions bit renders both a submit button and a cancel link. The problem is that the latter's target is always the index page. Instead, for a previously persisted record I would like to be taken back to the show page. As a possible workaround, f.actions could be replaced with:
f.actions do
f.action :submit
if resource.persisted?
f.cancel_link({action: :show})
else
f.cancel_link
end
end
This, however, seems verbose and clunky. Am I missing some more idiomatic trick to accomplish the same?

Rails - PreFill form using previous value store in database

I have created a form,
<%= semantic_form_for [:admin, #resource], builder: ActiveAdmin::FormBuilder do |f| %>
<%= f.semantic_errors %>
<%= f.inputs do %>
<%= f.input :resource_template, :as => :select, :collection => Resource.select {|r| r.resource_template ==true }, :input_html => { :style => 'width: 20%;'} %> //added now ,just to try out how it may work.
<%= link_to 'COPY', admin_root_path, class: 'myButton' %> //added now,just to try out how it may work.
<%= f.input :title %>
<%= f.input :url %>
<%= f.actions %>
This form helps us creating new resources, now I have added a drop-down to select old existing resources.
How can i pre-fill the rest of form after clicking COPY of whatever value selected in the dropdown list?
User might edit some prefill value and create its own new resource accordingly.
Unless you're OK with submitting the form immediately after the user selects an option, which id probably a bad idea, you're going to have to do this with JavaScript.
The exact implementation depends largely on how your app is configured, but the general approach would probably be something like:
Add an endpoint in your resources controller that accepts an POST request with an :id param and returns a JSON version of your resource
Add a change listener on your select box that sends a POST request to the endpoint in step 1. You'll also have to send the resource id as a param.
If the response from the endpoint in step 1 returns any data, use the keys and values in the JSON data to fill the form fields accordingly
This is a fairly broad question, so it's hard to give you a more detailed answer without knowing which JavaScript frameworks you have in your app, but this should point you in the right direction.
On clicking of copy button you need to fire an ajax request with option selected in the select box, get data in controller with which you want to fill the form, in success callback of your ajax request , fill the fields returned from the ajax request.

ActiveAdmin form on show page without a panel

I'm trying to add a form to the show page of a resource. The form works just fine, but it will only show up if I wrap it in a panel declaration, which results in a panel-within-a-panel.
Any idea how to get the form to show up without wrapping it in a panel?
Here's the code I have right now:
show do |ad|
...
panel "Foobar" do
semantic_form_for [:admin, resource, resource.reactions.build], builder: ActiveAdmin::FormBuilder do |f|
f.inputs "New Reaction" do
f.input :title
f.input :content
end
f.actions
end
end
end
Thanks!
An alternative to the panel would be to use a div.
The root issue is mixing Arbre and non-Arbre components; here those components are panel and the semantic_form_for. This alteration works because the last return value in an Arbre block is wrapped in a text_node when it is a String. In this case, the form is output as a string and then is wrapped as a text_node within the div, which renders the form. See Arbre's Element source for more info.
show do |ad|
div do # <- Note the div
semantic_form_for [:admin, resource, resource.reactions.build], builder: ActiveAdmin::FormBuilder do |f|
f.inputs "New Reaction" do
f.input :title
f.input :content
end
f.actions
end
end
end

Use Rails form element in 'show' view?

I'd like to have a split-dropdown button (twitter bootstrap style) in my show view so user can change the current 'state' for their 'job' using a collection (bids, in_progress, complete, canceled).
Here's how I'm current changing the states in the my _form (works fine):
<%= f.input :state, :collection => %w(bids in_progress complete canceled), :checked => 'bids', :as => :radio_buttons %>
But I'd rather the user not have to go through the entire form just to change the state. Would be nice for them to change the state 'on the fly' if you will.
How should I go about converting this to a dropdown and use it outside of my form? Note - I still want to retain the ratio collection in my form.
Should I create a partial with just the button and render it in the 'show' view? Do I need to add something to my controller? Maybe it would be easier/better to use javascript? Lots of questions, not sure where to start. Thanks.
Update
I've created a partial with this:
<%= simple_form_for(#job, :remote => true) do |f| %>
<%= f.input :state, collection: [ ['bids', 'Bid'], ['in_progress', 'In Progress'], ['complete', 'Complete'] ], label_method: :last, value_method: :first, :onchange => 'this.form.submit()' %>
<% end %>
And added this bit of javascript:
$(this.form).submit();
This is not working - am I on the right track? It seems like this should submit the form.
You could create a new form with only the state field directly in the show view (or in a partial, doesn't really matter). Then have some javascript submit the form whenever the dropdown is changed.
If you use rails form-helper they will point the submit to the update action of the controller and since only the state attribute is present that's the only attribute that will be updated.

Rails remote form submit through javascript

I'm trying to create a very small simple form that edits a single checkbox, and submits automatically with AJAX when the checkbox is modified. Here's the code, which other SO questions imply should work:
<%= form_for(workitem, :remote => true) do |f| %>
<%= f.check_box :is_complete, :onchange => 'this.form.submit()' %>
<% end %>
The problem is that this results in a full page HTML submit, rather than an AJAX submit. How do I trigger an AJAX submit?
This seems to work:
$(this.form).submit();
Good 'ol jquery to the rescue.
This is simply in Rails way :
$("#myform").trigger('submit.rails');
find it here : same question

Resources