ruby-on-rails - Ruby 参数传递

标签 ruby-on-rails ruby parameters

我目前有以下代码片段:

 <tbody>
  <% @requests.each do |request| %>
    <% @user = User.where(id: request[:student_id_id]).first %>
    <tr>
      <td><%= @user.last_name.titleize %></td>
      <td><%= @user.first_name.titleize %></td>
      <td><%= @user.preferred_name.titleize %></td>
      <td><%= render :partial => 'documents/resumelink' %></td>
      <td><%= mail_to @user.email.downcase %></td>
      <% if request.allowed_companies.nil? || request.allowed_companies.empty? %>
        <td> <a class="no_selected_companies_button">No Restrictions</a></td>
      <% else %>
        <% @allowed_request = AllowedRequest.where(professor_id_id: @current_user[:id], student_id_id: @user[:id]).first %>
        <td><%= link_to("View Companies", allowed_request_path(@allowed_request) , class: "selected_companies_button") %></td>
      <% end %>
    </tr>
  <% end %>
</tbody>

局部 View 。 documents/_resumelink 部分看起来像这样:

<% if (@document != nil) && (@document.resume_file_name != nil) %>
  <%= link_to 'View Resume', @document.resume.url %>
<% else %>
  <%= link_to 'No Resume available', '#'  %>
<% end %>

在 documents_controller 我有它所以它会 before_action set_document (无一异常(exception))。该定义看起来像:

def set_document(user=@current_user)
  @document = Document.find_by owner_user_id_id: user[:id]
end

因此,我希望能够做的是将@user 变量传递给我在渲染中的 set_document 函数:partial => 'documents/resumelink' 行在第一个 View 中并让它显示到简历的链接(如果有的话)。

有什么想法吗?

最佳答案

我认为您的问题比具体问题更具系统性;因此我想提供一些想法


Controller

当您使用 before_action 时, 它在 你的 Rails 操作运行之前运行

这意味着当您向 Rails 发送请求时,它会加载 before_action 过滤器,然后加载您请求 的相关操作。部分不构成此过程的一部分:

enter image description here

这意味着如果您想让各种变量可用于应用程序的不同部分,您需要了解它的工作方式如下:

Request -> Router -> Controller#Action -> View -> Partial

要使用适当的数据,您需要确保在上述过程的正确部分定义了它。您遇到的问题是您没有这样做——您如何才能使 @user 变量在 @user 之前可用于方法变量已定义?

现在这不是您的特定问题;但这将是您下次需要牢记的事情。


部分

如果您希望在 partial 中包含数据, 您将需要使用 locals render 方法的参数:

<%= render partial: "documents/resumelink", locals: { your_local_var: @value } %>

--

So, what I want to be able to do is pass the @user variable to my set_document function in the render

按照我上面的简单流程图,您的部分不会调用 set_document 方法,因为部分实际上只是您的 View 的扩展

您需要在 Controller 中设置@user 变量(我为您设置了ActiveRecord association),然后将其作为本地变量传递给您的部分:

#app/models/request.rb
Class Request < ActiveRecord::Base
   has_many :users, foreign_key: "student_id_id"
end

#app/models/user.rb
Class User < ActiveRecord::Base
   belongs_to :request, foreign_key: "student_id_id"
end

#app/controllers/documents_conroller.rb
Class DocumentsController < ApplicationController
   before_action :set_document

   def index
       @requests = Request.all
   end

   private

   def set_document(user=@current_user)
       @document = Document.find_by owner_user_id_id: user[:id]
   end

end

这将允许您调用以下内容:

#app/views/requests/index.html.erb
<% @requests.each do |request| %>
   <% user = request.users.first %>
   <%= render partial: "documents/resumelink", locals: {user: user} %>
<% end %>

关于ruby-on-rails - Ruby 参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25051939/

相关文章:

ruby-on-rails - 可以在 `table_name_prefix` 中指定架构吗?

javascript - 如何使用 moment.js 减去两次并将其与分钟数进行比较

jquery - 新定义函数中参数的命名空间

parameters - Go 如何查看产品类型

ruby-on-rails - 什么 Rubygem 可以轻松地向一部分用户发布新功能?

css - bootstrap 3.1.1 在外部单击时崩溃关闭

ruby-on-rails - "The behavior of ` attribute_changed? ` inside of after callbacks"基本`save`后

Ruby- CSV 合并字段值

javascript - 为什么作为函数参数传递的 Javascript 数组在顺序调用函数时可能会丢失其内容?

ruby-on-rails - Cucumber 不会接受 Capybara Minitest 规范断言