这个 ruby​​ 语句有什么问题?

发布于 2024-09-28 10:19:14 字数 290 浏览 4 评论 0原文

当我连接到我自己的服务器(本地)时,我试图做一些事情。 我从网站上找到了 request.env,因此我使用该数组来比较我的 IP。

<%
if request.env['HTTP_HOST']!="127.0.0.1" 
     puts request.env['HTTP_HOST']
else
     puts "its Local!"
end 
%>

当我在 Rails3 中运行上面时,我没有打印任何内容...... 我是 ruby​​&rails3 的新手..

I am trying to do something when I am connecting to my own server(local).
I found request.env from the website, so I am using that array to compare my IPs.

<%
if request.env['HTTP_HOST']!="127.0.0.1" 
     puts request.env['HTTP_HOST']
else
     puts "its Local!"
end 
%>

When I run above in rails3, I get nothing printed...
I am new to ruby&rails3..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

半寸时光 2024-10-05 10:19:14

当您希望在网页中输出时,请使用 <%= %>,而不是 <% %>。输出将是表达式的返回值,因此您不需要 puts

<%= 
   if request.env['HTTP_HOST']!="127.0.0.1"  
     request.env['HTTP_HOST'] 
   else 
     "its Local!" 
   end  
%> 

请注意,您还可以使用 local? 方法而不是直接检查环境。

<%= 
   if request.local?
     "its Local!"
   else 
     request.env['HTTP_HOST']
   end  
%> 

如果您喜欢简洁,您可以将其作为一行完成:

<%= if request.local? then "its Local!" else request.env['HTTP_POST'] end %>

为了使视图更加简洁,请使用辅助方法:

<%= ip_or_local %>

在您放置的匹配视图辅助程序中的位置:

def ip_or_local
   if request.local?
     "its Local!"
   else 
     request.env['HTTP_HOST']
   end  
end

对于这个简单的情况,它可能有点过分,但一般来说,当您开始看到在您看来有很多代码,是时候考虑在助手中隐藏某些内容了。

When you want output in the web page, use <%= %>, not <% %>. The output will be the return value of the expression, so you don't want puts.

<%= 
   if request.env['HTTP_HOST']!="127.0.0.1"  
     request.env['HTTP_HOST'] 
   else 
     "its Local!" 
   end  
%> 

Note that you can also use the local? method instead of checking environment directly.

<%= 
   if request.local?
     "its Local!"
   else 
     request.env['HTTP_HOST']
   end  
%> 

If you like conciseness you can do it as one line:

<%= if request.local? then "its Local!" else request.env['HTTP_POST'] end %>

For even more view conciseness, make use of a helper method:

<%= ip_or_local %>

where in the matching view helper you put:

def ip_or_local
   if request.local?
     "its Local!"
   else 
     request.env['HTTP_HOST']
   end  
end

For this simple case, it may be overkill but in general when you start seeing lots of code in your view, it's time to think about hiding certain things in helpers.

全部不再 2024-10-05 10:19:14

在这种情况下,puts 将写入服务器,而不是响应。所以你应该在服务器的日志中查找你的消息。

puts will write to the server in this case, not to the response. so you should look for your message in the log of the server.

不美如何 2024-10-05 10:19:14

您想打印到日志还是视图?

如果您将内容分解为单独的 erb 标签,可能会更清楚。

<% if local? %>
  <%= "Text for local" %>     
<% else %>
  <%= "Text for remote" %>    
<% end -%>

您需要使用 <%= %>;您想要打印的行的标签和 <% %>您想要逻辑的行的标签,例如条件。

如果您是 Rails 新手,您应该查看从头开始的 Peepcode Rails 视频,它们非常便宜,并且许多 Rails 开发人员都基于其中的内容构建了自己的基础。 Railscast 也是零食大小的小教程,可以轻松帮助您完成许多基础知识。

推荐阅读:

红宝石:
红宝石镐
ruby 方式

Rails:
铁路方式
Head First ruby​​ on Rails

希望我能有所帮助。

Do you want to print to the log or to the view?

It might be clearer if you break things up into seperate erb tags.

<% if local? %>
  <%= "Text for local" %>     
<% else %>
  <%= "Text for remote" %>    
<% end -%>

You need to use <%= %> tags for lines you want printed and <% %> tags for lines that you want logic in, like conditionals.

If you're new to rails, you should check out the peepcode rails from scratch videos, they're pretty cheap and a lot of rails developers built their base on what's in them. Railscasts are also snack sized little tutorials that will easily get you through a lot of the basics.

Recommended reading:

RUBY:
The ruby pickaxe
The ruby way

Rails:
The Rails way
Head first ruby on rails

Hope I was of some help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文