简体   繁体   English

对 rails 如何找到当前控制器中没有的动作感到困惑?

[英]Confused about how rails find the actions that does not in current controller?

This might be a dumb question, please forgive me.这可能是一个愚蠢的问题,请原谅我。 I am new to Rails and I google how to locate the actions that not included in current controller but could not figure it out.我是 Rails 的新手,我用谷歌搜索如何找到当前控制器中未包含但无法弄清楚的操作。 I noticed that Rails follows the inheritance characteristic.我注意到 Rails 遵循继承特性。 I am confused about how rails finds the 'log_in' method in create action?我对 rails 如何在创建操作中找到“log_in”方法感到困惑? Since I did not include log_in action in current user controller and neither in user_helper controller and application_helper controller, rails should not find this action at all.由于我没有在当前用户控制器中包含 log_in 操作,也没有在 user_helper 控制器和 application_helper 控制器中包含 log_in 操作,因此 rails 根本不应该找到此操作。 Is there any convention that rails will find action in other places?是否有任何约定,rails 会在其他地方找到行动?

1.This is normal my user_controller.rb 1.这是正常的我的user_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :require_login, except:[:new, :create] 

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = current_user
  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
        log_in(@user)
        puts @user
        puts session[:user_id]

        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

2. This is my application_helper.rb 2. 这是我的application_helper.rb

module ApplicationHelper
end

3. This is my users_helper.rb 3. 这是我的 users_helper.rb

module UsersHelper
end

4. This is my application_controller.rb 4. 这是我的application_controller.rb

class ApplicationController < ActionController::Base
    before_action :require_login
        def require_login
            if !logged_in?
                redirect_to login_path
            end
        end
end

If this is a variant of the RailsTutorial, which it looks like from here , I think the log_in method is defined in a helper named SessionsHelper in app/helpers/sessions_helper.rb that looks something like this:如果这是 RailsTutorial 的变体, 从这里看起来像这样,我认为log_in方法是在app/helpers/sessions_helper.rb中名为SessionsHelper的帮助app/helpers/sessions_helper.rb定义的,如下所示:

module SessionsHelper

  # Logs in the given user
  def log_in(user)
    session[:user_id] = user.id
  end
end

Helpers are automatically included into controllers, and public methods in helpers are available to both the controller and the view.助手会自动包含在控制器中,助手中的公共方法对控制器和视图都可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM