2

In my nav-list, I have a drop box selection that contains all users, exclusively available to an administrator. The administrator can select a user, and the view is updated via ajax to display the selected user's information.

The view is updated with something like this:

$(#Users).change(function() {
    $ajax({
        ... yada yada update information in view.

However, what I want to implement is if I change to another view/url (Such as my account html.erb to a mygraph.html.erb) or refresh my page, I want the user drop box selection to persist with the user selection I made.

I thought of using rails cookies to do this. But I must redirect to controller to set the cookie.

I attempted a POST ajax call,

$(#Users).change(function() {
  $.ajax({
    ...
    data: {
       "<%= cookies[:selected] %>": this.value

It doesn't work, but for some reason, I already doubted that it would be that simple.

How should I implement this solution?

1 Answer 1

4

You don't have to redirect_to controller action. You can make ajax call to controller action. You can set cookie in controller action as follow:

 $(function(){
   $('#user_select')change(function(){
     value = $(this).val()
     $.post("controller/action",{selected_value : value}, function(data, status){
       if(status == "success")
       {
         alert(data);
       } 
    });
   return false;
   })
  })

Controller:

 def action
   cookies[:name]=params[:selected_value]
   render :text => "success"
 end 
Sign up to request clarification or add additional context in comments.

2 Comments

Nice! A quick question, how should I pass the user_select value to the action?
The render is pretty intrusive but unavoidable right? I tried to use render :nothing => true but it still displays an empty message. I used a render json: something, but that seems like a hack to me :C. Anyway thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.