我有一个部分表单用于创建和更新...对于新的它呈现部分但在进行编辑时我不断收到此错误
ActionController::UrlGenerationError in Scoopes#edit
no route matches {:action=>"show", :controller=>"scoopes", :id=>nil, :user_name=>#<Scoope id: 11, name: "asdsada", user_id: 3, created_at: "2016-08-07 20:37:52", updated_at: "2016-08-07 20:37:52">} missing required keys: [:id]
我从旧问题中寻找答案,但没有人解决我的问题...表格是用于创建scoopes ....
scoope和用户之间的联系是:
scoope belong_to user
and user has_many scoopes
这是我的scoope控制器:
class ScoopesController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :set_scoope, only: [:show, :edit, :update, :destroy, :index]
before_action :owned_scoope, only: [:edit, :update, :destroy]
def index
@scoopes = @user.scoopes.all
end
def show
@scoope = @user.scoopes.find(params[:id])
end
def new
@scoope = current_user.scoopes.build
end
def create
@scoope = current_user.scoopes.build(scoope_params)
if @scoope.save
redirect_to scoope_path(current_user.user_name, @scoope)
else
render 'new'
end
end
def edit
@scoope = @user.scoopes.find(params[:id])
end
def update
@scoope = @user.scoopes.find(params[:id])
if @scoope.update(scoope_params)
redirect_to scoope_path(@user, @scoope)
else
render 'edit'
end
end
def destroy
@scoope = @user.scoopes.find(params[:id])
@scoope.destroy
redirect_to scoopes_path
end
private
def scoope_params
params.require(:scoope).permit(:name)
end
def set_scoope
@user = User.find_by(user_name: params[:user_name])
end
def owned_scoope
unless @user == current_user
flash[:danger] = "this scope dont belong to you"
redirect_to root_path
end
end
end
这是我的部分形式(我想也许问题可能与编辑路径有关,因为当我尝试用edit_scoope_path替换scoope然后它在编辑页面下渲染表单..但它不会解决我的鲸鱼问题,因为它是部分):
<div class="row">
<div class="col-md-5 formm">
<%= render 'shared/errors', obj: @scoope %>
<div class="well">
<%= form_for @scoope do |f| %>
<div class="form-group">
<%= f.label :name %><br/>
<%= f.text_area :name, rows: 6, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit class: 'btn btn-primary' %> <%= link_to "Back", :back, class: "btn btn-danger" unless current_page?(scoopes_path) %>
</div>
<% end %>
</div>
</div>
</div>
这是我的scoopes路线:
scoopes GET /:user_name/scoopes(.:format) scoopes#index
POST /:user_name/scoopes(.:format) scoopes#create
new_scoope GET /:user_name/scoopes/new(.:format) scoopes#new
edit_scoope GET /:user_name/scoopes/:id/edit(.:format) scoopes#edit
scoope GET /:user_name/scoopes/:id(.:format) scoopes#show
PATCH /:user_name/scoopes/:id(.:format) scoopes#update
PUT /:user_name/scoopes/:id(.:format) scoopes#update
DELETE /:user_name/scoopes/:id(.:format) scoopes#destroy
我的scoope表:
create_table "scoopes", force: :cascade do |t|
t.string "name"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
路线:
devise_for :users, :controllers => { :registrations => "user/registrations" }
root "posts#index"
scope '/:user_name' do
resources :scoopes
end
get ':user_name', to: 'profiles#show', as: :profile
get ':user_name/edit', to: 'profiles#edit', as: :edit_profile
patch ':user_name/edit', to: 'profiles#update', as: :update_profile
................
答案 0 :(得分:1)
您的通话失败是因为您正在使用scoope belongs to user
关系定义的nested route。
在routes.rb
中,您可以设置user
和scoope
之间的ReSTful路由,如下所示:
resources :users do
resources :scoopes
end
这种表示法应该为您提供所需的嵌套路线:
edit_user_scoope_path
要在user_name
下组织嵌套对象,请尝试:
scope "/:user_name" do
resources :posts
end
这应该给你一个这样的路线:
edit_scoope GET /:user_name/scoopes/:id/edit(.:format) scoopes#edit
答案 1 :(得分:0)
问题在于您的routes.rb
文件。您可以添加如下所示的嵌套路线:
resources :users do
resources :scoopes
end
将输出如下所示的网址。从此输出中您可以清楚地看到路径为user_scoope_path(current_user, @scoope)
。
scoopes#index
POST /users/:user_id/scoopes(.:format) scoopes#create
new_user_scoope GET /users/:user_id/scoopes/new(.:format) scoopes#new
edit_user_scoope GET /users/:user_id/scoopes/:id/edit(.:format) scoopes#edit
user_scoope GET /users/:user_id/scoopes/:id(.:format) scoopes#show
PATCH /users/:user_id/scoopes/:id(.:format) scoopes#update
PUT /users/:user_id/scoopes/:id(.:format) scoopes#update
DELETE /users/:user_id/scoopes/:id(.:format) scoopes#destroy
希望有所帮助。