1

I am trying to change "localhost:3000/users/1" to "localhost:3000/members/1".

I changed resources :users in the routes to resources :members and changed users_controller.rb to members_controller.rb but it doesn't seem to work (I also changed the class to class MembersController.

I have tried everything I can think of without success. Is there a way to manually define the "users/members" routes without using resources :users or resources :members?

routes.rb:

Template::Application.routes.draw do

  resources :users

  resources :sessions, only: [:new, :create, :destroy]


  get "users/new" 


  root 'static_pages#home'
  match '/signup',   to: 'users#new',            via: 'get'
  match '/signin',   to: 'sessions#new',         via: 'get'
  match '/signout',  to: 'sessions#destroy',     via: 'delete'
  match '/help',     to: 'static_pages#help',    via: 'get'
  match '/about',    to: 'static_pages#about',   via: 'get'
end 

users_controller.rb :

class UsersController < ApplicationController
   before_action :signed_in_user, only: [:edit, :update]
   before_action :correct_user,   only: [:edit, :update]



   def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      # Handle a successful update.
    else
      render 'edit'
    end
  end

 def show
    @user = User.find(params[:id])
  end

 def new
    @user = User.new
 end 


  def create
    @user = User.new(user_params)
    if @user.save
      flash[:success] = "Welcome to the Weight Loss Community"
      redirect_to @user
    else
      render 'new'
    end
  end

  private 

  def user_params
    params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
 end

  def signed_in_user
     unless signed_in?
      store_location
      redirect_to signin_url, notice: "Please sign in." unless signed_in?
end 

  def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end
  end
end 

users/show.html.erb:

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
  </aside>
</div>

Any help would be appreciated.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Bill K
  • 65
  • 1
  • 1
  • 6
  • 1
    You should provide an error. – Alexander Karmes Dec 07 '14 at 23:00
  • Have you already run the migrations to create users? If so, you'll have to change the table name with a migration, in which case this will help: http://stackoverflow.com/questions/1992019/how-can-i-rename-a-database-column-in-a-rails-migration – NM Pennypacker Dec 07 '14 at 23:03
  • Did you try using the `:as` or `:path` options in the route? something like: `resources :users, as: :members` or `resources :users, path: 'members'` ... something similar was in my code when I was working on a Rails 3 project, but I haven't had to use that for a while now... – Myst Dec 07 '14 at 23:08

1 Answers1

1

In your routes file, the resources command takes the model name, which doesn't necessarily have to match the path.

Check out section 4.3 http://guides.rubyonrails.org/routing.html

And try this:

resources :users, as: 'members'

evanbikes
  • 4,131
  • 1
  • 22
  • 17