Use Rails form element in 'show' view? - ruby-on-rails

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.

Related

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.

How to access a single, visible element from an array of objects being displayed by will_paginate?

I am working on a rails web app which manages students and courses. I have a courses controller which has the following index action:
def index
#courses = Course.paginate(:page => params[:page], :per_page => 1)
#courses.order(:startDate)
##thisCourse = Course.find(params[:page])
end
So pretty standard except for one thing - all the details of a single course are shown on one page and to show the details of the next course, you move to the next page of the pagination.
The problem is that in this index page showing the details of 1 course per pagination, I have a "Sign Up!" button which when pressed needs to create a a new record in the 'signups' db table which has the automated 'id' field and then the 'user_id' and the 'course_id' fields.
The 'user_id' is easy to find (current_user.id).
The 'course_id' is proving difficult. I imagine that pressing the Signup button should send the course_id to the signups_controller where a create function can do the work. But how can I get this exact course ID from the index page to the signups_controller's create action?
As you can see in the code I pasted from the courses_controller's index action,the '#thisCourse' variable has been commented out because I have found no way to define which course is currently being shown on the page.
The fields are rendered by the will_paginate Gem so I'm not sure how it's generating the fields but I was thinking that maybe I could create a named hidden field which includes the course_id and use that when the sign up button is pressed, however I'm not sure how to go about it.
Does anybody have any ideas?
Thanks!
Well, you can use show method (output one course) instead of index(output all courses) method, that will always get your course id through params.
Basically I changed my approach to the problem. I removed the button which was supposed to call the signups_controller and create the new record in the signups table. This button was replaced by adjusting the form_for helper so that it's submit button would send all the necessary data to the signups_controller (including the id value which was added to the form as a hidden field).
The form ended up looking like this:
<%= form_for course, :url => {:controller => "signups", :action => "create"}, :method => "post" do |f| %>
<%= hidden_field_tag :course_id, course.id %>
<%= f.label :"Course Title" %>
<%= f.text_field :courseTitle, class: 'form-control' %>
+ all fields included in the form....
<%= f.submit "Sign Up!", class: "btn btn-primary" %>
<% end %>
This parameter of form_for defines which controller and which action in that controller is the submission target:
:url => {:controller => "signups", :action => "create"}
and this parameter overwrites the default http action (default is PATCH but in this case I wanted to POST i.e. create a new entry in the signups table):
:method => "post"
I'm not sure if this is a very quick and dirty solution but technically it gets the necessary data to the correct destination controller.

rails erb form helper options_for_select :selected

I have an edit form in erb.
<%= form_for #animal do |f| %>
Within the code I have a select with options:
<%= f.select :gender, options_for_select([['Mare'], ['Stallion'], ['Gelding']], :selected => :gender) %>
However, the select is not showing the correct selected value.
What could I be doing wrong?
I can get it to work if I hardcode it but of course that is not a viable option.
In your code, your options_for_select() call sets the selected value to "gender" and does not attempt to use the value from your form object.
Please see the docs for options_for_select() for usage examples.
options_for_select(['Mare', 'Stallion', 'Gelding'], f.object.gender)
options_for_select(['Mare', 'Stallion', 'Gelding'], :selected => f.object.gender)
Alternatively, you can do this, which will already use the gender() value for your form object:
<%= f.select :gender, ['Mare', 'Stallion', 'Gelding'] %>
By the way, if you are using :include_blank => true, this will set your current selection to blank even though the form "knows" what is selected.

Inserting Javascript (or text) inside a form

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!

use form_for to submit data thats not selected by the user

I'm following this intro to rails: http://guides.rubyonrails.org/getting_started.html#creating-new-posts
and I want to be able to add data to my form thats not chosen in a field, that is just passed as a constant.
How do I do that?
<%= f.hidden_field :name, :value => value %>
See ActionView::Helpers::FormHelper

Resources