为什么 Ruby on Rails 不能正确生成“通知”? HTML?
在我看来,嵌入的 ruby 代码是...
<p id="notice"><%= notice %></p>
但是当 URL 读取时...
http://0.0.0.0:3000/projects/3?notice=Project+was+successfully+created
生成的 HTML 是...
<p id="notice"></p>
我期望它读取...
<p id="notice">Project was successfully created</p>
给出了什么?
编辑:这是我的控制器代码,只是为了缓解好奇心。
def create
@project = Project.new(params[:project])
respond_to do |format|
if @project.save
format.html { redirect_to(:action => "show", :id => @project, :notice => 'Project was successfully created.') }
format.xml { render :xml => @project, :status => :created, :location => @project }
else
format.html { render :action => "new" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
end
end
end
非常感谢!
The embedded ruby code in my view is...
<p id="notice"><%= notice %></p>
but when the URL reads...
http://0.0.0.0:3000/projects/3?notice=Project+was+successfully+created
the generated HTML is...
<p id="notice"></p>
I'm expecting it to read...
<p id="notice">Project was successfully created</p>
What gives?
EDIT: Here's my controller code just to ease any curiosity..
def create
@project = Project.new(params[:project])
respond_to do |format|
if @project.save
format.html { redirect_to(:action => "show", :id => @project, :notice => 'Project was successfully created.') }
format.xml { render :xml => @project, :status => :created, :location => @project }
else
format.html { render :action => "new" }
format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
end
end
end
Thanks so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的经历与 BeachRunnerJoe 完全相同,并在这里找到了答案。因为我不喜欢任何一个,所以我做了一些自己的研究。我知道这个问题/答案已经有一年多了。尽管如此,我想无论如何我都会发布一个迟来的答案。
以下内容对我有用:
redirect_to 方法采用两个参数:options 和 *response_status*。如果没有额外的 { },Ruby 会将所有参数解释为属于 options,因此在 URL 的 options 中包含 :notice 哈希。
希望这有帮助。
彼得
I was experiencing exactly the same as BeachRunnerJoe and came across the answers here. As I didn't like any, I did a bit of my own research. I know this question/answer is over a year old. Nonetheless, I thought I would post a belated answer anyway.
The following worked for me:
The redirect_to method takes two parameters: options and *response_status*. Without the extra { }s, Ruby interprets all parameters as belonging to options and therefore includes the :notice hash in the options for the URL.
Hope this helps.
Peter
尝试用类似的东西在控制器中分配值
,然后你可以
在你的视图中使用类似的东西。
您可能希望阅读此问题的答案,它更明确地处理传递变量URL 查询字符串。
Try assigning the value in the controller with something like
and then you can use something like
in your view.
You may wish to read the answer to this question which deals more explicitly with passing variables through URL query strings.
您的代码不起作用,因为“通知”便捷方法使用闪存,而您想要显示参数,而不是闪存。尝试先将通知分配给 flash 变量,然后再重定向,而不是进行重定向并在参数中分配通知。
Your code is not working because the "notice" convenience method uses the flash, whereas you want to display the params, not the flash. Try instead of doing a redirect and assigning the notice in the params, to first assign the notice to a flash variable then redirect.