Rails 5 - 如何编写视图助手
Rails 5 - how to write a view helper
我正在尝试学习如何在我的 Rails 5 应用程序中使用助手。
我有一个 org_requests 模型,它具有保存执行各种步骤的日期的属性。我正在尝试在保存相关控制器操作时使用控制器更新这些属性。
例如,我有一个名为 :approved_at 的属性。在我的控制器中,我有一个名为 approved with:
的动作
def approved
@org_request = current_user.organisation.org_requests.find(params[:id])
if @org_request.state_machine.transition_to!(:approved)
@org_request.update_attribute(approved_at: Time.zone.now)
flash[:notice] = "You've added this member."
redirect_to org_requests_path
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to :index
end
end
我认为这一行:
@org_request.update_attribute(approved_at: Time.zone.now)
现在应该保存时间,当批准的动作被保存时。
在我看来,我正在尝试渲染相关操作的执行时间。为此,我正在尝试编写:
<% @org_requests.each do |org_req| %>
<%= text_for_time_of_status_change(org_req.current_state) %>
然后我有一个助手有:
def text_for_time_of_status_change(current_state)
case current_state
when 'requested'
org_request.requested_at.try(:strftime, ' %l:%M %e %B %Y')
when 'approved'
org_request.approved_at.try(:strftime, ' %l:%M %e %B %Y')
when 'rejected'
org_request.rejected_at.try(:strftime, ' %l:%M %e %B %Y')
when 'removed'
org_request.removed_at.try(:strftime, ' %l:%M %e %B %Y')
end
end
我无法像这样给我的助手 org_request(因为它不接受 '.'):
def text_for_time_of_status_change(org_request.current_state)
这是不对的。我什至不确定是否需要在 org_request 前面使用“@”(尽管我在尝试时遇到错误)。
谁能看到我需要做什么才能使用视图助手?
这些文章:http://6ftdan.com/allyourdev/2015/01/28/rails-helper-methods/ 似乎暗示助手不应该用于表示逻辑。也许它们适用于某些其他类型的操作。我什至不确定我是否在尝试将助手用于正确的目的。
您必须更新 text_for_time_of_status_change
方法并传递 org_req
而不是 org_req.current_state
。您收到错误是因为您想要访问 org_request
变量但没有将其传递给辅助方法。在此更改之后,不要忘记更新辅助方法代码,以便在 case
中调用 org_request.current_state
而不是 current_state
你必须修复你的 helper
并且在你的情况下最好使用 partial:
帮手:
def text_for_time_of_status_change(org_request)
case org_request.current_state
when 'requested'
org_request.requested_at.try(:strftime, ' %l:%M %e %B %Y')
when 'approved'
org_request.approved_at.try(:strftime, ' %l:%M %e %B %Y')
when 'rejected'
org_request.rejected_at.try(:strftime, ' %l:%M %e %B %Y')
when 'removed'
org_request.removed_at.try(:strftime, ' %l:%M %e %B %Y')
end
end
比创建部分:
_requested.html.erb
<%= text_for_time_of_status_change(org_request) %>
比在视图中部分使用集合:
<%= render partial: 'requested', collection: @org_requests, as: org_request %>
我正在尝试学习如何在我的 Rails 5 应用程序中使用助手。
我有一个 org_requests 模型,它具有保存执行各种步骤的日期的属性。我正在尝试在保存相关控制器操作时使用控制器更新这些属性。
例如,我有一个名为 :approved_at 的属性。在我的控制器中,我有一个名为 approved with:
的动作def approved
@org_request = current_user.organisation.org_requests.find(params[:id])
if @org_request.state_machine.transition_to!(:approved)
@org_request.update_attribute(approved_at: Time.zone.now)
flash[:notice] = "You've added this member."
redirect_to org_requests_path
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to :index
end
end
我认为这一行:
@org_request.update_attribute(approved_at: Time.zone.now)
现在应该保存时间,当批准的动作被保存时。
在我看来,我正在尝试渲染相关操作的执行时间。为此,我正在尝试编写:
<% @org_requests.each do |org_req| %>
<%= text_for_time_of_status_change(org_req.current_state) %>
然后我有一个助手有:
def text_for_time_of_status_change(current_state)
case current_state
when 'requested'
org_request.requested_at.try(:strftime, ' %l:%M %e %B %Y')
when 'approved'
org_request.approved_at.try(:strftime, ' %l:%M %e %B %Y')
when 'rejected'
org_request.rejected_at.try(:strftime, ' %l:%M %e %B %Y')
when 'removed'
org_request.removed_at.try(:strftime, ' %l:%M %e %B %Y')
end
end
我无法像这样给我的助手 org_request(因为它不接受 '.'):
def text_for_time_of_status_change(org_request.current_state)
这是不对的。我什至不确定是否需要在 org_request 前面使用“@”(尽管我在尝试时遇到错误)。
谁能看到我需要做什么才能使用视图助手?
这些文章:http://6ftdan.com/allyourdev/2015/01/28/rails-helper-methods/ 似乎暗示助手不应该用于表示逻辑。也许它们适用于某些其他类型的操作。我什至不确定我是否在尝试将助手用于正确的目的。
您必须更新 text_for_time_of_status_change
方法并传递 org_req
而不是 org_req.current_state
。您收到错误是因为您想要访问 org_request
变量但没有将其传递给辅助方法。在此更改之后,不要忘记更新辅助方法代码,以便在 case
org_request.current_state
而不是 current_state
你必须修复你的 helper
并且在你的情况下最好使用 partial:
帮手:
def text_for_time_of_status_change(org_request)
case org_request.current_state
when 'requested'
org_request.requested_at.try(:strftime, ' %l:%M %e %B %Y')
when 'approved'
org_request.approved_at.try(:strftime, ' %l:%M %e %B %Y')
when 'rejected'
org_request.rejected_at.try(:strftime, ' %l:%M %e %B %Y')
when 'removed'
org_request.removed_at.try(:strftime, ' %l:%M %e %B %Y')
end
end
比创建部分:
_requested.html.erb
<%= text_for_time_of_status_change(org_request) %>
比在视图中部分使用集合:
<%= render partial: 'requested', collection: @org_requests, as: org_request %>