Rails - 将局部变量传递给partial

时间:2013-12-05 09:47:55

标签: ruby-on-rails ruby-on-rails-4

我有视图,它可以呈现我的应用的用户。我想制作部分,其中包含必要的字段,并传递给它本地变量

用户/索引

- @vk = VkontakteApi::Client.new
- fields = [:first_name, :last_name, :url, :photo_medium, :sex, :bdate]

- @vk.friends.get(uid: current_user.uid, fields: fields) do |friend|
  %td.span
    .centred
      = render 'shared/users', locals: { friend: friend }

共享/用户

- @vk = VkontakteApi::Client.new
- fields = [:first_name, :last_name, :url, :photo_medium, :sex, :bdate]

%span= image_tag friend.photo_medium
%span= friend.uid
%span= friend.name

但是,如果我尝试它有一个错误

undefined local variable or method `friend'

谢谢!如果您需要更多信息,请对其进行评论

1 个答案:

答案 0 :(得分:3)

这应该有效:

= render partial: 'shared/users', locals: { friend: friend }

或简写:

= render 'shared/users', friend: friend