Auto click a link in ruby on rails - ruby-on-rails

Hi I am working in ruby on rails,
I want to auto click on
<%= link_to 'Referesh', :onclick => 'window.opener.location.reload(true)' %>
when I submit the form by
<%= f.submit 'Save' %>
My question is how I can perform auto clicking on link_to when I submit the form by f.submit.
Both are in the same form.

I am not sure why/what you want to do.. the question is not clear. However, you can override the onsubmit() and do something there.
If you're using JQuery, you can trigger the click event on a link with something like this:
$("a#my_link").trigger('click');
If you're using Prototype, you should include event.simulate.js
I've used this several times and it works like a charm. It allows you to manually trigger native events, such as click or hover:
$('a#my_link').simulate('click');

Related

On click submit button state change in ruby on rails

I want to change to state of the submit button when a user clicks on that I want to make these transitions on that button.
Text change to "submitting..." and state of that submit button becomes "disabled"
I know how to do this with Jquery something like:
Let's say that the button has a #derp id.
$("#derp").click(function(){
this.toggleClass("pressed");
});
Basicly this will add or remove the "pressed" class from the item based on its state. Just add the css for .pressed and you're ready to go.
#derp {...}
#derp:hover {...}
#derp:active {...}
#derp.pressed {...}
Or we do using different selectors.
Question: Is there is default functionality in ruby on rails.
Update:
I am using ajax based form submission and when i try to click 2-3 times form submittion quickly. Its actually submitted 2-3 times. I want to prevent that thing and I am using simple_form
<%= f.button :submit, "Submit Review", :disable_with => "Processing..." %>
You can use :disable_with option:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag
To make :disable_with Work with Ajax Remote You have to edit your public/javascripts/rails.js and change
document.on("ajax:after", "form", function(event, element) {
to
document.on("ajax:complete", "form", function(event, element) {

Ruby on Rails submit button

How can I change submit button on form to text?
<%= f.submit "Text" %>
I know how to change text on button but I need to change a way to submitting.
You can press on link, for example Submit and submit.
You don't say what ruby version you are using. But try this
<%= submit_tag "Text" %>
Note that there is no form helper here, so no "f." in front
In order to change the submitting of a form to be triggered by a link as opposed to a submit button, you must use javascript instead of raw generated HTML only.
See similar answers on StackOverflow but a summary of a possible solution in jQuery (which comes baked into recent versions of Rails) would be as follows:
$('#submit_link').click(function(){
$('form').submit();
return false;
});
Update: This should do it using HAML notation in Rails 2.3.x:
- form_for(#object) do |f|
= f.submit 'Update Or Whatever'
You could also try "button" tag or use an image link that looks like a button.

How can I use radio buttons properly in Rails

I am doing my first project using Ruby on Rails and need to display a set of radio buttons. It should behave exactly like a selection list. For usability reasons, I need it in a radio buttons format.
In my project, I use the collection select which also allows me to display on the edit page as follows:
select('project','project_type_id',#project_types.collect{|project_type|[project_type.name,project_type.id]}) <br>
I need something exactly like this, (especially the ability to display the selected value in the edit page) but using radio buttons.
I did a Google search and read the entire Rails guides on radio buttons but I can't find the answer.
How can I do this?
I suppose you can do it like this in your view
<% #project_types.each do |project_type| %>
<%= radio_button("project", "project_type", project_type.name) %> #assuming you have a name attribute on project_type
<% end %>
If you want a particular radio button to be checked then you can pass the checked option like so
<%= radio_button("project", "project_type", project_type.name, {:checked => true}) %>

How to show the content of the form just entered in the ":confirm =>" option on Ruby on Rails

I am trying to have a way of confirming the information entered before actually saving it to the DB
Considered making an individual confirmation page as discussed here
Ruby on Rails: Confirmation Page for ActiveRecord Object Creation
However my form includes an attached file using paperclip which makes it more of a hassle to implement.
I thought of just having a :confirm => pop up that would show the information that the user
had just entered.
The problem is how to show the information that the user had just entered, for example
<% form_for #entry, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<%= f.label :name %><br />
<%= f.text_field :name %>
<%= f.label :file %><br />
<%= f.file_field :file %>
<%= f.submit 'Create', :confirm => "????? " %>
<% end %>
Given that your loading attachments it may not be a bad idea to render a staging view including information derived from the attachment allowing the user to confirm. As in display the file if it's an image, or the first paragraph of text if it's a text file, etc.
It's going to take more work than the just adding a confirm pop up, but I feel the enhanced user experience is worth the extra effort.
I'm not familiar with the way that paperclip works. So you're mostly on your own for the intimate details.
You will probably have to create a record before the staging view can be rendered with the sample of the uploaded file. To accomplish that I'd set up an "active" column on the model in question, that defaults to false.
Usage would look something like this:
User complets new form.
Attachment is updated and records are saved, with the active field set to false.
Redirected to confirmation page that is essentially the show page with a confirm link/button and a cancel link/button
a. When the confirm link/button is clicked it sends a request to the controller triggering the update action on this record setting active to true.
b. When the cancel link/button is clicked it sends a request to the controller trigering the destroy action on this record.
All that's left is to set up a recurring task to remove objects that are inactive and were crated long enough ago that it's safe to assume the user has just ended the browser session.
The confirm option for the Rails submit method can only take a text value.
If you want to dynamically generate the confirm text, one way you could do it is to write your own HTML submit tag, and write custom javascript to analyse the fields you want to use in your generated text.
Or you could use the Rails submit method, but use something like JQuery to add an event handler to it.
I'd just throw my js into an onclick handler. That's all Rails does.
<%= f.submit 'Create', :onclick => "confirm('Name is ' + $F('entry_name'));" %>
(note, didn't test that, but it looks close. confirm is a core js function, not part of any lib)

Should I be putting html input tags in my view?

I'm new to rails, and was wondering if I should be putting code like the second line inside my view:
<%= text_field_tag 'new_ingredient' %>
<input type=button ID="btnAddIngredient" Value="Add" onclick="addIngredient();" />
or is there a helper I should be using to generate the tag instead? I know about the form and tag helpers, but I don't want this particular button to be a form submission button. It's just a button that manipulates some items via javascript.
I'm guessing I should be using a helper, but I'm still trying to get familiar with the Rails API documentation and can't seem to find what I am looking for.
Depending on your approach, it may also be worth noting that putting JS function calls into on-click events is considered bad-form and will be on its way out in Rails 3, I believe. Depending on your JS framework, its better to listen for the click event on the button and act on that.
Running with jQuery, you could do something like this in the view:
<%= button_to "Add", :url => "#", :id => "btnAddIngredient" %>
And this in your application.js or other JS file:
$("#btnAddIngredient").click(function() {
addIngredient();
});
you can use button_to_function, like this
button_to_function "Add", :id => "btnAddIngredient", :onclick => "addIngredient();"
hope it helps =)
more details here: http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M001757

Resources