关于rails上的ruby:将localhost:3000 / users / 1更改为localhost:3000 / members / 1

Change localhost:3000/users/1 to localhost:3000/members/1

我想将"localhost:3000 / users / 1"更改为"localhost:3000 / members / 1"。

我将路径中的resources :users更改为resources :members并将users_controller.rb更改为members_controller.rb但它似乎不起作用(我还将类更改为class MembersController

我已经尝试了所有我能想到的但没有成功的事情。 有没有办法手动定义"用户/成员"路由而不使用resources :usersresources :members

routes.rb中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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

用户/ show.html.erb:

1
2
3
4
5
6
7
8
9
10
<% provide(:title, @user.name) %>

 
    <section>
     
        <%= gravatar_for @user %>
        <%= @user.name %>
     
    </section>
  </aside>

任何帮助,将不胜感激。


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

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

试试这个:

resources :users, as: 'members'