【发布时间】: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