首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails将局部变量从视图传递到js、控制器,然后返回到表单

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

Stack Overflow用户
提问于 2014-01-31 14:30:09
回答 1查看 194关注 0票数 1

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

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

代码语言:javascript
复制
NameError at /cus_departments
undefined local variable or method `f' for #<#<Class:0x000001078cbf88>:0x0000010782a138>

这是我的视图文件

new.html.haml

代码语言:javascript
复制
.page-content
  .page-header
    %h1 New agreement
  .row
    .col-xs-12
      = render :partial => 'form'
.actions
  = link_to 'Back', agreements_path

_form.html.haml

代码语言:javascript
复制
= 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

代码语言:javascript
复制
$ ->
  $('.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

代码语言:javascript
复制
def cus_departments

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

    render :partial => 'departments'
end

_departments.html.haml

代码语言:javascript
复制
= collection_select(:department, :customer_id, @departments, :id, :department, :prompt => "Select a department")

有没有人能帮我一下?

EN

回答 1

Stack Overflow用户

发布于 2014-01-31 17:20:09

您提供的内容有点令人困惑,但据我所知,我认为问题在于将检索到的值加载到选择框中。用户远程javascript或rjs这些都是强大的,完美的这种类型的需求。如果你希望,我将发送一些代码样本。:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21473839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档