登台环境的未定义方法,在本地工作正常

时间:2014-04-15 09:45:44

标签: ruby heroku ruby-on-rails-4

目前我的暂存环境存在一些问题。我在Heroku上有我的应用程序(分期和制作)。它在本地完美运行,但是当我将它推送到临时环境时,我一直在为category_id获取一个未定义的方法。

我重置了数据库,运行迁移并将种子放入无效状态。我能想到的唯一一件事就是我的产品型号出了问题,但是自从我上次将它推向生产以来,我还没有改变应用程序的这一部分(在那里使用当前版本可以正常工作)。

我的某次迁移是否有可能无法通过?这是我唯一能想到的。

产品型号:

class Product < ActiveRecord::Base
belongs_to :subcategory

mount_uploader :product_image, ProductImageUploader

validates :title, :description, presence: true
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: { with: %r{\.(gif|jpg|png)\Z}i, message: 'must be a URL for GIF, JPG or PNG image.'}
validates :subcategory_id, presence: true
end

产品控制器:

class ProductsController < ApplicationController
  skip_before_filter :authorize, only: [:show, :index]
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.order("title ASC")
    @categories = Category.all
    @subcategories = Subcategory.order("title ASC")
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
     if @product.save
       format.html { redirect_to @product, notice: 'Product was successfully created.' }
       format.json { render json: @product, status: :created, location: @product }
     else
       format.html { render action: 'new' }
       format.json { render json: @product.errors, status: :unprocessable_entity }
     end
   end
 end

 # PATCH/PUT /products/1
 # PATCH/PUT /products/1.json
 def update
   respond_to do |format|
    if @product.update(product_params)
      format.html { redirect_to @product, notice: 'Product was successfully updated.' }
      format.json { head :ok }
    else
      format.html { render action: 'edit' }
      format.json { render json: @product.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /products/1
# DELETE /products/1.json
def destroy
  @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end

 private
   # Use callbacks to share common setup or constraints between actions.
   def set_product
     @product = Product.find(params[:id])
   end

   # Never trust parameters from the scary internet, only allow the white list through.
   def product_params
     params.require(:product).permit(:title, :description, :image_url, :product_image, :subcategory_id, :category_id)
   end
 end

导致错误消息的视图:

=form_for(@product) do |f|
-if @product.errors.any?
    #error_explanation
        %h2
            =pluralize(@product.errors.count, "error")
            prohibited this product from being saved:

        %ul
        -@product.errors.full_messages.each do |msg|
            %li
                =msg
        %br

.field
    =f.label :title
    %br
    =f.text_field :title, size: 100
    %br

.field
    =f.label :description
    %br
    =f.text_area :description, cols: 100, rows: 10
    %br

.field
    =f.label :product_image
    =f.file_field :product_image

.field
    =f.label :category_id
    %br
    =f.collection_select(:category_id, Category.all, :id, :title)
    %br

.field
    =f.label :subcategory_id
    %br
    // =f.collection_select(:subcategory_id, Subcategory.all, :id, :title)
    %select{:id => "product_subcategory_id", :name => "product[subcategory_id]", :disabled => "disabled"}
        %option
            Select a category first...
    %br

.actions
    %br
    =f.submit
    %br
    %br

:javascript
    $(document).ready(function(){
        $("select#product_category_id").change(function(e) {
            var val = $(this).val();

            var subCatSelect = $("select#product_subcategory_id");
            subCatSelect.empty();

            subCatSelect.append("<option>Loading...</option>");

            $.get("/subcategories.json?category="+val)
            .done(function(response) {
                subCatSelect.empty();
                if (response.length > 0) {
                    $.each(response, function(k,v) {
                        subCatSelect.append("<option id='"+v.id+"'>"+v.title+"</option>");
                        subCatSelect.removeAttr("disabled");
                    });
                } else {
                    subCatSelect.attr("disabled", "disabled");
                    subCatSelect.append("<option>No Subcategories</option>");
                }
            });
        });
    });

错误信息:

ActionView::Template::Error (undefined method `category_id' for #<Product:0x007f64ab47c1d0>):
30:   .field
31:     =f.label :category_id
32:     %br
33:     =f.collection_select(:category_id, Category.all, :id, :title)

app/views/products/_form.html.haml:33:in `block in _app_views_products__form_html_haml__3508934121535598535_70035173692040'
app/views/products/_form.html.haml:1:in `_app_views_products__form_html_haml__3508934121535598535_70035173692040'
app/views/products/new.html.haml:7:in `_app_views_products_new_html_haml__3953831312052620477_70035173605140'

2 个答案:

答案 0 :(得分:0)

资产管道中的命名约定可能存在问题。

尝试预编译您的资产管道,以便推送到Heroku:

bundle exec rake assets:precompile RAILS_ENV=production
git commit -a -m "Prempile assets for release"
git push
git push heroku master

以下是来自Heroku的更多信息:

https://devcenter.heroku.com/articles/rails-asset-pipeline

不确定这是否会解决问题,但尝试它非常容易。我发现这解决了从开发/测试到生产的大部分问题。

希望这有帮助!

答案 1 :(得分:0)

我不太确定发生了什么,但是当我清除heroku数据库时,重新运行我的迁移和种子它决定工作......我不知道发生了什么。