【问题标题】:is there a way to not have view for an action in rails?有没有办法看不到rails中的动作?
【发布时间】:2010-02-14 06:19:48
【问题描述】:

在 rails + jQuery 中,当下拉框中的选项发生更改时,我试图向服务器发出 get 请求。我使用以下 jquery 代码:

$("#product_prod_name").change(function(){
    $.get("/page/volume_or_quant", "id="+$(this).val(), function(result) {
        alert(result)
    })
})

现在出现错误:Missing template pages/volume_or_quant.erb in view path app/views

如您所见...我不需要创建视图。我只是想从我的行动中得到一些回应。我的操作如下所示:

def volume_or_quant
    @product = Product.find(params[:id])
    @product.volume     
end

在这里做什么最好?

【问题讨论】:

    标签: jquery ruby-on-rails


    【解决方案1】:
    def volume_or_quant
        product = Product.find(params[:id])
        render :text => product.volume      
    end
    

    【讨论】:

      【解决方案2】:
      def volume_or_quant
          @product = Product.find(params[:id])
          @product.volume    
      
          # tell rails to ignore the view
          render :layout => false
      end
      

      应该为您解决问题。

      【讨论】:

      • 这只会忽略布局,但仍然需要视图。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      相关资源
      最近更新 更多