在我的Business模型中,当我尝试创建业务时出现错误,我注意到在发布后它删除了我的shared/page_header partial:
def page_title
"#{@page_title}"
end
<% unless @page_title.blank? %>
<div class="row">
<div class="page-header span12">
<h1><%= @page_title %></h1>
</div>
</div>
<% end %>
def new
@business = Business.new
@page_title = "Add Business"
end
def create
@business = Business.new(params[:business])
if @business.save
redirect_to :back, :notice => "This Business was successfully added."
else
render :new, :notice => "It seems there was an error. Please try again."
end
end现在,我注意到我从/businesses/new开始,但在帖子之后,它转到了/businesses。有人告诉我这很正常,但直到现在我都没见过这种行为。如果有帮助,我的Business资源中只有new和create操作。我能做些什么来让它正常工作呢?
发布于 2012-05-30 08:38:01
只需将@page_title = "Add Business“添加到您的create方法中。"render : new“呈现新模板,但不执行新方法。
使用render with :action是Rails新手经常感到困惑的地方。指定的操作用于确定要呈现哪个视图,但Rails不会在控制器中运行该操作的任何代码。在调用render之前,必须在当前操作中设置视图中所需的任何实例变量。来自http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
https://stackoverflow.com/questions/10807678
复制相似问题