how to catch value of symbol :confirm in rails - ruby-on-rails

<%= f.submit "#{t('next_text')}", :class => "submit_button" ,:confirm=>'Edit all copies?'%>
how can I take value(true or false) of :confirm in rails 2.3.5?
I need this value to do some action in controller .

The easiest solution would be to just add a checkbox to your form whether or not to edit all copies.
If you really need the confirmation box on submit, attach an onSubmit handler to the form
<% form_for(#some_model, :html=> {:class => 'edit_confirmation'}) do |f| %>
<%= hidden_field_tag :edit_all, 0 %>
....
<% end %>
The javascript using jQuery:
$('form.edit_confirmation').on('submit', function(e) {
// the hidden field
var field = $('input#edit_all');
if(confirm("Edit all copies?")) {
field.val(1);
} else {
field.val(0);
}
return true;
});
In your controller you can get the result with params[:edit_all]

Related

Call update method when a checkbox is checked

I am trying to figure out how to call the update method when the user checks on the check box. I want to update the database by deleting the value for the check box that is stored in the database. Here is my code
<% #items.each do |item|%>
<%if item.email == #user.email%>
<%= form_for #item do |f| %>
<%= f.check_box :to_do, url: update,:value => item.id%>
<%= item.to_do%><br />
<% end %>
<%end%>
<%end%>
you can do something like..
by using checked property..call ajax using jquery
$('input[type=checkbox][id=to_do]').change(function() {
var hall_id = $('#to_do:checked').val();
$.ajax({
url:"/todo",
type:'POST',
//pass any agruments if needed
//data: {"id": pass any data that you need},
beforeSend: function (){
//show the user that something is in progess
$("#to_do").parent().html("Submitting..");
},
success:function(data){
//update user on success/failure
$("#to_do").parent().html("Submitted");
}
});
})
Now,just implement your action on the controller...thats it.
Hope it helps :)

How to use AngularJS with Rails Form?

I'm using simple_form with AngularJS:
= simple_form #post do |f|
= f.input :title, input_html: { "ng-model" => "title" }
It works great for my scenario on new post, but for editing on existing post, it doesn't bind/fill in existing value from post's title on form. From what I thought Rails already fill in the value, but AngularJS wipes it out after the page load because $scope.title is blank.
I found the trick is to actually create a controller with an init function that takes the value you want. In my case I just created a app/assets/javascripts/angular_app.js file that looks like this:
//= require_self
AngularRails = angular.module('AngularRails', []);
AngularRails.controller('PostFormCtrl', function($scope) {
$scope.init = function(title) {
$scope.title = title;
}
});
You'll have to translate the view into haml but it should look something like this:
<div ng-app="AngularRails">
<div ng-controller="PostFormCtrl" ng-init="init('<%= #post.title %>')">
<%= form_for #post, html: {name: "postForm", "novalidate" => true} do |f| %>
<%= f.text_field :title, "ng-model" => "title", required: true %>
<%= f.submit "ng-disabled" => "postForm.$invalid" %>
<% end %>
</div>
</div>
Remember to include the angular_app file into application.js and it should world. Obviously, this isn't a very robust solution but you could use active model serializer to convert the rails object to a json object. Then, pass that json object to the init function and in the controller, iterate over the key/value pairs of that json object and set them to $scope. Something like this:
AngularRails.controller('PostFormCtrl', function($scope) {
$scope.init = function(input) {
Object.keys(input).forEach(function(key) {
$scope[key] = input[key];
});
};
});
Hope that helps!

Two submit buttons one remote and one for submit in rails [duplicate]

This question already has answers here:
HTML form with two submit buttons and two "target" attributes
(16 answers)
Closed 8 years ago.
I looking for the solution to use two submit buttons with one form like the following:
The first should submit the given form, to the create function in my controller
The second should execute the given form remotely, to show the entered text under the form as preview
View:
<%= form_for #topic do |f| %>
.................
... some code ...
.................
<p>
<%= f.fields_for :topic_content do |tf| %>
<%= tf.text_area :text , :id => 'topic_text', :cols => 100 , :rows => 15, :class => 'topic_text' %></p>
<% end %>
.................
... some code ...
.................
<%= f.submit "Save", :name => 'save' %>
<%= f.submit "Preview", :name => 'preview' %>
<%end%>
<div id='preview_topic_text'>
</div>
Please change your submit to link_to tag and give id for that link_tag,then write ajax function
<%=link_to('Save', '#', :class => "save","data-button" => "save") %>
for preview
<%=link_to('preview', '#',:class => "save","data-button" => "preview") %>
jQuery(".save").click(function(){
var form_value = jQuery('#your_form_id').serialize();
var button = $(this).data("button");
if(button == "save"){
//your ajax code
// in data you append button the value
}
else{
}
// document.multi.action+="?"+form_value+"&flag="+ button;
// document.multi.submit();
});
so,in controller you can get params[:flag],then you could check the condition.
u can use PHP to define conditions of what button is pressed, try this code:
if (isset($_GET['submitbtn1']))
{
//execute the 1st code here
}
else if (isset($_GET['submitbtn2']))
{
//execute the 2nd code here
}

How to add a link_to in a select_tag with Rails/ERB?

I have the following code in my index.html.erb
<%= select_tag 'team_id', options_for_select(#teams.map{|team| ["#{team.name} #
{team.nick}", team.id] }) %>
Where would I add a link_to helper within that block? Can I even add a link_to for select_tag?
The desired link_to would go to '/Teamleader/ID_OF_OPTION_PICKED'
UPDATE:
To be more clear; when a user selects an option from the select tag, I want to redirect the page to the desired link (from link_to).
<%= select_tag 'team_id', options_from_collection_for_select(#teams, "id", "name") %>
<script>
$(function(){
$('#team_id').bind('change', function () {
var url = "/Teamleader/" + $(this).val()
if (url) {
window.location.replace(url);
}
return false;
});
});
</script>
Try:
<%= select_tag 'team_id', options_from_collection_for_select(#teams, "id", "name"),:onchange => "window.location.replace('/Teamleader/'+this.value);" %>

Using an AJAX call to update a rails page without a refresh?

I have a page on my site that lets users respond to invites by either accepting or declining them.
Their response is stored in the database as the boolean 'accepted', and is used to apply the classes 'selected' or 'not_selected' (selected makes the text orange) to the 'attending' div or the 'not attending' div.
<% #going, #not_going = invite.accepted ? ['selected','not_selected'] : ['not_selected','selected'] %>
<%= link_to(outing_invite_accept_path( { :outing_id => invite.outing_id, :invite_id => invite.user_id } )) do %>
<div class="attending_div <%= #going %>">
attending
</div>
<%end %>
<%= link_to(outing_invite_decline_path( { :outing_id => invite.outing_id, :invite_id => invite.user_id } )) do %>
<div class="attending_div <%= #not_going %>">
not attending</div>
</div>
<% end %>
When either div is clicked, it's diverted to the appropriate controller actions:
def invite_accept
#outing = Outing.find(params[:outing_id])
#invite = OutingGuest.find_by_outing_id_and_user_id(params[:outing_id], params[:invite_id])
#invite.update_attribute(:accepted, true)
redirect_to({:action => "index"})
end
def invite_decline
#outing = Outing.find(params[:outing_id])
#invite = OutingGuest.find_by_outing_id_and_user_id(params[:outing_id], params[:invite_id])
#invite.update_attribute(:accepted, false)
redirect_to({:action => "index"})
end
And as right now, this code works just fine. But it requires the index page be refreshed for it to take effect.
I know it's possible to update the page without a refresh using a jQuery ajax call attached to a listener on the appropriate div, but I have no idea what such a call would look like, or where to start, really...
You want to use rail's link_to :remote => true.
See http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
For dealing with callbacks you can bind to certain events that will trigger. For example:
<%= link_to "Click Me!", some_path, :class => 'ajax', :remote => true %>
<script>
jQuery(function($) {
$("a.ajax")
.bind("ajax:loading", console.log('loading'))
.bind("ajax:complete", console.log('complete'))
.bind("ajax:success", function(event, data, status, xhr) {
console.log(data);
})
.bind("ajax:failure", function(xhr, status, error) {
console.log(error);
});
});
</script>
This page is also a pretty good write up: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

Resources