繁体   English   中英

Rails将局部变量从视图传递到js,控制器,然后返回到表单

[英]Rails passing local variable from view to js, controller and then back to form

我正在从下拉菜单中选择一个客户,根据选择,它应该在下一个下拉菜单中显示部门。

但是它给出了未定义的局部变量或方法f

NameError at /cus_departments
undefined local variable or method `f' for #<#<Class:0x000001078cbf88>:0x0000010782a138>

这是我的查看文件

new.html.haml

.page-content
  .page-header
    %h1 New agreement
  .row
    .col-xs-12
      = render :partial => 'form'
.actions
  = link_to 'Back', agreements_path

_form.html.haml

= form_for @agreement, :html => {:class => 'form-horizontal', :role => 'form'} do |f|
  .form-group
    %label.col-sm-3.control-label.no-padding-right.form-field-2
      = f.label :agreement_customer, 'Agreement customer'
  .col-sm-9
    #form-field-2.col-xs-10.col-sm-5.input.movies
      = f.select :agreement_customer, options_from_collection_for_select(Customer.all, :id, :customer_name), prompt: true
  .space-4
  .form-group
    %label.col-sm-3.control-label.no-padding-right.form-field-2
      = f.label :agreement_customer_dept, 'Customer department'
    .col-sm-9
      #form-field-2.col-xs-10.col-sm-5.input.characters
        %select{:class => 'form-control'}
          %option
  .space-4

agreement.js.coffee

$ ->
  $('.input.movies select').change ->
    url = "/cus_departments?customer_id=" + $(this).val() # get the selected value from the drop-down
    $('.input.characters select').load(url) # load the response from the url into the specified element(s)

agreements_controller.rb

def cus_departments

@customer = Customer.find_by_id(params[:customer_id])
@departments = @customer.departments

    render :partial => 'departments'
end

_departments.html.haml

= collection_select(:department, :customer_id, @departments, :id, :department, :prompt => "Select a department")

有人可以帮我吗?

您提供的内容有点混乱,但是据我了解,我认为问题在于加载检索到的值以选择框。 用户远程javascript或rjs这些功能强大且完美,可满足此类需求。 如果您愿意,我将发送一些代码示例。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM