将localhost:3000 / users / 1更改为localhost:3000 / members / 1

时间:2014-12-07 22:51:20

标签: ruby-on-rails ruby ruby-on-rails-4

我正在尝试将“localhost:3000 / users / 1”更改为“localhost:3000 / members / 1”。

我将路由中的resources :users更改为resources :members并将users_controller.rb更改为members_controller.rb但似乎无效(我也将类更改为{{1} }。

我已经尝试了所有我能想到的但没有成功的事情。有没有办法在不使用class MembersControllerresources :users的情况下手动定义“用户/成员”路由?

routes.rb中:

resources :members

users_controller.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 

用户/ show.html.erb:

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 

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

在您的路线文件中,resources命令采用模型名称,该名称不一定必须与路径匹配。

查看第4.3节http://guides.rubyonrails.org/routing.html

试试这个:

resources :users, as: 'members'