2016-08-02 213 views
0

我想學習如何在Rails 4中使用路線和路徑。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

這是因爲方法名稱是「request_approval_organisation_request」。你正在調用這條路線來控制一個控制器。如果添加

get 'exit', to: 'sessions#destroy', as: :logout 

內,您的資源:organisation_requests你會得到

logout_organisation_request_path 

0

rake在Rails 4中有一個routes命令,它可以讓你看到你的URL路徑助手都是由你的routes.rb文件的內容命名的。

從您的項目根目錄,rake routes | grep request_approval會產生什麼?