我有一个按字母顺序排列的帖子标题列表,但我的编辑链接和删除链接不在我的索引页面上工作。当我将它们添加到节目页面时,它们可以正常工作。我以为我之前有这个工作,但得到错误"未定义的局部变量帖子"我试过这个。
edit_post_path(posts)
edit_post_path(@post)
edit_post_path(@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 :(得分:0)
似乎t
是each
内的帖子对象。所以你会把它传递给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 %>