使用rails4

时间:2015-09-12 14:19:40

标签: ruby-on-rails ruby ruby-on-rails-4

我正在使用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)

基本上,删除控制器操作永远不会被调用。知道我做错了什么吗?如果您了解问题,请分享,并分享有关如何解决问题的想法。

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

另外,我之前的路线文件中没有“删除'帖子'=&gt;'帖子#stroy'”,但我仍有同样的问题

更新:  我白色列出了IP地址,但这并没有解决问题。 URL格式不正确(我认为):

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

2 个答案:

答案 0 :(得分:0)

默认情况下,只允许来自IPv4和IPv6 localhosts的请求。

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

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

在帖子控制器中,执行以下操作:

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

答案 1 :(得分:0)

看起来你正在调用redirect两次。这将导致rails抛出双重渲染错误。删除响应阻止后的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.'

设置