0

I have installed Devise (called members) and have set up some custom routes so that I can spit out all the members and allow them to have their own page (/memebers/ and /members/:id/)

However in my view file for the members index when passing the route members_path(member_id) it is outputting members.1 instead of members/1

Code below: index view:

<% @members.each do |member| %>
<tr>
<td><%= link_to member.name, members_path(member.id) %></td>
<td><%= member.email %></td>
<td><%= member.id %></td>
<td><%= member.admin %></td>
</tr>
<% end %>

Routes:

devise_for :members
match 'members/' => 'members#index'
match 'members/:id' => 'members#show'

Members Controller:

class MembersController < ApplicationController

  def index
    @members = Member.all
    respond_to do |format|
     format.html # show.html.erb
     format.json { render json: @student } 
   end 
  end

  def show
    @member = Member.find(params[:id])
  end

end

Rake Routes:

members        /members(.:format)               members#index
               /members/:id(.:format)           members#show

Any help? Cheers :)

2 Answers2

0

Had to add

, as: :show_member

so in my routes file I had to define a path for that

match 'members/:id/' => 'members#show', as: :show_member

and adding this in the index view file:

show_member_path(member)

and rake routes is now:

members GET    /members(.:format)               members#index
show_member GET    /members/:id(.:format)           members#show

Cheers :)

0

This is not the helper method you are looking for. members_path creates a path to members#index and takes format as its only argument.

The helper that links to members#show is member_path. Although, it's not automatically created by match:

# config/routes.rb
resources :members, only: [:index, :show]
# Remove `only` to get the remaining restful routes

# and in your view:
<%= link_to member.name, member_path(member.id) %>

You can also just pass in the member itself and link_to will automatically call to_param, which returns id by default:

<%= link_to member.name, member_path(member) %>

Alternatively, you can just pass in your member directly with no path helper:

<%= link_to member.name, member %>

Read more here: http://guides.rubyonrails.org/routing.html

Jeff Peterson
  • 1,429
  • 9
  • 10