开发者

ruby Online Quote app

开发者 https://www.devze.com 2022-12-12 06:58 出处:网络
I am trying to build an online quote app for roofing.I have a controller called Cost that has squarecost and squaresell.There are other models and controllers for gable, shingle, ridgevent and more wi

I am trying to build an online quote app for roofing. I have a controller called Cost that has squarecost and squaresell. There are other models and controllers for gable, shingle, ridgevent and more with similar fields. These additional models and controllers are the add ons for the quote.

Ideally, sales person would create a new quote. Enter customer info and it would ask for length "L", width "W" and pitch "P". There would be check boxes for the add ons. When sales clicks "continue" they are taken to the new page (addon.html.erb) with the L, W and P which is passed along and开发者_Python百科 all the add ons that where checked on from the page before (in render partials).

Quote holds L, W and P, along other things like customer name, email, etc. Inside of quotes I have a function called getsquare and it gets the area from (LxW)xP, where P == 1.03 (this number depends on how much of a pitch a roof has.) The reason Quote stores L, W and P is because if a material like Roof Vent is checked, then it gets the L from the roof to calculate how much material is needed.

How can I get the form to pass the L, W and P from the first page on to the second where it now displays all the add ons that where selected?

Also, I have having problems doing math functions. I have the vision on what I want to do, but can't seem to get it done. For example, (@length * @width) * @pitch = "currentsquare". Currentsquare is holder because if Gable is checked then I need to add the Gable squares to the total (Gable also has "(L * W)*P = gablesquare", and at the end of the form I have a "currentsquare + gablesquare = totalsquares" and totalsquares is a field in Quotes. Where can I find the wiki for math functions and helpers for this? All I find is helpers like Math.floor, Math.log, etc.

I know a lot of this can be done with Java but, trying to avoid java so it can also be used with cell phones.


I have a controller called Cost that has squarecost and squaresell. There are other models and controllers for gable, shingle, ridgevent and more with similar fields.

I am not familiar with the roofing vocabulary, so I'll make some assumptions.

Assumption 1: You meant you had a Cost "model" instead of a controller. Controllers usually have actions like "new" "create" "update", "show" or "delete".

Assumption 2: You have "several types of roof-related jobs" and you want to issue a cost depending on the width, height, pitch and type. I'm also assuming that the calculations are very different depending on the type.

If you want to save your W, L and P on your database, you could use single table inheritance - Instead of having all your "types of roof" models inheriting from ActiveRecord::Base, you would have something like this:

#app/models/roof.rb
class Roof < ActiveRecord::Base
  validates_presence_of width, height, pitch
  validates_numericallity_of width, height, pitch
  ... (general calculations with width, height, pitch - maybe squarecost & squaresell )
end

Then your Gable model would be something like this:

#app/models/gable.rb
class Gable < Roof #instead of ActiveRecord::Base
  ... (gable-related validations and calculations)

Your Roof table would need at least colums for width, height, pitch and type ("type of roof"). Plus all the additional fields of all the types - for example, if Gable has a field called "material_id" you would have to include it on the Roof table, too.

I think you need that solution. But, if you DON'T want to store width, height and pitch on your database, you would have to create a module in your lib/ directory, and implements validations yourself. Something like this:

#lib/roof.rb
module Roof
  attr_accessor :width, :height, :pitch
  def validate_dimensions
    ... (add errors to the model if with, height or pitch are missing or non-numeric)
  end
  ... (again, general calculations here)
end

Then on your Gable model:

#app/models/gable.rb
require roof.rb #you could also include this on an initializer
class Gable < ActiveRecord::Base
  include Roof
  ... (Gable-specific validations)
  validate :validate_dimensions #this must be put after all other validations
  ... (Gable-specific stuff)
end

In both cases, the Gable controller & view would be the same:

#app/controllers/gable_controller.rb
class GableController < ApplicationController
  def new
    @gable = Gable.new()
    ... (usual scaffolding-generated stuff for new, create, show, etc.)
end

#app/views/gable/new.html.erb
... (header, title, etc)
<% form_for @gable do |f| %>
  <%= f.text_field :width %>
  <%= f.text_field :height %>
  <%= f.text_field :pitch %>
  ... (gable-specific fields)
<% end %>

This form will throw the usual errors if with, height and pitch are missing.

I hope this helps you - I didn't actually run the code, so there might be typos.


The form fields get passed in the params hash, which is accessible from the controller

Update: Search form

  def search
    @find = params[:find] ? true : false
    @user_filter = params[:user_filter] ? params[:user_filter].strip : ''
    @dj_filter = params[:dj_filter] ? params[:dj_filter].strip : ''
    @date_from = params[:date_from] ? Date.civil(params[:date_from][:year].to_i, params[:date_from][:month].to_i, params[:date_from][:day].to_i) : Time.now - 1.year
    @date_to = params[:date_to] ? Date.civil(params[:date_to][:year].to_i, params[:date_to][:month].to_i, params[:date_to][:day].to_i) : Time.now + 1.day
    @requests = []
    if @find
      users = User.find :all, :conditions => ["login LIKE ?", "%" + @user_filter.downcase + "%"]
      djs = User.find :all, :conditions => ["login LIKE ?", "%" + @dj_filter.downcase + "%"]
      conditions = (@user_filter.empty? ? "" : "(user_id in (#{users.map {|u| u.id}.join(",")})) and ") +
              (@dj_filter.empty? ? "" : "(dj_id in (#{djs.map {|u| u.id}.join(",")})) and ")
      @requests = Request.find :all,
                :conditions => [conditions + "(created_at between ? and ?)", @date_from, @date_to],
                :order => "created_at DESC",
                :limit => 100
    end
    respond_to do |format|
      format.html
      format.xml
    end
  end

Relevant parts are:

params[:user_filter], params[:dj_filter], params[:date_from], etc.. 
0

精彩评论

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