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.
Related
Noob question! :)
I have a form, that has basically no point other than call some_action. The reason I use a form for this, is because we have a specific styling for this in our large website.
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= f.save_button %>
<% end %>
I thought, since it's a form, I should be able to put a checkbox in there. It should have no other goal than confirming the user wants to do this action indeed. E.g. "Yes, I want to do some_action to the user model".
How would I make a checkbox that does not really change any attribute or affect anything - Other than that it should be checked for the form to submit?
This is probably dead simple, but according to the documentation and various error messages I should provide arguments such an attribute (which I don't want...)
form_for is meant to work on attributes of a model, which is what all the documentation you are reading is telling you. So if your model had a boolean column you could easily attach a check box to it.
If you ever want a form (or specific tag) that does not follow this, you can use the _tag version of these methods. For example, form_tag or, in your particular case, check_box_tag.
Example:
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= check_box_tag "do_some_method" %>
<%= f.save_button %>
<% end %>
NOTE: You will only get a param entry for :do_some_method if it is checked off. If you want to get a param regardless, you have to add a hidden_field_tag before it.
<%= hidden_field_tag "do_some_method", "no_dont_do_it" %>
<%= check_box_tag "do_some_method", "yes_do_it" %>
Now if the checkbox is selected you'll get params[:do_some_method] set to "yes_do_it"; if it's not checked off, instead of getting no entry, you'll get params[:do_some_method] set to "no_dont_do_it".
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.
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.
I have this form:
<%= form_tag posts_path, :method => :get, :class => "search_nav" do %>
<%= text_field_tag :search, params[:search], :class => "input-long search-query", :placeholder => "#{t('.search_nav')}" %>
<%= hidden_field_tag('ip', "#{request.ip}") %>
<%= hidden_field_tag('city', "#{request.location.city}") %>
<%= hidden_field_tag('country', "#{request.location.country}") %>
<%= content_tag(:div, "",:class => "icon-search") %>
<% end %>
I get a url something like:
http://localhost:3000/en/posts?utf8=%E2%9C%93&search=check+params&ip=127.0.0.1&city=&country=Reserved
My question is:
Can I hide or encrypt the url params ip, city and country?
I can not use POST because I have paginate results:
<a rel="2" href="/en/posts?city=&country=Reserved&ip=127.0.0.1&page=2&search=check+params&utf8=%E2%9C%93">2</a>
<a rel="3" href="/en/posts?city=&country=Reserved&ip=127.0.0.1&page=3&search=check+params&utf8=%E2%9C%93">3/a>
Encrypting URL parameters is pretty pointless. Why don't you want the user to see these values? Sure you COULD encrypt them before adding them to the form, but is that really necessary?
Furthermore, and perhaps more importantly, if these values are based on the request, then there is a good chance you don't need to submit them in the first place. #{request.xxx} is going to be the same on the result page as is on the form page. Is there any good reason to pass these along? By submitting these as GET parameters, you're actually sending redundant information to the server. Ruby/Rails is already going to calculate these values based off of the IP address automatically when the next page is loaded.
The problem here isn't with the form, but rather with the logic you've applied to designing it. I think you may have over-analysed your situation, and need to take a step back and re-think the problem.
I have a similar form like this:
<%= form_for :customer,
:params => #result && #result.params[:customer],
:errors => #result && #result.errors.for(:customer),
:builder => BraintreeHelper::BraintreeFormBuilder,
:url => Braintree::TransparentRedirect.url,
:html => { :autocomplete => "off"} do |f| -%>
First name: <%= f.text_field :first_name , :value => "John"%><br />
<%= f.submit %>
<% end %>
When I try to add the value to that text field is not letting me. However, if I take the builder from the form_for tag, I am able to do so. Anyone has had experience with Braintree form builder?
Siwei Shen is on the right track...
Form builders in Rails help you tie your forms to models in your application and generate values, names, and IDs for your form elements that correspond to model attributes. They're tremendously helpful, but sometimes it's necessary to override what they give you.
The example form builder you found in the Braintree Rails example app provides some things that are helpful since TR forms post directly to our gateway rather than your app and gives your form fields attribute names and default values that Braintree's gateway will expect as well as helps to populate validation errors which are returned to your app.
In this particular case, the form builder is ultimately merging the values for the inputs (including :first_name) with either the existing value (if any) or nil (over-writing what you've passed in.
https://github.com/braintree/braintree_ruby_examples/blob/master/rails_tr_checkout/app/helpers/braintree_form_builder.rb#L22
Feel free to modify the form builder to suit your needs!
when using "form_for", the default value is the form-object's value. e.g.
#person = Person.new(:first_name => "Jack")
<%= form_for :person do |f| %>
<%= f.text_field :first_name %>
<% end %>
and you will get a text input with the default value "Jack" :
<input type="text" value="Jack"/>
so, when using f.text_field, you are not able to set the default value, unless you change the form-object ( here is the #person).
If you want to set the default value, I suggest you either use "form_tag", which does not have "form object", or use jQuery to implement the default value stuff.