我正在尝试在 Controller 方法的 format.html
中传递参数像这样:
def create
@doc = Doc.new(params[:doc])
respond_to do |format|
if @doc.save
format.html { redirect_to share_url(@doc.user.ftp, @doc) }
else
format.html { render "new", :locals => { :template => @doc.template_id } }
end
end
end
我收到一个无方法错误,告诉我我正在向
:template
发送正确的参数在局部变量中:local_assigns {:template=>4}
有什么我想念的东西让它工作吗?它应该重定向到
doc#new
行动,但它会去doc#index
反而。有任何想法吗?
最佳答案
在 Controller 中使用此语法:
render "new", :locals => { :template => @doc.template_id }
表示您正在渲染
new.html.erb
模板,不是部分的。您无法将局部变量传递给非局部 View 。你应该做的是调用render "new"
, 并引用 @doc
在你需要的那个 View 中。
关于ruby-on-rails - Rails : params in format. html { 渲染 ... },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517771/