0

View

<%= link_to "Link", {:action => "AjaxView",:col => "colname"}, 
            :update => "Ajaxcall", :remote => true %>

Controller

  def AjaxView
    @vars= Var.find(:all,:conditions => { :varName=> "one" },:select=>(params[:col]))
    respond_to do |format|
      format.js { render :layout=>false }
    end
  end

AjaxView.js.erb

   if ( (<%= col %>) == "colName") {
     $("#3").text("<%= escape_javascript(render(:partial => "var") %>");
   }
   else
   {
     $("#2").text("<%= escape_javascript(render(:partial => "var") %>");
   }

Issue here is this Variable "col" doesn't get its exact value in AjaxView.js.erb file, and my If else condition doesn't get executed. What am i missing here ?

2
  • side note: the modern approach is to get JSON (not JS code) and let JS (in the client) update the view. Commented Jan 2, 2012 at 13:32
  • @amit don't replicate questions stackoverflow.com/questions/8712852/… Commented Sep 26, 2012 at 12:14

3 Answers 3

0

The local variable col isn't assigned in AjaxView.js.erb. I think you meant to write it as params[:col].

Also the double quotes around var should be single quotes, to play nice with the surrounding double quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

is tried with params[:col] in my if condition, but it didn't worked.
0
{:action => "AjaxView",:col => "colname"}

This is not how you build a url for link_to.

What's the route to access the AjaxView method?
It should be something like: link_to "Link", AjaxView_resources_path(:col => "colname").

And in your controller, you need to set an instance variable @col = params[:col] to be able to use it in your view.

ps: your should name your method ajax_view instead of AjaxView.

9 Comments

Thanks @Robin for your reply, when I put "( @ col = params[:col])" in controller and in AjaxView.js.erb when I run this " (if ( (<%= @ col %>) == "colName"))", it still didn't worked.
The thing is, with the way you build your link, I don't even know how your action gets executed...
lot of people have doubted that, some even suggested that I should start using form instead of link, there are blogs written on this, but somehow i was able to run this without any issue. And it works for me, only issue here that I am facing now is to try with multiple options, and for that I have to run if condition in .js.erb file, if you could help me running this, I will be really thankful. I am almost done with my work.
Also, AjaxView_resources_path(:col => "colname") is same as {:action => "AjaxView",:col => "colname"} just the way of writing is different, whatever I am writing in curly bracket goes as a HTML input.
I dont think it is... How is rails supposed to know about which controller to use? Also, I can't see why you would need a form. A link is fine in this case.
|
0
  if ( @col.to_s == "colName") {
      $("#3").text("<%= escape_javascript(render(:partial => "var") %>");
     }
      elsif ( @col.to_s == "colName1")
     {
     $("#2").text("<%= escape_javascript(render(:partial => "var") %>");
     }

Comments

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.