【问题标题】:Rails 4 - routes and pathsRails 4 - 路线和路径
【发布时间】:2016-08-02 00:03:05
【问题描述】:

我正在尝试学习如何在 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>

我不确定我哪里出错了?

【问题讨论】:

    标签: ruby-on-rails ruby path routes


    【解决方案1】:

    那是因为方法名称是“request_approval_organisation_request”。您在控制器中调用该路由。如果你添加

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

    在你的资源中:organisation_requests 你会得到

    logout_organisation_request_path
    

    等等

    【讨论】:

      【解决方案2】:

      Rails 4 中的rake 有一个routes 命令,可让您查看由于routes.rb 文件的内容而命名的url 路径助手。

      从您的项目根目录,rake routes | grep request_approval 产生了什么?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 1970-01-01
        • 2014-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多