如何从另一个控制器渲染视图模板并传递是一个局部变量?

时间:2015-09-27 21:17:58

标签: ruby-on-rails haml

在Ruby on Rails中,如何从另一个控制器的命名空间渲染部分并将对象作为局部变量传递?

例如,在views/posts目录中的视图模板(由PostsController管理)中,我需要从views/polls目录中呈现部分内容。

我尝试过以下方法:

.item_index  
  = f.fields_for :poll_items do |poll|
    = render :partial => 'polls/poll_item_fields', f: poll
  .links
    = link_to_add_association 'Add more', f, :poll_items, render_options: {class: 'links'}

我错了:

Showing /home/ubuntu/workspace/app/views/polls/_poll_item_fields.html.haml where line #3 raised:
undefined local variable or method `f' for #<#<Class:0x0000000c01ec38>:0x0000000f56b170>**

修改

所有完整观点:

文章/ new.html.haml

%h1= title "New post"
= simple_form_for [@group, @post] do |f|
  = f.error_messages header_message: nil
  = render 'form', f: f

  .form-actions
    %p= attach_links
    = f.button :submit, 'Опубликовать пост', class: 'btn-bg'
    - if paid?
      = f.button :submit, 'сохранить черновик', class: 'btn-inverse', name: 'draft'
    = button_tag 'Добавить опрос', type: 'button', id: 'poll-btn', class: 'btn btn-bg'
    %p
      Вернуться в группу:
      = link_to @group, @group

文章/ _form.html.haml

- cat = Post::CATEGORIES.map { |c| [t(c, scope: :post_categories), c] }
= f.input :category, collection: cat
= f.input :subject, input_html: { class: 'input-block-level' }
= f.input :body, input_html: { rows: 5, class: 'ctrlenter input-block-level expanding' }
= link_to "#", class: 'smiley', role: 'add_smiley', tabidex: 4 do
  %i.icon.icon-smile
  смайлы
= smiles_helper '#post_body'

.form-horizontal
  = f.input :tag_list, input_html: { class: 'input-block-level' }
  - if (current_user.paid? && current_user.moderated_group_ids.include?(@group.id)) || moderator?
    = f.input :comments_disabled, inline_label: true, label: false
.attachments
  = f.simple_fields_for :attachments do |af|
    = render "attachments/#{af.object.asset_type.underscore}", :f => af
.poll{style: "display: none;"}
  %h1 "Новый опрос"
  = f.simple_fields_for :poll do |poll|
    = render "polls/poll_fields", f: poll

轮询/ _poll_fields.html.haml

= f.error_messages header_message: nil
= f.input :question, input_html: { class: 'input-block-level' }
= f.input :results_hidden, as: :boolean, inline_label: 'Скрыть результаты до окончания опроса', label: false
= f.input :from_date, as: :datetime, input_html: { class: 'poll_date' }
= f.input :to_date, as: :datetime, input_html: { class: 'poll_date' }
%h3#poll-items Варианты ответа (не больше пяти)
.item_index  
  = f.fields_for :poll_items do |poll_item|
    = render partial: 'polls/poll_item_fields', locals: {f: poll_item}
  .links
    = link_to_add_association 'Добавить еще вариант', f, :poll_items, render_options: {class: 'links'}

轮询/ poll_items_fields

.poll_row
  .poll_item
    = f.input :answer, input_html: { class: 'ctrlenter expanding' }, label: false, placeholder: 'Введите вариант ответа'
    = link_to_remove_association "удалить", f, { wrapper_class: 'poll_item' }

使用= render partial: 'polls/poll_item_fields', locals: { f: poll }时,显示错误: howing /home/ubuntu/workspace/app/views/polls/_poll_fields.html.haml第11行引发:howing / home / ubuntu / workspace /第11行引出的app / views / polls / _poll_fields.html.haml: 缺少部分帖子/ poll_item_fields

第11行是= link_to_add_association 'Add more', f, :poll_items, render_options: {class: 'links'}

2 个答案:

答案 0 :(得分:0)

通过render将变量传递给部分时,需要将它们包装在locals键中:

= render partial: 'polls/poll_item_fields', locals: { f: poll }

有关详细信息,请参阅Layouts and Rendering guide

答案 1 :(得分:0)

var currentDocketQuery = from d in db.Dockets where d.OccurrenceStartDate >= datetime && d.BuildingDetail.CampusName == Campus select new CampusBuildingDocketTypeViewModel() { BuildingCode = d.BuildingDetail.BuildingCode, BuildingName = d.BuildingDetail.BuildingName, //BuildingPolygons = d.BuildingDetail.BuildingPolygons, DocketTypes = d.DocketTypes.Select(e => new DocketTypeViewModel() { Category = e.Category, SubCategory = e.SubCategory, ShortDescription = e.ShortDescription }) }; 方法有两种形式,可让您以不同方式传递本地人。

快捷方式版本将接受render

Hash

但是,您正在使用 long 版本。如果您使用= render 'polls/poll_item_fields', f: poll 密钥,则还需要使用partial密钥:

locals

这不仅仅是风格问题,而是因为红宝石如何处理方法论点。

快捷方式版本中,第一个参数是= render partial: 'polls/poll_item_fields', locals: { f: poll } ,第二个参数是String。 Rails会知道Hash将包含本地人 但是,在第二个版本中,第一个(也是唯一的)参数是Hash。没有Hash键,并且由于Ruby的语法允许省略括号和花括号,它将被翻译为:

:locals

这会让Rails有点困惑,因为它无法将本地人与传递给该方法的其他键控选项隔离开来。

最后,它仍然是一个风格问题:方法签名就是这样设计的。我希望我的解释让事情更清楚一点!