开发者

Rails based simple AJAX calculation fxn seems harder than it should be

开发者 https://www.devze.com 2023-03-14 14:30 出处:网络
I feel like there is a large step from where Rails tutorials end and where I can actually use it to do what I need.This is manifesting itself in this relatively simple case:

I feel like there is a large step from where Rails tutorials end and where I can actually use it to do what I need. This is manifesting itself in this relatively simple case:

I want a page that takes inputs and calculates a volume. Ideally, this would do it with AJAX because it always irritates me when a simple page makes me reload it.

I've been doing this in IRB with:

radius = 25; length = 0.15;( radius**2) * Math::PI * length *1e-3 
# radius in microns, length in meter开发者_Go百科s, answer in microliters

But the confusion comes in just about here. I understand that I must route a page, I understand that my controller is responsible for directing to the correct response, but where should my calculation occur? How do I capture the responses for my form?

I just don't seem to understand how to integrate all these things...

Here's my view (which renders, but from which I can't seem to feed the information in or out:

<h2>Here I'll calculate the dead volume of a piece of tubing for you.</h2>

<%= form_tag url_for(:method => :get), :remote => true, :id => 'calculation_entry' do %>
  <div id="internal_diameter">
    <label for="diameter">Enter the internal diameter:</label>
    <%= text_field_tag :diameter %>
    <label for="diameter_unit"></label>
    <%= select_tag :diameter_unit, options_for_select({
      "Microns" => 'micrometers', 
      'inches' => 'inches'
    })  %>  
  </div>
  <div id="length">
    <label for="length">Enter the length of tubing:</label>
    <%= text_field_tag :length %>
    <label for="length_unit"></label>
    <%= select_tag :length_unit, options_for_select({
      "meters" => 'meters', 
      "inches" => 'inches'
    })  %>
  </div>
  <%= submit_tag "Calculate!" %>
<% end %>

<div class="output">
  <span>The volume of the tubing parameters entered is:</span> 
 <%= @object %>
</div>

And here is the controller I've built:

class PagesController < ApplicationController

  def index
  end

  def dead_volume
    @object = Object.new
  end

  def calculate
    @object = params.inspect
  end
end


Here is an example of how I implemented autocomplete functionality with jQuery UI & Ajax. It may help you to determine how to do this for your own application's needs.

Note: This is was done under Rails 3.0.X. There are probably other/better ways to do this.

View/Layout:

<div id="search">
  <%= form_tag('/<my_controller>/search', :method => :get, :id => "#search-text") do %>
    <input type="text" name="search" value="" id="search-text" placeholder="Search"> 
    <input type="submit" value="Go" class="button">
  <% end %>
</div>

JavaScript (note: uses jQuery UI):

$(document).ready(function() {

  $("#search-text").autocomplete({
    source: function(request, response) {
      $.getJSON("/<my_controller>/autocomplete", {
        search: request.term
        },
      response);
    },
    minLength: 2,
    select: function(event, ui) {
      this.value = ui.item.value;
      return false;
    },
  });

});

Route:

match '/<my_controller>/autocomplete' => '<my_controller>#autocomplete', :via => :get

Controller:

respond_to :json, :only => [:autocomplete]

def autocomplete
  @model = Model.autocomplete(params[:search])
  render(:json => @model)
end

I hope this helps you get on your way.

0

精彩评论

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