gpt4 book ai didi

ruby-on-rails - 如何使 Rails View .erb 文件打印 URL 的最后一部分?

转载 作者:太空宇宙 更新时间:2023-11-03 18:31:11 25 4
gpt4 key购买 nike

我是 Rails 的新手。我已经学会了如何制作静态 Controller Action 和相应的 View 文件。现在我想做到这一点,当我转到 localhost:3000/hi/anita 时,页面显示“Hi anita”,但是当我转到 localhost:3000/hi/bob 时,页面显示“Hi bob”。当然,我不想对 anita 和 bob 做出 Action 。你明白了。任何提示我该怎么做?谢谢!

最佳答案

我会定义路由以首先接受“最后一部分”,然后在操作中检索它:

# in routes.rb
# for rails 3:
match "/hi/:who" => "static#say_hi"
# for rails 2:
map.connect "/hi/:who", :controller => "static", :action => "say_hi"

# in StaticController
def say_hi
@who = params[:who] || "No body" # as the user can use the url "/hi" without the "last part"
end

# in views/static/say_hi.html.erb
Hi <%= @who %>

关于ruby-on-rails - 如何使 Rails View .erb 文件打印 URL 的最后一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3930227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com