[英]How to overriding model controller with another controller in Rails
Question: 题:
Why the route still looking for properties/create
and how to make it route to admin_panels/properties#create
? 为什么路由仍在寻找
properties/create
以及如何使其路由到admin_panels/properties#create
?
Explaination: 说明:
I'm creating my own admin panel from scratch. 我正在从头开始创建自己的管理面板。 The main reason of creating the admin panel is to allow users easily perform
new
, edit
, update
, create
and destroy
actions for property
model. 创建管理面板的主要原因是允许用户轻松地对
property
模型执行new
, edit
, update
, create
和destroy
操作。 So, I've created a new PropertiesController
for admin panel
to override the existing one. 因此,我为
admin panel
创建了一个新的PropertiesController
来覆盖现有的。
What I'm expecting from this is after the user
create a new property
, it will create the property using the admin_panels/properties#create
. 我期望的是在
user
创建新property
,它将使用admin_panels/properties#create
创建该属性。 However, what i get is it try to find a route to properties/create
which is not correct. 但是,我得到的是它试图找到通往
properties/create
的路由,这是不正确的。
New AdminPanels::PropertiesController
新的
AdminPanels::PropertiesController
class AdminPanels::PropertiesController < PropertiesController
before_action :set_property, only: [:show, :edit, :update, :destroy]
def index
@properties = Property.all
end
def show
end
def new
@property = Property.new
end
def edit
end
def create
@property = Property.new(property_params)
respond_to do |format|
if @property.save
format.html { redirect_to ADMIN_PANEL_PROPERTY_SHOW }
else
format.html { render ADMIN_PANEL_PROPERTY_NEW }
end
end
end
def update
respond_to do |format|
if @property.update(property_params)
format.html { redirect_to ADMIN_PANEL_PROPERTY_SHOW }
else
format.html { render ADMIN_PANEL_PROPERTY_EDIT }
end
end
end
def destroy
@property.destroy
respond_to do |format|
format.html { redirect_to ADMIN_PANEL_PROPERTY_INDEX }
end
end
private
def set_property
@property = Property.find(params[:id])
end
def property_params
params.require(:property).permit(:name, :description, :address)
end
end
to override existing PropertiesController
覆盖现有的
PropertiesController
class PropertiesController < ApplicationController
before_action :set_property, only: [:show]
def index
@properties = Property.all
end
def show
end
private
def set_property
@property = Property.find(params[:id])
end
def property_params
params.require(:property).permit(:name, :description, :address)
end
end
routes.rb routes.rb
resources :users
resources :properties, only: [:index, :show]
namespace :admin_panels do
resources :properties
end
Updated: 更新:
views > admin_panels > create > _form.html.slim 视图> admin_panels>创建> _form.html.slim
= simple_form_for(@property) do |f|
= f.error_notification
.form-inputs
= f.input :name
= f.input :description
= f.input :address
.form-actions
= f.button :submit
Currently, you have routes of properties
for index
and show
and under admin_panels
you have routes of all actions of properties
当前,您具有用于
index
和show
的properties
路由,在admin_panels
下,您具有properties
的所有操作的路由
Change your routing file like this: 像这样更改路由文件:
resources :users
namespace :admin_panels do
resources :properties, only: [:index, :show]
end
Write the following command in terminal to view you routes. 在终端中编写以下命令以查看路由。
rake routes
Write the following command in terminal to view routes of admin_panels
在终端中编写以下命令以查看
admin_panels
路由
rake routes | grep admin_panel
Specify action and controller in form: 以以下形式指定动作和控制器:
<%= simple_form_for @course_group,
url: url_for(
action: 'create',
controller: 'admin_panels/properties'
),
method: :post do |f| %>
Syntax might be different a little bit. 语法可能有所不同。
You can try that in two ways, first using the normal url
and also by using the url_for
helper of the simple_form_for. 您可以通过两种方式进行尝试,一种是使用常规
url
,另一种是使用simple_form_for的url_for
帮助器。
Normal way, 正常方式
<%= simple_form_for @provider, :url => admin_panel_properties_create_path, :method => :post do |f|%>
Using the helper, 使用助手
<%= simple_form_for @provider, url: url_for( action: 'create',controller: 'admin_panels/properties'), method: :post do |f| %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.