【发布时间】:2016-12-14 06:19:53
【问题描述】:
我有一个用于创建和更新的部分表单...对于新的它呈现部分但在进行编辑时我不断收到此错误
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]
我从旧问题中寻找答案,但没有人解决我的问题...该表格用于创建独家新闻...
scoope 和用户之间的关联是:
scoope belong_to user
and user has_many scoopes
这是我的勺子控制器:
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
我的勺子桌:
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
................
【问题讨论】:
-
请发布 routes.rb
-
在您进行编辑调用时,您的控制台日志是否显示
id正在传递? -
@Deepak 我添加了你现在可以看到的路线
-
@MarsAtomic 如果你在这里谈论这个是.. 参数:{"user_name"=>"Sam Abunu", "id"=>"11"} that 错误前显示的 id在日志中
-
@Tobias 感谢您的改进
标签: ruby-on-rails ruby forms ruby-on-rails-4 routes