有没有办法在保存之前引用Rails ActiveRecord对象?

时间:2011-02-16 19:28:59

标签: jquery ruby-on-rails-3

我正在尝试根据用户输入更新表单上的div(使用jQuery)。在edit上工作正常,因为object.id存在,所以我可以将它传递给jQuery更新的控制器操作。在new中还没有object.id,那么如何才能在控制器操作中找到正确的对象? CONTROLLER ...

  def update_category_select
    @organization = Organization.find(params[:id])
    categories = Category.where(:type => params[:master_type]).order(:name)
      render :partial => "categories", :locals => {:collection => categories, :organization => @organization}
  end

的application.js

$(document).ready(function() { 
  $('#master_type').change(function () {
    var master_type = $('select#master_type :selected').val();
    var id = $('#organization_id').val();
    jQuery.get('/organizations/update_category_select/'+ id + '?master_type='+ master_type, function(data){
      $("#categories").html(data);
    })
    return false;
  });
});

提前感谢任何建议!

1 个答案:

答案 0 :(得分:0)

以防万一其他人在寻找。 Ryan Bates今天通过Twitter与我分享了这个......

“@ patrick_connor基本上你不能。你必须先保存它或传递所有新属性。”