简体   繁体   English

如何提交form_tag以更新当前页面?

[英]How can I submit a form_tag to update the current page?

I am new to coding and Rails. 我是编码和Rails的新手。 I and have worked through the Rails Tutorial by Michael Hartl, but I am still very raw. 我曾经通过Michael Hartl编写过Rails教程,但是我仍然很生。 I am hoping someone can explain this to me or point me to a resource. 我希望有人可以向我解释这一点或为我提供资源。 My Google skills were failing me. 我的Google技能使我失望。

When a user lands on my homepage I am generating a random password for them. 当用户登陆我的主页时,我正在为他们生成一个随机密码。 But if they want to generate a new password I'd like to provide them with some options. 但是,如果他们想生成一个新密码,我想为他们提供一些选择。 I started with capitalizing a random letter. 我从大写随机字母开始。

<%= form_tag(root_path, method: 'get') do %>
  <%= check_box_tag(:random_capital) %>
  <%= label_tag(:random_capital, "Capitalize a random letter") %>
<%= submit_tag("Generate a new password") %>
<% end %>

I have the above form on my homepage and when I tick the checkbox I do manage to get a value of 1 passed through an instance variable and can display that value back to my homepage along with a new password 我的主页上有上面的表格,当我勾选复选框时,确实设法通过实例变量获得了值1,并且可以将该值以及新密码显示回我的主页

But this code in my controller action is never triggering 但是我的控制器操作中的此代码永远不会触发

  if @randomness == 1
    random_capital words
  end

So I get a new password when I submit the form, but not with a random capital. 因此,我在提交表单时会得到一个新密码,但是没有大写字母。

Where am I going wrong? 我要去哪里错了?

You need to check params[:random_capital] , not @randomness . 您需要检查params[:random_capital] ,而不是@randomness I would also advise to move this logic to a separate controller. 我也建议将此逻辑移至单独的控制器。

Turns out the error was in the following code 原来错误是在以下代码中

if @randomness == 1

I did not realize the value of params[:random_capital] was a string, not an integer. 我没有意识到params [:random_capital]的值是字符串,而不是整数。

if @randomness == '1'

fixed it. 修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM