为什么 Ruby on Rails 不能正确生成“通知”? HTML?

发布于 2024-09-28 06:26:29 字数 1037 浏览 1 评论 0原文

在我看来,嵌入的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2024-10-05 06:26:29

我的经历与 BeachRunnerJoe 完全相同,并在这里找到了答案。因为我不喜欢任何一个,所以我做了一些自己的研究。我知道这个问题/答案已经有一年多了。尽管如此,我想无论如何我都会发布一个迟来的答案。

以下内容对我有用:

redirect_to( { :action => "show", :id => @project }, { :notice => 'Project was successfully created.' } )

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:

redirect_to( { :action => "show", :id => @project }, { :notice => 'Project was successfully created.' } )

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

荆棘i 2024-10-05 06:26:29

尝试用类似的东西在控制器中分配值

@notice_text = params[:notice]

,然后你可以

<p id="notice><%= @notice_text %></p>

在你的视图中使用类似的东西。

您可能希望阅读此问题的答案,它更明确地处理传递变量URL 查询字符串。

Try assigning the value in the controller with something like

@notice_text = params[:notice]

and then you can use something like

<p id="notice><%= @notice_text %></p>

in your view.

You may wish to read the answer to this question which deals more explicitly with passing variables through URL query strings.

ぃ弥猫深巷。 2024-10-05 06:26:29

您的代码不起作用,因为“通知”便捷方法使用闪存,而您想要显示参数,而不是闪存。尝试先将通知分配给 flash 变量,然后再重定向,而不是进行重定向并在参数中分配通知。

if @project.save
  flash[:notice] = 'Project was successfully created'
  redirect_to project_path(@project)
end

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.

if @project.save
  flash[:notice] = 'Project was successfully created'
  redirect_to project_path(@project)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文