【问题标题】:Render a partial that uses helper methods to determine view渲染一个使用辅助方法来确定视图的部分
【发布时间】:2020-06-03 01:13:14
【问题描述】:

目前的工作方式:

在我的 BookingsController#Index 方法中,我有这个

def index
    @user = current_user
    if @user.user_type == 'host'
      @bookings = Booking.where(host_id:@user.id, status: "#{params[:status]}")
    else @user.user_type == 'cleaner'
      @bookings = Booking.where(cleaner_id:@user.id, status: "#{params[:status]}")
    end
  end

这会设置当前用户并根据 user_id 和参数确定要显示的 @bookings

在视图中,在我的index.html.erb 文件中,我呈现了我在booking#index 中定义的预订<%= render @bookings %>。我有一个被渲染的_booking.html.erb 部分...在_booking.html.erb 部分中,我能够成功返回预订详细信息,例如,

<div id='booking-time'><b>Unit Name:</b> <%= booking.unit.name %></div>

将返回单位名称。它识别局部变量booking 和属于它的字段。

我想重构:

我想重构它并在我的index.html.erb 文件中呈现部分&lt;%= render 'bookings/booking' %&gt;。此部分将呈现不同的辅助方法,每个方法都路由到不同的部分。

目前我的_booking.html.erb 文件有一堆更适合帮助者的条件逻辑......我尝试了很多不同的方法来完成这个无济于事,通常是由于未定义的变量......什么是优雅的方式来完成这个?

我的尝试:

这是我的 bookings_helper.rb` 文件中的一个方法:

  def incoming_to_cleaner_partial_path(bookings)
    return unless booking.status=='pending' && current_user.user_type =='cleaner' && booking.requested_by_id != current_user.id
      'bookings/booking_requests/incoming_to_cleaner'
  end

我为这些方法添加了逻辑以确定要渲染的部分。

在我修改后的_booking.html.erb 文件中,我调用这些方法,我将@bookings 实例变量参数传递给它们。

<%= render incoming_to_cleaner_partial_path(@bookings) %>

我也试过这个不带参数的..都不行..

最后,我尝试在辅助方法中调用方法determine_bookings_to_show。此方法包含来自控制器的相同逻辑...

def incoming_to_cleaner_partial_path
    determine_bookings_to_show
    return unless booking.status=='pending' && current_user.user_type =='cleaner' && booking.requested_by_id != current_user.id
      'bookings/booking_requests/incoming_to_cleaner'
  end

private

def determine_bookings_to_show
    @user = current_user
    if @user.user_type == 'host'
      @bookings = Booking.where(host_id:@user.id, status: "#{params[:status]}")
    else @user.user_type == 'cleaner'
      @bookings = Booking.where(cleaner_id:@user.id, status: "#{params[:status]}")
    end
  end

我的主要问题是我在使用这些部分时遇到了范围问题......

【问题讨论】:

  • “我尝试了很多不同的方法..”你能展示一些你尝试过的方法吗?
  • 我用我尝试过的内容更新了我的帖子....不想让帖子变得比它必须的更混乱;以防我的解决方案完全被误导了。

标签: ruby-on-rails controller refactoring helper partials


【解决方案1】:

..在我的索引中[我想]渲染不同的部分[取决于预订的属性]..

我过去曾为此使用过render(locals:)

# index.html.erb
@bookings.each do |b|
  render_booking(b)
end

# bookings_helper.rb
def render_booking(booking)
  if booking.status == 'pending'
    render partial: 'path/to/bookings/_pending', locals: { booking: booking }
  elsif booking.foobar?
    render partial: 'path/to/bookings/_foobar', locals: { booking: booking }
  else
    # etc.
  end
end

这样的条件可能会变得复杂,但在此之前,我认为这是对辅助方法的适当、实际使用。

# bookings/_pending.html.erb
<h1>Pending Booking <%= booking.id %></h1>

您可能需要传递其他locals:。我会避免在您的部分中使用任何实例变量。

【讨论】:

  • 这个帮助程序成功地将用户路由到正确的页面(待处理或已批准的页面),但是我最初的问题涉及这些页面上显示的内容 - 例如,如果我导航到待处理页面,应该有一个“用户发出的待定预订”(助手)和“用户传入的待定预订”(也是一个助手)的索引 - 我目前有一个大的 html 文件来处理显示这些预订类型的逻辑,但我想要将该 html 文件拆分为更小的部分并通过辅助方法调用它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 2011-05-10
  • 1970-01-01
相关资源
最近更新 更多