Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6341495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:02:45+00:00 2026-05-24T20:02:45+00:00

I am using Rails 3, Jquery-UJS and Jquery. I have a working setup of

  • 0

I am using Rails 3, Jquery-UJS and Jquery. I have a working setup of Jquery with Rails3 .I am trying to send a variable from Javascript to Rails controller by clicking a link.

My current approach is using js as follows to make an ajax call and pass msg string.

$("#select_link").click(function() 
{$.ajax({
type: 'POST',
url: 'http://your_url/jmsg',
data: {msg: "hello world"},
;}
});
});

Should I include this code in my application.js file?

In my .html.erb file I have

<%= link_to "Send variable", jmsg_path, :remote => true %>

In my controller I have

def jmsg
@message= params[:msg]
respond_to do |format|
format.js
end
end

Till now, I’ve not been able to get the javascript variable in my @message instance variable

EDIT:

I made changes as suggested. However, the variable @message is not set when I do

  @message=params[:msg]

When I check XHR using Fiebug, I see 302 Moved Temporarily for the POST action (the POST parameter msg is set correctly though). Response is – $(“#show_message”).html(“”)

My .js view is:

 $("#show_message").html("<%= @message %>")

EDIT2 : When I check Firebug, it shows that there are two actions now:
1. POST /jmsg – with response @message = “hello world”
2. GET /jmsg – with response @message= ” “

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:02:45+00:00Added an answer on May 24, 2026 at 8:02 pm

    When i wanna get any value from controller, i always use json format by doing something like these.

    // jmsg.js.erb

    {
      "message": "<%= @message %>"
    }
    

    // yourjs.js

    $.ajaxSetup({
      'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript") }
    });
    
    $("#select_link").click(function(){
      $.ajax({
        type: 'POST',
        url: 'http://your_url/jmsg',
        dataType: 'json',
        data: {msg: "hello world"},
        success: function(json, status, xhr){
          // $("#show_message").html(json.message)
        }
      });
    });
    

    // in your.html.erb, remove :remote => true

    <%= link_to "Send variable", jmsg_path %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Rails 3.0.3 with jQuery ujs. I want to send a form
I am using Rails and jQuery, making an ajax call initiated by clicking a
I'm using rails, but doing all my Javascript unobtrusively using jQuery in application.js .
I'm using jQuery with rails and have the following piece of code $('#related').html(<%= render
I am using jQuery with rails. I am trying to make a PUT request
I have started using jQuery and rails. I have made a simple form that
Using Rails v2.1, lets say you have an action for a controller that is
I'm using Rails with jQuery, and I'm working on a page for a simple
I'm using Ruby on Rails with jQuery and trying to do the following: <%=
I am trying to update a record from an index using either jQuery alone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.