繁体   English   中英

使用rails4销毁的问题

[英]issue with destroy using rails4

我使用的是Rails v4.2.4,在名为post的模型上的destroy调用有问题。 复制下面的配置和代码以及日志输出。 谁能发现我在做什么错?

index.html.erb

  <tbody>
    <% @posts.each do |post| %>
      <tr>
        <td><%= post.title %></td>
        <td><%= post.body %></td>
        <td><%= link_to 'Show', post %></td>
        <td><%= link_to 'Edit', edit_post_path(post) %></td>
        <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Ar
e you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>

岗位控制器

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy
   # flash[:success] = "Post deleted"
    respond_to do |format|
      flash[:success] = "post was deleted"
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
    redirect_to_posts_url
  end

在配置目录中路由

Rails.application.routes.draw do
  resources :posts
 # get 'posts/index'

  # 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"
   delete 'posts' => 'posts#destroy'
   root 'posts#index'

当我在浏览器的HTML页面上单击Destroy调用时,这是我在日志文件中看到的内容:

Started GET "/posts" for x.x.x.x at 2015-09-12 10:06:10 -0400
Cannot render console from x.x.x.x! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PostsController#index as HTML
  Post Load (0.3ms)  SELECT "posts".* FROM "posts"
  Rendered posts/index.html.erb within layouts/application (2.7ms)

本质上,从不调用delete控制器动作。 知道我在做什么错吗? 仅当您了解问题并可以分享解决问题的想法时,才可以分享。

注意:我也尝试过inde.html.erb文件中的posts_path(post)和posts_path(而不只是“ posts”,但这不能解决问题)。

另外,我的路由文件中之前没有“ delete'posts'=>'posts#destroy'”,但仍然存在相同的问题

更新:我将IP地址列为白色,但这不能解决问题。 网址格式不正确(我认为):

<td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/posts/1">Destroy</a></td>

默认情况下,仅允许来自IPv4和IPv6本地主机的请求。

您需要在Web控制台配置中将xxxx网络空间列入白名单。 像这样:

class Application < Rails::Application
     config.web_console.whitelisted_ips = 'x.x.x.x'
end

参考: https : //github.com/rails/web-console#configweb_consolewhitelisted_ips

并且您还需要进行以下更改:

# config/routes.rb

resources :posts

然后在posts控制器中,执行以下操作:

class PostsController < ApplicationController
    before_action :set_post, only: [:show, :edit, :update, :destroy]

    # ...
    # DELETE /posts/1
    # DELETE /posts/1.json
    def destroy
       @post.destroy

       redirect_to posts_url
    end

    private
        def set_post
            @post = Post.find(params[:id])
        end
end

您好像两次调用了重定向。 这将导致导轨抛出双重渲染错误。 删除响应块之后的redirect_to_posts_url 您的方法应如下所示:

def destroy
  @post = Post.find(params[:id])
  @post.destroy
  respond_to do |format|
     format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
    format.json { head :no_content }
   end
end

您也可以删除Flash消息,因为它已经由通知notice: 'Post was successfully destroyed.'设置notice: 'Post was successfully destroyed.'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM