2015-04-08 2 views
-1

Я пытаюсь сделать ссылку на метод удаления вложенного маршрута внутри страницы индекса, но я получаю следующее сообщение об ошибке:ActionController :: UrlGenerationError в сообщениях # индекс

No route matches {:action=>"show", :controller=>"comments", :id=>"1", :post_id=>nil} missing required keys: [: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 

ответ

2

отметить, как @post определяется в вашем _comment.html.erb. Вы должны сделать вот так:

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

Спасибо за быстрый ответ, это исправление! – zazaalaza

+0

You knerr ничего jon snerr –

+0

Может быть: D: D: D –

Смежные вопросы