如何在另一个MVC中使用表单数据?

时间:2016-02-24 06:38:38

标签: ruby-on-rails

我刚开始学习rails和ruby,但已经卡住了。

我创建了一个模型,一个视图文件和一个控制器,其形式允许用户输入一些数据。现在我正在研究另一个MVC,它使用这些数据进行各种计算并返回结果。我的问题是:我根本不理解如何将用户输入的数据输入到进行计算的模型中。我知道这是一个非常初学者的问题,但好吧,看起来我不能单独解决它。

参赛作品型号:entry.rb

class Entry
    include ActiveModel::Model
    attr_accessor :icao, :string
    attr_accessor :elevation, :integer
    attr_accessor :qnh, :integer
    attr_accessor :temperatur, :integer
    attr_accessor :winddirection, :integer
    attr_accessor :windspeed, :integer
    attr_accessor :basedistance, :integer
    validates_presence_of :icao
    validates_presence_of :elevation
    validates_presence_of :qnh
    validates_presence_of :temperatur
    validates_presence_of :winddirection
    validates_presence_of :windspeed
    validates_presence_of :basedistance
end

参赛作品视图:new.html.erb

<% content_for :title do %>Flytical<% end %>
<h3>Airfield Conditions</h3>
<div class="form">
  <%= simple_form_for @entry do |form| %>
    <%= form.error_notification %>
    <%= form.input :icao, label: 'Airfield ICAO', input_html: { maxlength: 4 }, autofocus: true%> 
    <%= form.input :elevation, label: 'Airfield Elevation', input_html: { maxlength: 4 }%> 
    <%= form.input :qnh, label: 'QNH', input_html: { maxlength: 4 }%> 
    <%= form.input :temperatur, label: 'Temperatur', input_html: { maxlength: 3 }%> 
    <%= form.input :winddirection, label: 'Wind Direction', input_html: { maxlength: 3 }%> 
    <%= form.input :windspeed, label: 'Wind Speed', input_html: { maxlength: 3 }%> 
    <%= form.input :basedistance, label: 'Base Takeoff Distance at MSL', input_html: { maxlength: 4 }%> 
    <%= form.button :submit, 'Submit', class: 'submit' %>
  <% end %>
</div>

条目控制器:entries_controller.rb

class EntriesController < ApplicationController

  def new
    @entry = Entry.new
  end

  def create
    @entry = Entry.new(secure_params)
    if @entry.valid?
      flash[:notice] = "Entries for #{@entry.icao} received."
      render :new
    else
      render :new
    end
  end

  private

  def secure_params
    params.require(:entry).permit(:icao, :elevation, :qnh, :temperatur, :winddirection, :windspeed, :basedistance)
  end

end

现在进行计算。这是计算模型:calc.rb 您看到我尝试使用@ entries.xxx获取数据,但显然无法正常工作

class Calc
    include ActiveModel::Model
    def pressalt
        if @entry.qnh < 1013
            pressalt = @entry.elevation + (1013 - @entry.qnh) * 30
        else
            pressalt = @entry.elevation
        end
    end
    def densalt
        if @entry.temperatur > 15
            densalt = pressalt + (((120 * ((@entry.temperatur - (15 - @entry.elevation * 2 /1000)))  
        else
            densalt = pressalt
        end
    end
end

计算视图:new.html.erb

<% content_for :title do %>Flytical<% end %>
<h3>Results</h3>
<p> Test result <%= @densalt %> </p>

Calc控制器:calcs_controller.rb

class CalcsController < ApplicationController

  def create
    @calc = Calc.new
    end
end

这些是我的路线:

Rails.application.routes.draw do
  resources :entries, only: [:new, :create]
  resources :calcs, only: [:new, :create]
  root to: 'visitors#new'
end

老实说,我的印象是我遗漏了一些基本的,但主要的观点 - 我希望我将通过这个项目来学习它。

0 个答案:

没有答案