开发者

How to implement an AJAX price compare feature in Rails?

I want to create an AJAX price compare feature. I don't really know how it should work.

A user should be able to select a company and a loading icon will appear. The list of companies will be ordered by price and show how much the user can save.

Here is my form:

<select style=" margin-left:10px;width:370px;float: left;
    margin-top: 10px;"name="konkurrancer[form]" id="konkurrancer_form" class="select optional">
<option value="Andet">Company 1</option>
<option value="Andet">Company 2</option>
<option value="Andet">Company 3</op开发者_如何学编程tion>
</select>

And I have a table with all the company names and prices.

My table:

name    price   id
comany 1    120 1
comany 2    80  2
comany 3    60  3
comany 4    40  4

Example: A user selects company 1, that has the price $120. He will see all the companies ordered and determine how much he can save. In this case, he would see company 4 first and the information that he can save (120-40) 80$.


  1. add a route to config/routes.rb to support your new controller action.
  2. add the javascript that fires off the ajax
  3. add the action handler to your controller
  4. insert your above described business logic
  5. return your data using the json version of render
  6. handle the return data on the javascript side.

I think if you use something like jQuery, a lot of this becomes easier because it appears you'll want to do some basic DOM manipulation as a part of (6).

I can't really help you sort out the algorithm in (4) but I can answer more directed questions about the other steps if you have any.

Edit: Sort the companies by price, low to high, then when you subtract the current price from each one you'll end up with the greatest savings first.

As for your controller, I guess you'll need to pass in the current price, and the id of the company selected...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜