Posts #index中的ActionController :: UrlGenerationError

时间:2015-04-08 11:59:03

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

我正在尝试链接到索引页面内嵌套路由的删除方法,但是我收到以下错误:

  

没有路线匹配{:action =>“show”,:controller =>“comments”,:id =>“1”,   :post_id => nil}缺少必需的键:[:post_id]

以下是代码:

show.html.erb

<p><%= notice %></p>

<p>
  <strong>Content:</strong>
  <%= @post.content %>
</p>

<div id="comments_wrapper">
    <%= render @post.comments %>
    <div id="form">
        <%= render "comments/form",  %>
    </div>
</div>

<%= link_to 'Edit', edit_post_path(@post) %>
<%= link_to 'Back', posts_path %>

_form.html.erb

<%= form_for([@post, @post.comments.build]) do |f| %>
    <%= f.text_field :content, placeholder: "New Comment" %>
    <%= f.submit %>
<% end %>

_comment.html.erb

<p><%= comment.content %></p>
#the line below gives me the error.
<%= link_to "delete", post_comment_path(@post, comment.id), method: :delete, data: { confirm: "Are you sure?" } %>

这很有效,但当我尝试在索引页面上显示相同的内容时,我会从 _comment.html.erb 中获得错误。

index.html.erb

<h2>Posts</h2>
    <% @posts.each do |post| %>
        <%= post.content %>
        <%= link_to 'Show', post %>
        <%= link_to 'Edit', edit_post_path(post) %>
        <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
        <br>

        <div id="comments_wrapper">
            <%= render post.comments %>
            <div id="form">
                <%= render "comments/form" %>
            </div>
        </div>
    <% end %>
<br>
<%= link_to 'New Post', new_post_path %>

我如何更改代码以便在索引页面上显示删除按钮?

以下是我的其余代码:

  #routes.rb
  resources :posts  do
    resources :comments
  end
  #post.rb
  has_many :comments
  #comment.rb
  belongs_to :post


  #comments_controller.rb
  before_action :set_post

  def create
    @comment = @post.comments.create(comment_params)
    redirect_to @post
  end

  def destroy
    @comment = @post.comments.find(params[:id])
    if @comment.destroy
      flash[:success] = "Comment was deleted."
    else
      flash[:error] = "Comment could not be deleted."
    end
    redirect_to @post
  end

  private

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

  def comment_params
    params[:comment].permit(:content)
  end

#posts_controller.rb
before_action :set_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all
  end

  def show
  end

  def new
    @post = Post.new
  end

  def edit
  end

  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    def post_params
      params.require(:post).permit(:content)
    end

1 个答案:

答案 0 :(得分:2)

comment.html.erb 中定义了@post之类的内容。 你必须这样做,

<%= link_to "delete", post_comment_path(comment.post.id, comment.id), method: :delete, data: { confirm: "Are you sure?" } %>