我正在尝试学习如何在Rails 4中使用路由和路径。
我有一个名为组织请求的模型。
我在routes.rb中有路由,如下所示:
resources :organisation_requests do #, only: [ :index, :new, :create ]
member do
put "requested" => "organisation_requests#requested", as: :request_approval #what does this mean?
put "approved" => "organisation_requests#requested", as: :approved #what does this mean?
put "rejected" => "organisation_requests#rejected", as: :not_approved #what does this mean?
put "removed" => "organisation_requests#removed", as: :removed #what does this mean?
end
end
在我的组织请求控制器中,我有:
def approved
organisation_request = OrganisationRequest.find(params[:id])
authorize @organisation_request
if organisation_request.state_machine.transition_to!(:approved)
flash[:notice] = "You've been added as a member. Welcome aboard."
format.html { redirect_to :index }
# format.json { render :show, status: :ok, location: @project }
# redirect_to action: :show, id: project_id
# add mailer to send message to owner that article has been approved
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to(profile_path(current_user.profile))
# redirect_to action: :show, id: project_id
end
end
在我的组织请求索引中,我正在尝试创建一个允许用户批准请求的路径:
<% @organisation_requests.each do |orgReq| %>
<tr>
<td>
<%#= link_to orgReq.profile.user.full_name, organisation_request.profile_path(organisation_request.profile.id) %>
</td>
<td>
<%= orgReq.created_at.try(:strftime, '%e %B %Y') %>
</td>
<td>
<%= orgReq.current_state %>
</td>
<td>
<% if policy(orgReq).approved? %>
<%= link_to "APPROVE", request_approval_path(@organisation_request), :class=>'btn btn-info', method: :put %>
<% end %>
</td>
</tr>
<% end %>
当我保存所有这些并尝试时,我希望批准按钮能够正常工作。相反,我收到的错误是:
undefined method `request_approval_path' for #<#<Class:0x007fa470f72968>:0x007fa474d17a98>
我不确定我哪里出错了?
答案 0 :(得分:0)
rake
有一个routes
命令,可以让您查看由于routes.rb
文件的内容而命名的url路径帮助程序。
从项目根目录,rake routes | grep request_approval
会产生什么?
答案 1 :(得分:0)
那是因为方法名称是“request_approval_organisation_request”。你正在呼叫该路线确保控制器。如果你添加
get 'exit', to: 'sessions#destroy', as: :logout
在您的资源中:您将获得organisation_requests
logout_organisation_request_path
等等