Params function not working

Either params function is not working or I am misunderstanding what it’s
role is. In this line:

Site

the value of order is either -site_num or site_num.

I have a function in javascript that alters it:
sort: {
insert: function(order, page){
arr = [];
arr[arr.length] = order;
arr[arr.length] = page;
return function(order_value, page_value){
alert((typeof order_value) + " : " +
order_value.toString());
var order_value = (order_value.indexOf(“-”) > -1) ?
order_value : “-” + order_value;
var page_value = page_value;
return {order : order_value, page : page_value };
}
},
msg: “Sorting…”
}
}

The result gets passed through ajax:
$.ajax({
type: ‘GET’,
url: ‘/’ + url,
data: params,
dataType: ‘html’,
error: function(){},
beforeSend: function(){},
complete: function() {},
success: function(response) {
listview.html(response);

      var form = $('form');
      form.calculation();

      var table = $('table');
      table.calculation();

  }
})

When I look at my query string in the console of firebug: it always
shows site_num to be negative after the first time. It’s supposed to
toggle between “-site_num” and “site_num”. But it never happens.

So is the params[order] not assigning a value into the href of the link?
And if it’s not, then how would I do this?

I have the full javascript here:

Thanks for any response.

params is not a function is a hash, hashes are unordered key value pair
in
ruby, the key are symbols. The hash is assembled by rails from the query
string or from serialized data( i think :S) like a form.

If you want to pass data to java script use the render method like this

render :json => @product

more info here

Radhames Brito wrote in post #949717:

params is not a function is a hash, hashes are unordered key value pair
in
ruby, the key are symbols. The hash is assembled by rails from the query
string or from serialized data( i think :S) like a form.

If you want to pass data to java script use the render method like this

render :json => @product

more info here

Layouts and Rendering in Rails — Ruby on Rails Guides

The only interaction I want is from javascript to Rails. While the
params does seem to be grabbing the query string, it is always returning
the “-” in front. Could this be the issue:
<%= (params[:order].nil?) ? :site_num : params[:order] %>

Radhames Brito wrote in post #949984:

The only interaction I want is from javascript to Rails. While the
params does seem to be grabbing the query string, it is always returning
the “-” in front.

Oh then you are doing it all wrong, you should be usign a getScript and
let
rails return a js.erb file that uses a partial to redraw part of the
page.

That sounds like it’s orthogonal to the OP’s concern. If the - is
always there, then I guess the params hash is getting populated, just
with the wrong data.

Anyway, any solution requiring js.erb is, almost by definition, “doing
it all wrong”. js.erb should never be necessary in a well-written
project: you shouldn’t be dynamically generating source code.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

The only interaction I want is from javascript to Rails. While the
params does seem to be grabbing the query string, it is always returning
the “-” in front.

Oh then you are doing it all wrong, you should be usign a getScript and
let
rails return a js.erb file that uses a partial to redraw part of the
page.

check this example code

John,

I’m not able to run your code, so my observations are based purely on
reading your email. From what I can gather, the problem lies in your
javascript, specifically the following assignment:

 var order_value = (order_value.indexOf("-") > -1) ?

order_value : “-” + order_value;

Your ternary expression evaluates to order_value if it contains a ‘-’
character, and to ‘-’ + order_value otherwise. This will result in a
value that always includes a ‘-’ character. If you can safely assume
that ‘-’ will always be the first character, you can do something like

var order_value = order_value.indexOf( ‘-’ ) > -1 ?
order_value.substring( 1 ) : ‘-’ + order_value


med vnlig hlsning
David J. Hamilton

@marnen

He has a sorting function, i believe thats what he wanted to do, wouldnt
it
be easier for him to make an ajax call and return a partial?
that is what i am suggesting.

Anyway, any solution requiring js.erb is, almost by definition, “doing
it all wrong”. js.erb should never be necessary in a well-written
project: you shouldn’t be dynamically generating source code.

What do you mean by “dynamically generating source code” here? Are you
saying that going for a partial to the server using Ajax is the wrong
thing to do?

No, absolutely not. I am saying that dynamically generating JS by means
of ERb is a design problem. Receiving a partial from an Ajax call is
fine.

I would really like to see an example of how to pull a partial with ajax
without js.erb or js.haml, in a easy way of course, note that if he
wants to
sort the partial is dynamic.

please , post a snippet.

OT.
John please confirm if your intention is to sort a list, thats what i
understood you wanted to do, if not please clarify.
What i understood from your code is that when the user hits the Site
link ,
you want to order the list by site number and that
“-” in front is meant for choosing asc or desc , i also see you are
paginating, am i correct?

pepe wrote in post #950138:

Anyway, any solution requiring js.erb is, almost by definition, “doing
it all wrong”. js.erb should never be necessary in a well-written
project: you shouldn’t be dynamically generating source code.

What do you mean by “dynamically generating source code” here? Are you
saying that going for a partial to the server using Ajax is the wrong
thing to do?

No, absolutely not. I am saying that dynamically generating JS by means
of ERb is a design problem. Receiving a partial from an Ajax call is
fine.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Radhames Brito wrote in post #950151:

No, absolutely not. I am saying that dynamically generating JS by means
of ERb is a design problem. Receiving a partial from an Ajax call is
fine.

I would really like to see an example of how to pull a partial with ajax
without js.erb or js.haml, in a easy way of course,

I’ve just been looking at your sample code on Github, and I see the
(anti)pattern you’re using. What you want to do, it seems to me, is
this.

  1. Use an Ajax call to get a rendered partial (in HTML) from Rails.
  2. Put that HTML into the DOM.

So, something like (with jQuery):
$.ajax({ url: “http://myrailsapp.com/some_partial”, success: function
(data) { $(‘#destination_element’).innerHTML = data })

This is, I believe, the proper way to do it.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Radhames Brito wrote in post #950213:

So, something like (with jQuery):
$.ajax({ url: “http://myrailsapp.com/some_partial”, success: function
(data) { $(‘#destination_element’).innerHTML = data })

This is, I believe, the proper way to do it.

would it be

http://myrailsapp.com/mycontroller/some_partialhttp://myrailsapp.com/some_partial”?

Depends on your routes! But you knew that. :slight_smile:

Yeah, I was just giving a sample URL. I wasn’t worried too much about
the exact structure of it.

makes sense, it jumps one step from the ones im used to, ill tst it when
i
get home.

and what do you mean by “(anti)pattern”?

I mean that you’re apparently using it as a repeated pattern. I think
it’s an antipattern – something that looks like a good design pattern
at first, but is in fact to be avoided.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I mean that you’re apparently using it as a repeated pattern. I think
it’s an antipattern – something that looks like a good design pattern
at first, but is in fact to be avoided.

I intentionally user $.get and $.post to a keep it simple, instead of
using
$.ajax, but the pattern i think is not wrong, just outdated maybe.

David Hamilton was right. This was a javascript issue, not a Ruby issue.
This worked:
var order_value1 = (order_value.indexOf("-") > -1) ?
order_value.substring(1) : “-” + order_value;

Now regarding what’s the intentions here. If you look at the full
javascrpt I posted in stackoverflow, you will see that when someone
clicks a link, I basically check the link href attribute in order to
determine which list to populate. So if it’s posts, it will populate the
posts page with records from the posts table in the database. Now when
that initializeTable method is called, it instantiates a Form and Table
object. These objects inherit certain properties, so when a user
interacts with their subelements, such as dropdown, input field, links,
it will call the list object’s methods in order to filter, search, sort,
etc the table. Now the idea is that the lists object just returns a
bunch of hashes (key/value pairs) that get appended to query string so
that the params hash in Rails can catch it and send it to the model to
perform sql. At least that’s what I am hoping to accomplish here. I am
not trying to use any plugins at all. I am trying to do this purely in
javascript (using jquery library but no jquery plugins) and in rails.

On Oct 15, 3:43am, radhames brito [email protected] wrote:

I mean that you’re apparently using it as a repeated pattern. I think
it’s an antipattern – something that looks like a good design pattern
at first, but is in fact to be avoided.

Marnen,

As someone who is a relative rails newbie, and whose last programming
experience was 25+ years ago programming assembly language and pascal,
it would be appreciated if you could elaborate as to why it should be
avoided.

Given the sparse documentation on ROR I have picked up this
“antipattern method” from watching a Ryan B. screencast, and would
like to better understand whether it would be worth the effort to
refactor. In the web application we are developing we have made the
implicit assumption that the site should require javascript enabled by
users, as we are heavily dependent on google maps and other content
only available with javascript.

From your extensive knowledge would there be any design or performance
advantage in refactoring so that the browser requests a pure html
partial only?

Hopefully you are not going to tell me to google the answer!

Thanks for sharing,

David

So, something like (with jQuery):
$.ajax({ url: “http://myrailsapp.com/some_partial”, success: function
(data) { $(‘#destination_element’).innerHTML = data })

This is, I believe, the proper way to do it.

would it be
http://myrailsapp.com/mycontroller/some_partialhttp://myrailsapp.com/some_partial”?
makes sense, it jumps one step from the ones im used to, ill tst it when
i
get home.

and what do you mean by “(anti)pattern”?

On Oct 15, 2010, at 10:15 AM, dbkbali wrote:

From your extensive knowledge would there be any design or performance
advantage in refactoring so that the browser requests a pure html
partial only?

I am guessing here what Marnen’s intention was, but I believe he was
saying that the browser should not require a dynamic JavaScript to do
the magic. A static JavaScript, receiving dynamic data and / or html
from Rails should be able to manage your entire interaction without
trouble. Dynamic JS was the anti-pattern here, as far as I can tell.

Walter

Walter D. wrote in post #954576:

On Oct 15, 2010, at 10:15 AM, dbkbali wrote:

From your extensive knowledge would there be any design or performance
advantage in refactoring so that the browser requests a pure html
partial only?

I am guessing here what Marnen’s intention was, but I believe he was
saying that the browser should not require a dynamic JavaScript to do
the magic. A static JavaScript, receiving dynamic data and / or html
from Rails should be able to manage your entire interaction without
trouble. Dynamic JS was the anti-pattern here, as far as I can tell.

You are correct. I believe fairly strongly that, for reasons of
performance and maintainability, JavaScript files should in general be
static. It also seems to be the case that the (perceived) need for
dynamic JS is usually a symptom of a lurking design problem.

Walter

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]