Mel Mel - 1 year ago 85
Ruby Question

Rails 4 - routes and paths

I am trying to learn how to use routes and paths in Rails 4.

I have a model called organisation requests.

I have routes in by routes.rb as follows:

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?

In my organisation requests controller, I have:

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
flash[:error] = "You're not able to manage this organisation's members"
# redirect_to action: :show, id: project_id

In my organisation requests index, I'm trying to make a path that allows a user to approve a request:

<% @organisation_requests.each do |orgReq| %>

<%#= link_to orgReq.profile.user.full_name, organisation_request.profile_path( %>
<%= orgReq.created_at.try(:strftime, '%e %B %Y') %>
<%= orgReq.current_state %>
<% if policy(orgReq).approved? %>
<%= link_to "APPROVE", request_approval_path(@organisation_request), :class=>'btn btn-info', method: :put %>
<% end %>


<% end %>

When I save all this and try it, I expect the approve button to work. Instead I get an error that says:

undefined method `request_approval_path' for #<#<Class:0x007fa470f72968>:0x007fa474d17a98>

I'm not sure where I'm going wrong?

Answer Source

That's because the method name is "request_approval_organisation_request". You're calling that route insine a controller. If you add

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

Inside your resources :organisation_requests you will get


and so on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download