【问题标题】:Rails 5 - Namespaced foldersRails 5 - 命名空间文件夹
【发布时间】:2016-10-20 05:53:19
【问题描述】:

我正在尝试(并没有走得太远)了解如何在我的 Rails 5 应用程序中使用命名空间文件夹。

我的架构中有用于用户、身份和设置的表。

这些关联是:

用户

has_many :identities, dependent: :destroy
has_one :setting, dependent: :destroy 

设置

belongs_to :user

身份

belongs_to :user

我已经组织了我的控制器文件夹,以便拥有 app/controllers/users/identities_controller.rb 和 app/controllers/users/settings_controller.rb

每个控制器都有第一行:

class Users::SettingsController < ApplicationController
class Users::IdentitiesController < ApplicationController

我以类似的方式组织了我的视图文件夹。

我有 app/views/users/settings/show。

除了名为 _authentications.html.erb 的部分身份之外,我没有身份视图。目前,它存储在 app/views/users 文件夹中。我正在尝试将其移动到 app/views/users/settings 文件夹并从 app/views/users/settings/show 页面呈现它。

在我的路线文件中,我有:

resources :users, shallow: true do
    scope module: :users do
      resources :assign_roles
      resources :identities
      resources :profiles
      resources :settings
    end

当我从 app/views/users 显示页面呈现部分身份验证时,我没有错误。一切正常。我被卡住了,因为有些东西我不理解而且我不知道它是什么。

我正在尝试将身份验证部分移至 app/views/users/settings,以便我可以从 app/views/users/settings/show 呈现它。

当我尝试这样做时,我收到一条错误消息:

undefined method `identities' for nil:NilClass

该错误引用了我的部分身份验证块:

<% if @user.identities.map(&:provider).include?('linkedin') %>
    <span class="glyphicon glyphicon-ok"></span>
<% else %>  
    <%= link_to 'Connect your Linkedin account', user_linkedin_omniauth_authorize_path %>
<% end %>

我不明白我是否应该尝试以某种方式在该 if 语句中添加设置?我尝试了 if @setting.user.identities(仅作为猜测),但没有成功。

谁能看到我做错了什么?我似乎找不到关于如何开始使用嵌套和命名空间的简单英文解释。

添加设置控制器

class Users::SettingsController < ApplicationController
    before_action :set_setting, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  after_action :verify_authorized

  def index
    @settings = Setting.all
    authorize @settings
  end

  def show
    # authorize @setting
  end


  def new
    @setting = Setting.new
    authorize @setting

  end

  def edit
  end

  def create
    @setting = Setting.new(setting_params)
    authorize @setting

    respond_to do |format|
      if @setting.save
        format.html { redirect_to @setting }
        format.json { render :show, status: :created, location: @setting }
      else
        format.html { render :new }
        format.json { render json: @setting.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
   respond_to do |format|
      if @setting.update(setting_params)
        format.html { redirect_to @setting }
        format.json { render :show, status: :ok, location: @setting }
      else
        format.html { render :edit }
        format.json { render json: @setting.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @setting.destroy
    respond_to do |format|
      format.html { redirect_to settings_url }
      format.json { head :no_content }
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_setting
      @setting = Setting.find(params[:id])
      authorize @setting
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def setting_params
      params.require(:setting).permit( :newsletter )
    end

end

【问题讨论】:

  • 错误消息似乎表明 @user 在您的部分中为零。可以分享一下相关的控制器动作代码吗?
  • @omnikron - 你指的是哪个部分?如果我在将 _authentications.html.erb 保存为视图/用户下的文件并在用户/显示中渲染时渲染它 - 一切正常。我要更改的事情是我移动身份验证部分以将其保存在views/users/settings 文件夹中,并将其呈现在views/users/settings/show.html.erb 中。在那种情况下哪个控制器是相关的?
  • 那个控制器的动作是渲染部分。我的猜测是设置控制器。
  • 我添加了设置控制器

标签: ruby-on-rails ruby namespaces nested


【解决方案1】:

在您的 show 操作中没有设置 @user 变量,这就是您收到 NoMethodError undefined method 'identities' for nil:NilClass 错误的原因。 @user 在您的部分返回 nil。您可以在控制器中使用@user = current_user 进行设置(假设您使用的是设计),或者在您的部分中将@user 替换为current_user

说了这么多——有什么特别的原因要像这样命名所有控制器吗?仅仅因为您在 models 之间设置了 has_manyhas_one 关系并不一定意味着您也必须为控制器命名空间。如果您有充分的理由这样做,那很好;如果没有,那么您可以考虑将控制器移出用户命名空间。

【讨论】:

  • 非常感谢。我想要名称空间的原因是我在顶层尝试了所有内容,当没有任何文件夹组织时它变得非常混乱。非常感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2011-05-05
  • 2011-09-08
  • 2019-05-11
相关资源
最近更新 更多