sinatra 中的错误数组

发布于 2024-12-18 00:59:39 字数 1073 浏览 1 评论 0原文

为了管理地址簿应用程序中的错误,我初始化了一个这样的数组

err = Array.new

,然后当我发布内容时它会检查是否有空字段。如果是,对于每个空字段,它在数组中添加一条记录,然后重定向到 /add 页面,像这样,

post '/' do
if params[:fname] == ""
    err.push "Insert a valid first name"
end
if params[:lname] == ""
    err.push "insert a valid last name"
end
if params[:phone] == ""
    err.push "insert a valid phone number"
end
if params[:mail] == ""
    err.push "insert a valid e-mail address"
end
if err.empty?
    c = Contatto.new
    c.fname = params[:fname]
    c.lname = params[:lname]
    c.phone = params[:phone]
    c.mail = params[:mail]
    c.save
    redirect '/'
else
    redirect '/add'
end
end

然后添加页面读取数组是否有任何记录,如果是,则循环它以打印每条消息,

get '/add' do
  @err = err
  @title = 'Aggiungi'
  erb :aggiungi
end



<% if @err.any? %>
<div class="error">
    <% @err.each do |err| %>
        <%= err %><br>
    <% end %>
</div>
<% end %>

我认为错误是每次它从 post '/' 更改为 get '/add' 时都会重新初始化数组,因此结果是一个空数组...... 我该如何解决?谢谢大家!

To manage errors in my address book app i initialize an array like this

err = Array.new

and then when i post something it checks if there are empty fields. If yes, for each empty field it adds a record in the array, and then redirect to /add page, like this

post '/' do
if params[:fname] == ""
    err.push "Insert a valid first name"
end
if params[:lname] == ""
    err.push "insert a valid last name"
end
if params[:phone] == ""
    err.push "insert a valid phone number"
end
if params[:mail] == ""
    err.push "insert a valid e-mail address"
end
if err.empty?
    c = Contatto.new
    c.fname = params[:fname]
    c.lname = params[:lname]
    c.phone = params[:phone]
    c.mail = params[:mail]
    c.save
    redirect '/'
else
    redirect '/add'
end
end

then the add page reads if the array has any record and if yes, cycles it to print each message

get '/add' do
  @err = err
  @title = 'Aggiungi'
  erb :aggiungi
end



<% if @err.any? %>
<div class="error">
    <% @err.each do |err| %>
        <%= err %><br>
    <% end %>
</div>
<% end %>

i think the error is that it re-initialize the array every time it changes from post '/' to get '/add' and so the result is an empty array...
How can i solve? thank you everyone!

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

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

发布评论

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

评论(2

可是我不能没有你 2024-12-25 00:59:39

如果您希望特定访问者的数据在请求之间保留,您需要将错误数组存储在会话或 cookie 中(会话可能最有意义)。

幸运的是,Sinatra 中的会话非常简单: http://www.sinatrarb.com/intro#Using%20Sessions 。启用后,您可以将几乎任何您想要的内容放入会话哈希中,因此使用 session[:errors] = [] 进行初始化并使用 session[:errors] << “一个错误”应该给你你正在寻找的持久性。

If you want data for a specific visitor to persist between requests you need to be storing the error array in either a session or a cookie (session probably makes the most sense).

Luckily sessions in Sinatra are pretty easy: http://www.sinatrarb.com/intro#Using%20Sessions . Once enabled you can put pretty much anything you want into the session hash, so initializing with session[:errors] = [] and pushing with session[:errors] << "An error" should give you the persistence you are looking for.

や莫失莫忘 2024-12-25 00:59:39

您正在创建一个局部变量并期望它在请求之间持续存在。这应该如何发生? Sinatra 没有通灵能力,它只会记住你告诉它记住的内容,而这通常是通过某种数据库或客户端 cookie 来完成的。

通常,您应该在失败时呈现一个响应页面,利用您收集的错误,或者在成功时重定向,其中空错误数组不相关。

作为一种风格问题,执行此类操作的更 Ruby 方式是:

err = [ ] # Equivalent to Array.new

err << "Example error" # Equivalent to err.push

You're creating a local variable and expecting it to persist between requests. How is this supposed to happen? Sinatra is not psychic, it will only remember what you tell it to remember, and that's usually done through some kind of database or a client-side cookie.

Generally you should render a response page on failure, making use of the errors you've collected, or redirect on success, where the empty errors array is not relevant.

As a matter of style, the more Ruby way to do things like this is:

err = [ ] # Equivalent to Array.new

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