2015-03-31 57 views
0

我有一個按字母順序排列的帖子標題列表,但是我的編輯鏈接和刪除鏈接在我的索引頁上不起作用。當我將它們添加到顯示頁面時,它們會工作。我以爲我有這個工作之前,但得到錯誤「未定義的局部變量職位」我試過這個。爲什麼我的編輯和刪除鏈接不能在我的索引頁上工作?

edit_post_path(posts) 
edit_post_path(@post) 
edit_post_path(@posts) 

posts控制器

def index 
     @posts = current_user.posts.all.group_by {|post| ('a'..'z').include?(post.title.downcase[0]) ? post.title.downcase[0] : '#' } 
end 

def edit 
    @post = current_user.posts.find(params[:id]) 
end 


def update 
    @post = current_user.posts.find(params[:id]) 

    if @post.update_attributes(post_params) 
     redirect_to action: "index" 
     flash[:success] = "Post Updated" 
    else 
     render 'edit' 
    end 
end 


def destroy 
    @post = current_user.posts.find(params[:id]) 
    @post.destroy 
    redirect_to action: "index" 
    flash[:success] = "Post Deleted" 
end 

index.html.erb

<% @posts.keys.sort.each do |key| %> 

    <div class= "posts-letter"><%= key.upcase %></div> 

    <% @posts[key].each do |t| %> 
     <div class="post"> 

      <div class="post-title"><%= t.title %></div> 

      <div class="action-buttons"> 

       <%= link_to edit_post_path(post), class: "edit-button" do %> 
        <i class="fa fa-pencil"></i> 
       <% end %> 

       <%= link_to post, method: :delete, data: { confirm: "You sure?" }, class: "delete-button" do %> 
        <i class="fa fa-trash"></i> 
       <% end %> 

      </div> 

     </div> 
    <% end %> 

<% end %> 

回答

0

看來,teach內您的文章對象。所以你可以把它傳遞給edit_post_path。或者您可以將其命名爲post而不是t

<% @posts[key].each do |post| %> 
     <div class="post"> 

      <div class="post-title"><%= post.title %></div> 

      <div class="action-buttons"> 

       <%= link_to edit_post_path(post), class: "edit-button" do %> 
        <i class="fa fa-pencil"></i> 
       <% end %> 

       <%= link_to post, method: :delete, data: { confirm: "You sure?" }, class: "delete-button" do %> 
        <i class="fa fa-trash"></i> 
       <% end %> 

      </div> 

     </div> 
    <% end %> 
相關問題