-2

我正在和朋友一起编写网站,但我的代码出现了一个错误。在我朋友的电脑中,他的代码相同,没有任何错误。

我收到以下错误:

我们很抱歉,但有些不对劲。

好吧,我们是 Rails 的新手,当我尝试加载帖子页面时出现此错误。我们认为错误在于删除帖子,因为如果我们评论“posts_delete_path”行,一切正常。对于删除帖子,我们使用:

显示.hmtl.erb:

<% @hashtag.posts.each do |p| %>
    <li>Posted by: <%= p.user.name %></li>
    <li>Content:</li>
    <li><%= p.content %></li>
    <li><a href="<%= posts_delete_path %>/<%= p.id %>" >Delete</a></li>
<% end %>

路线:

   match '/signout', to: 'sessions#destroy'
   match 'posts/delete/:id', to: 'posts#destroy'
   resources :posts, :only => [:create, :show]

职位控制器:

def destroy
    @post = Post.find(params[:id])
    if current_user.id == @post.user_id
       @post.destroy
    end
end

----编辑----耙路线:

      posts GET    /posts(.:format)           posts#index
            POST   /posts(.:format)           posts#create
   new_post GET    /posts/new(.:format)       posts#new
  edit_post GET    /posts/:id/edit(.:format)  posts#edit
       post GET    /posts/:id(.:format)       posts#show
            PUT    /posts/:id(.:format)       posts#update
            DELETE /posts/:id(.:format)       posts#destroy

我们知道这是删除帖子的新手方法,但我们没有找到更好的解决方案。(如果您想提出建议,我们也会很高兴)。

4

4 回答 4

0

看起来你的链接很好,但听起来你没有包含你的 js,这就是为什么当你点击删除时它会将你重定向到显示页面。

<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>



1. 检查你的/assets/application.js

对于//= require jquery//= require jquery_ujs



在 application.js 中添加这段代码

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

2. 检查您的 /layouts/application.html.erb

为了<%= javascript_include_tag "application"%>

<head>
    <title>My awesome app</title>
    <%= stylesheet_link_tag "application" %>
    <%= javascript_include_tag "application"%>
    <%= csrf_meta_tag %>
  </head>
于 2013-03-27T13:13:53.717 回答
0

感谢所有尝试帮助我的人。现在我解决了我的问题,但通过其他方式。

我要做的是:

鉴于:

<% if current_user.id == p.user_id %>
            <%= button_to "Delete", { :controller => :posts, :action => 'destroy', :id => p.id }, :method => :delete %>
<% end %>

在控制器中:

  def destroy
    @post = Post.find(params[:id])
    #if current_user.id == @post.user_id
       @post.destroy
    end
  end

在路线:

resources :posts

多谢你们!

于 2013-03-28T13:03:33.577 回答
0
<%= link_to "Delete", p, method: :delete %>

并修改routes.rbwith

资源:帖子

于 2013-03-27T12:11:53.480 回答
0

你应该使用这样的link_to符号:

<%= link_to "Delete", p, method: :delete %>

此外,您应该尝试阻止用户甚至在视图中看到删除按钮,例如:

<% if current_user == p.user %>
    <%= link_to "Delete", p, method: :delete %>
<% end %>

这只会向帖子的所有者显示删除链接。

也像 Ganesh 说的,转这个:

match 'posts/delete/:id', to: 'posts#destroy'
resources :posts, :only => [:create, :show]

至:

resources :posts, :only => [:create, :show, :destroy]

查看 rails 指南中的路由系统:http: //guides.rubyonrails.org/routing.html,然后查看您需要哪些路线。

于 2013-03-27T12:05:02.743 回答