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

同样根据要求,这里是显示微帖子部分的 show.html.erb 文件

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="span4">
    <section>
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
  </aside>
  <div class="span8">
    <%= render 'follow_form' if signed_in? %>
    <% if @user.microposts.any? %>
      <h3>Microposts (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>
</div>
4

2 回答 2

0

在 show.html.erb 中,您必须添加以下行以在显示视图中呈现您的“微帖子”部分。

<%= 渲染“微博”%>

于 2013-06-13T15:55:39.550 回答
0

你能发布你的控制器代码吗?通常,您在控制器中设置成员变量,因此您的 erb 如下所示:

<%= @micropost.content %>

你的控制器是这样的:

def show  
  @micropost = Micropost.find(params[:id])  
end
于 2012-05-10T23:23:19.700 回答