Remote AJAX call when checking a ceckbox in Rails 3 - ruby-on-rails-3

If I have a checkbox in Rails3, How can I perform a remote AJAX call when the user changes the value of it.
It sound like a simple task, but I fail to find a good solution for it.

You can pass the value of check-box on change event to the form and by using :remote => true you can use AJAX in form_for.syntax of on changing the check box and submitting the form is ":onchange => "this.form.submit()
I hope this would help you.
Thanks.

Related

Yii captcha never passes the validation even if, right letters are in place

This is on my model rules:
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
This is on my _form:
$this->widget('CCaptcha', array('buttonOptions' => array('class' => 'captcha-anchor')));
On my accessRules:
array('allow','actions'=>array('create','index','view','member', 'captcha'),
'users'=>array('*'),
),
I keep getting,
"The verification code is incorrect"
no matter what I do.
I'm using AjaxValidation with CActiveForm, and wish to keep using it.
Any clue how to fix this?
Please don't post any link, if you happen to know the answer, please describe it, so we can discuss if in need.
Please note that the captcha image is displaying and reloading. No issues there.
Thanks.
I have same problem and it return false every time because :
1- I define Get POST in some base controller and i send a ajax request to home controller which extended from base , because i can not call base controller directly so i change ajax request to current controller not home controller for all pages.
2- I hard coded JQuery in my layout so Yii load two JQuery in the Page so it send 2 request , first verified and second fails, i disabled JQuery auto load helping by this link.

Persist CMultiFileUpload selection through input validation

I'm using a CMultiFileUpload control in one of my forms like this:
$this->widget('CMultiFileUpload', array(
'name' => 'neueAnhaenge',
));
When input validation for some other form element fails and the input form is rendered again, a previous selection in this control is gone (as expected).
How do I repopulate this control, what do I have to do in my controller, is there a way to prepopulate this?
Thanks in advance.
For file fields it is rather impossible to reset the values assigned to it after it has been sent to the server.
One way to solve this would be to get the uploaded files, store them temporarily on the server and modify the form so it sends a reference to the file on the server.
A much better way would be to use Ajax or Client side validation of form fields to ensure no validation error occures when form has been sent. You can enable these options for CActiveForm: $enableClientValidation and $enableAjaxValidation.

Rails 3 - Ruby running within JS if

Why is ruby code ran even if its within an jQuery if statement?
I'm working with a modal (twitter bootstrap) and setting a session when the modal is shown/hidden, the modal has this code:
$("#modal").on("shown", function () {
<% session[:modal] = true %>
})
...
$("#modal").on("hidden", function () {
<% session[:modal] = nil %>
})
What I want is to set the session when the modal is opened, so a method knows when its opened, and set it to nil, so the same method knows when its closed.
Thanks.
Your ruby code is being run on the server and javascript in the browser. So all ruby code executes before javascript.
If you want the client modify something in the rails session, you'll need to send an ajax request to the server.
Another option might be to use session cookies instead of using the rails session. Session cookies are great if you're just setting little bits of data inside the browser. There's a few cookie plugins for jquery. Here's one on github. You could do something like this:
$("#modal").on("shown", function () {
$.cookie('is_modal', '1');
})
Update: I just read your other comment. If you're doing server logic based on that value, then yeah, you'll need to tell the server with an ajax call.

How do I validate a jQuery datepicker field using the Rails client_side_validations gem?

I'm trying to get a jQuery datepicker instance to validate with client_side_validations and not having any luck.
Anytime I include this in my model
validates_presence_of :due_date, :message => "Please select the due date"
The form just doesn't submit - no JS error, no log entry from Rails.
When I remove focus from the field without a value I do get the error message, so I know it's working at least a little bit.
Just can't figure out how to get the form submitting.
Thanks.
Seems to me CSV is hijacking the form before POST, because it doesn't pass, which makes sense. Do you have a :validate => true on the field itself, and are you using callbacks to generate a message? Because without them, it seems CSV will just reject the POST silently. Also, thinking about it, I've had a similar problem with select boxes and CSV, where no "field_with_errors" can be generated. Also, it's possible that the datepicker JS is conflicting with CSV.

Setting returnURL for CButtonColumn button

I'm looking at the controller for the default Delete button in the CButtonColumn class. It manages to return to the previous web-page after deleting a CGridView line and remain on the same page of the CGridView, as opposed to going to the first page. The lines responsible for this in the associated controller seem to be:
if (!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
I would like to create a new custom button that has this behavior (i.e. returning to the previous view without resetting the pagination to page 1), but simply including the above lines of code in the button's associated action does not do the trick. I think I need to send that 'returnUrl' parameter somehow, but I cannot figure out how :)
The 'returnUrl' code you are looking at uses a POST variable for the returnUrl. To use this, you will need to POST that somehow. On the View this code is called from I am assuming there is a <input name="returnUrl"> field in the form. You should make sure this field (populated with the correct URL value) is on all of the Views you are POSTing from in order to access that POST variable in your Controller action.
If you are POSTing to the deleteAction via AJAX, I think you can set the $_POST['returnUrl'] variable with the jQuery AJAX function.
Another way to go might be to use CWebUser's returnUrl SESSION variable instead of this POST variable. I have never done this, but it's built in to Yii so I assume it works OK.
I never really liked the hacky $_POST['returnUrl'] that Gii generates anyway.
ANOTHER thing you could do, possibly, is look at the $_SERVER['HTTP_REFERER'] variable, and use that for the return redirect in your deleteAction. I don't know if that will be set correctly though, with complications from the 302 redirect/rewrites that Yii does.
Good luck!
You can set the return url via the CHtml::link call. Here is an example using delete
CHtml::link(
'Delete',
'#',
array('submit'=>array('delete','id'=>$model->id),
'params'=>('returnUrl'=>'controller/action...'),
'confirm' => 'Are you sure?'
)
);
Pulled from this Stackoverflow answer.