我试图在一个控制器的 create 方法中创建一个实例变量,然后在另一个控制器的视图中使用它。这可能吗?
我正在创建@content
变量MicropostsController
:
class MicropostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
@content = 'test' #params[:micropost][:content]
#pattern = /\A@\w+/
#if params[:micropost][:content] =~ pattern
# @content = params[:micropost][:content]
#end
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_path
else
@feed_items = []
render 'static_pages/home'
end
end
我正在尝试在类视图中使用的部分中使用它,StaticPages
但它不起作用:
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
<% if @content %>
<%= @content %>
<% else %>
<%= 'no content' %>
<% end %>
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
data: { confirm: "You sure?" },
title: feed_item.content %>
<% end %>
</li>