我为名为Userfolder的模型创建了嵌套路由。路线映射如下:
userfolder_userfolders POST /userfolders/:userfolder_id/userfolders(.:format) userfolders#create
new_userfolder_userfolder GET /userfolders/:userfolder_id/userfolders/new(.:format) userfolders#new
这正是我想要的方式。但是当我创建一个新的用户文件夹时,Rails会将创建操作重定向到“/ userfolders”而不是“/ userfolders /:userfolder_id / userfolders”。它仍然遵循最初的Rails脚手架路线。
有没有办法改变这个?或者我完全错过了什么?
编辑1:这是我的用户文件夹控制器代码。
class UserfoldersController < ApplicationController
before_action :set_userfolder, only: [:show, :edit, :update, :destroy]
before_action :set_parentfolder, except: [:show, :edit, :update, :destroy, :index]
# GET /userfolders
# GET /userfolders.json
def index
if Userfolder.first.nil?
Userfolder.create(:name => 'root', :parent_id => 0)
end
redirect_to Userfolder.first
end
# GET /userfolders/1
# GET /userfolders/1.json
def show
end
# GET /userfolders/:userfolder_id/userfolders/new(.:format)
def new
@userfolder = @parentfolder.children.build
end
# GET /userfolders/1/edit
def edit
end
# POST /userfolders/:userfolder_id/userfolders
def create
@userfolder = @parentfolder.children.build(userfolder_params)
respond_to do |format|
if @userfolder.save
format.html { redirect_to userfolder_path(@parentfolder.id), notice: 'Userfolder was successfully created.' }
else
render :action => 'new'
end
end
end
# PATCH/PUT /userfolders/1
# PATCH/PUT /userfolders/1.json
def update
respond_to do |format|
if @userfolder.update(userfolder_params)
format.html { redirect_to @userfolder, notice: 'Userfolder was successfully updated.' }
else
render :action => 'edit'
end
end
end
# DELETE /userfolders/1
# DELETE /userfolders/1.json
def destroy
parent_folder = @userfolder.parent
@userfolder.destroy
respond_to do |format|
format.html { redirect_to parent_folder, notice: 'Userfolder was successfully destroyed.' }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_userfolder
@userfolder = Userfolder.find(params[:id])
end
def set_parentfolder
@parentfolder = Userfolder.find(params[:userfolder_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def userfolder_params
params.require(:userfolder).permit(:name, :parent_id)
end
end
这是我的routes.rb文件:
Rails.application.routes.draw do
resources :userfiles
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'userfolders#index'
resources :userfolders, :shallow => true, :except => [:new, :create] do
resources :userfolders, :only => [:new, :create]
end
编辑2: 这是控制台输出:
答案 0 :(得分:1)
所以问题出现在我怀疑的_form.html.erb上。
这是创建控制器:
def create
@userfolder = @parentfolder.children.build(userfolder_params)
respond_to do |format|
if @userfolder.save
format.html { redirect_to @parentfolder, notice: 'Userfolder was successfully created.' }
else
render :action => 'new'
end
end
end
这是_form.html.erb:
<%= form_for([@parentfolder, @userfolder]) do |f| %>
<% if @userfolder.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@userfolder.errors.count, "error") %> prohibited this userfolder from being saved:</h2>
<ul>
<% @userfolder.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :parent_id %><br>
<%= f.number_field :parent_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
通过添加<%= form_for([@parentfolder, @userfolder]) do |f| %>
,我告诉Rails重定向到/ userfolders /:userfolder_id / userfolders。而不是重定向到/ userfolders。