路由问题传递id rails 4

时间:2014-10-27 15:21:32

标签: ruby-on-rails-4 routes

首先道歉,因为当我认为我抓住路线时,我发现我不会!!

当注册用户登录我的应用程序时,他们将被带到管理员索引视图,而我想要做的就是在该视图中显示他们的用户名。

这是我的路线配置: -

Easygifts::Application.routes.draw do
  get 'admin' => 'admin#index' 
    controller :sessions do
        get 'login' => :new
        post 'login' => :create
        delete 'logout' => :destroy
    end
  get "sessions/create"
  get "sessions/destroy"
  resources :users 
  resources :admin 

    resources :stores do
        collection do
          get "writing"
          get "office"
          get "time"
          get "home"
          get "wellness"
          get "travel"
          get "bags"
          get "leisure"
          get "contact"
          get "terms"
         end
          member do
          get 'quote'
         end
        end

    resources :products

用户通过会话控制器登录,并传递到管理控制器索引视图。我试图使用以下内容显示其姓名: - <th colspan="2">Welcome <%= @users.name %></th>

Sessions控制器: -

class SessionsController < ApplicationController
    skip_before_action :authorize
  def new
  end

  def create
    user = User.find_by(name: params[:name])
    if user and user.authenticate(params[:password]) 
        session[:user_id] = user.id 
        redirect_to admin_url 
    else 
        redirect_to login_url, alert: "Invalid user/password combination" 
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to stores_url, notice: "Logged out"
  end
end

用户控制器: -

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.order(:name)
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to users_url, notice: "User #{@user.name} was successfully created." }
        format.json { render action: 'show', status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to users_url, notice: "User #{@user.name} was successfully updated." }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    begin 
    @user.destroy
        flash[:notice] = "User #{@user.name} deleted" 
    rescue StandardError => e 
        flash[:notice] = e.message
    end
    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:name, :password, :password_confirmation, :add1, :add2, :add3, :add4, :post_code, :home_tel, :mobile, :ice_tel, :ni_number, :position, :image_url, :staff_num, :start_date, :birthday)
    end
end

管理员控制器: -

class AdminController < ApplicationController
  def index
  @users = User.find(params[:id])
  end
end

我的第一个困惑点是我是应该通过Sessions控制器还是用户控制器?

如果它在Sessions控制器中,我应该写一些类似get 'admin#index' => :show

的内容

或者,如果我在用户中写下resources :users do member do get 'admin' end end

之类的内容

我认为我必须在我的路线中定义一个带有动作的控制器,但在这个例子中我不明白如何弄清楚谁在做什么。

0 个答案:

没有答案