我无法弄清楚这一点。一切似乎都正确设置。创建注释工作正常,但当我尝试销毁它时,给我一个丢失的模板错误。我已经搜索并尝试了我能想到的一切。我的一部分认为它是一个JS问题但是当我查看我的日志时,它似乎工作正常。我也使用设计和FYI。任何帮助都会很棒。感谢。
浏览器错误
Missing template comments/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.
_comment.html.erb
<p>
<strong>Comment:</strong>
<%= comment.body %>
<p>posted by: <%= comment.user.name %></p>
</p>
<p>
<%= link_to 'Destroy Comment', [@pit, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
评论控制器
class CommentsController < ApplicationController
def create
@pit= Pit.find(params[:pit_id])
@comment = @pit.comments.build(comments_params)
@comment.user = current_user
@comment.save
redirect_to pit_path(@pit)
end
def destroy
@pit = Pit.find(params[:pit_id])
@comment = @pit.comments.find(params[:id])
@comment.destroy
redirect_to pit_path(@pit)
end
def show
end
private
def comments_params
params.require(:comment).permit(:body, :user_id)
end
end
坑控制器
def index
@pit = Pit.all
@user = User.find_by(params[:id])
@pit = @user.pits
@pits = Pit.order('created_at DESC').group_by { |pit| pit.created_at.strftime("%B %Y") }
end
def create
@user = current_user
@pit = current_user.pits.create(pit_params)
if @pit.save
redirect_to @pit
else
render 'new'
end
end
def show
@pit = Pit.find(params[:id])
end
def edit
end
def update
end
private
def pit_params
params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id)
end
end
路线
Rails.application.routes.draw do
devise_for :users, :controllers => { registrations: 'registrations' }
devise_scope :user do
get 'users/sign_in' => 'devise/sessions#new'
get 'users/sign_out' => 'devise/sessions#destroy'
match 'users/:id', to: 'users#show', as: 'user', via: 'get'
end
resources :pits do
resources :comments
end
root to: 'pages#home'
get '/about' => 'pages#about'
end
路线
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
users_sign_in GET /users/sign_in(.:format) devise/sessions#new
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
user GET /users/:id(.:format) users#show
pit_comments GET /pits/:pit_id/comments(.:format) comments#index
POST /pits/:pit_id/comments(.:format) comments#create
new_pit_comment GET /pits/:pit_id/comments/new(.:format) comments#new
edit_pit_comment GET /pits/:pit_id/comments/:id/edit(.:format) comments#edit
pit_comment GET /pits/:pit_id/comments/:id(.:format) comments#show
PATCH /pits/:pit_id/comments/:id(.:format) comments#update
PUT /pits/:pit_id/comments/:id(.:format) comments#update
DELETE /pits/:pit_id/comments/:id(.:format) comments#destroy
pits GET /pits(.:format) pits#index
POST /pits(.:format) pits#create
new_pit GET /pits/new(.:format) pits#new
edit_pit GET /pits/:id/edit(.:format) pits#edit
pit GET /pits/:id(.:format) pits#show
PATCH /pits/:id(.:format) pits#update
PUT /pits/:id(.:format) pits#update
DELETE /pits/:id(.:format) pits#destroy
root GET / pages#home
about GET /about(.:format) pages#about
page GET /pages/*id
答案 0 :(得分:1)
由于您正在讨论“JS问题”,我假设您希望AJAX请求调用destroy操作。由于嵌套路由看起来很好,并且您还分配了正确的HTTP谓词,但您错过了将remote:true分配给执行AJAX请求的链接,如:
<%= link_to 'Destroy Comment', [@pit, comment],
method: :delete,
remote: true,
data: { confirm: 'Are you sure?' } %>
此外:
你最终在show动作中的原因是两个请求的资源URL是相同的(所以你的嵌套路由工作正常)。但似乎错过了HTTP动词
请做:
1.)发布生成的HTML链接
2.)发布请求标题(特别是X-Requested-With和Accept)
3.)在你的application.js中禁用除jQuery之外的所有自定义JS(在RAILS_ENV =开发中重启你的webserver)并测试删除请求是否仍然失败
如果在application.js中禁用自定义JS适用于链接,那么搜索,自定义JS会使其失败...