0

所以我正在尝试添加对此应用程序中的微博的回复https://github.com/railstutorial/sample_app_2nd_ed

我想我弄清楚了所有的模型和控制器的东西,但这不是重点。

当我尝试向 app/views/microposts/_micropost.html.erb 添加回复链接时。它永远不会起作用。

<li>
  <span class="content"><%= micropost.content %></span>
  <p>this text doesnt show up!</p>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>
  <% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method:  :delete,
                                     confirm: "You sure?",
                                     title:   micropost.content %>
  <% end %>

  <%= link_to "reply", new_comment_path(:micropost_id => comment) %> |

  <p> why isnt this working!!?</p>
</li>

如您所见,我已尝试在第 3 行、第 13 行和第 15 行添加基本文本或回复链接。它永远不会出现。我做错了吗?我应该以什么格式放置回复链接/甚至是我想要显示的基本文本?

这是我的微柱控制器代码

class MicropostsController < ApplicationController
  before_filter :signed_in_user
  before_filter :correct_user,   only: :destroy

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_path
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    redirect_back_or root_path
  end

  private

    def correct_user
      @micropost = current_user.microposts.find_by_id(params[:id])
      redirect_to root_path if @micropost.nil?
    end
end

这是我的评论控制器

class CommentsController < ApplicationController 
  def create 
    @comment = @micropost.comments.new(params[:comment]) 
    if @comment.save 
      redirect_to @user
    else 
      redirect_to @user
    end 
  end 

  def destroy
    @comment.destroy
    redirect_back_or root_path
  end
end
4

1 回答 1

0

在你的 index.html.erb 中大概是这样的

<% @microposts.each do |micropost| %>
  <%= render partial: 'micropost', locals: { :micropost => micropost} %>
<% end %>

部分名称就是文件的名称减去 _ 和 .html.erb。局部变量将变量传递给部分

于 2012-05-11T03:37:57.863 回答