我每次去新的'时都需要刷新页面。形成

时间:2015-12-03 18:17:03

标签: ruby-on-rails ruby

我使用模型和html5创建了一个包含客户端和服务器端验证的表单,如代码中所示。一切都很好,但每次点击链接" New Ingredient"我填写下面的表格并不起作用。此外,它只有在刷新页面时才有效。 为什么它一开始不工作?

ingredient.rb

class Ingredient < ActiveRecord::Base 
  has_many :meal_ingredients 
  has_many :meals, through: :meal_ingredient 
  before_save { self.name = name.capitalize } 

  validates :name, presence: true, length: { minimum: 2, maximum: 25 }, uniqueness: true 
  validates :unit, presence: true validates :carb, :prot, :fat, numericality: true, 
                   length: { minimum: 1, maximum: 5 } 
end

/new.html.erb

<div class = "row">
  <div class= "col-md-10 col-md-offset-1 well">
    <table class= "table">
      <%= form_for @ingredient do |f| %>
        <thead>
          <tr>
            <td>
              <%= f.label "Nome" %>
            </td>
            <td>
              <%= f.label "Unidade" %>
            </td>
            <td>
              <%= f.label "Carbo" %>
            </td>
            <td>
              <%= f.label "Prot" %>
            </td>
            <td>
              <%= f.label "Gordura" %>
            </td>
            <td>
              <%= f.label "kcal" %>
            </td>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td scope="row" class="col-md-5">
              <%= f.text_field :name, required: true, class: "form-control" %>
            </td>
            <td class="col-md-3">
              <%= f.text_field :unit, required: true, class: "form-control" %>
            </td>
            <td class="col-md-1">
              <%= f.text_field :carb, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control" %>
            </td>
            <td class="col-md-1">
              <%= f.text_field :prot, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control" %>
            </td>
            <td class="col-md-1">
              <%= f.text_field :fat, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control" %>
            </td>
            <td class="col-md-1">
            </td>
          </tr>
        </tbody>
        <tfooter>
          <tr>
            <td class="pull-right">
              <%= f.submit @ingredient.new_record? ? 'Create Ingredient' : 'Edit Ingredient', class: "btn btn-success" %>
            </td>
            <td>
              <%= link_to "Voltar", ingredients_path, class: "btn btn-warning" %>
            </td>
          </tr>
        </tfooter>
      <% end %>
    </table>
  </div>
</div>

/ingredients_controller.rb

  def new
    @ingredient = Ingredient.new
  end

  def create
    @ingredient = Ingredient.new(ingredient_params)
    @ingredient.kcal = @ingredient.carb * 4 + @ingredient.prot * 4 + @ingredient.fat * 9
    if @ingredient.save

      flash[:success] = "Ingrediente criado com sucesso"
      redirect_to ingredients_path
    else
      render 'new'
    end
  end

1 个答案:

答案 0 :(得分:0)

这可能是由Turbolinks引起的,它通过刷新DOM的一小部分而不重新加载整个页面来“加快”加载时间。因此,您需要使用占据部分刷新的事件来触发您的脚本:

尝试将JS包装在其中:

X11-xft-0.3.1: canonicalizePath: does not exist (No such file or directory)

这是在CoffeeScript中:

var ready;

ready = function() {
  # Your code here
};

$(document).ready(ready);
$(document).on("page:load", ready);

您还可以从“新成分”链接中禁用Turbolinks,这将导致重新加载整个表单页面:

ready = ->

  # Your code here

$(document).ready ready
$(document).on "page:load", ready