【问题标题】:Ruby on Rails page does not refresh when form is posted发布表单时,Ruby on Rails 页面不刷新
【发布时间】:2023-03-25 03:45:01
【问题描述】:

在下面发布的代码示例中,我试图让页面的用户过滤掉哪些项目应该由用户显示。我可以通过手动修改地址栏中传递的参数来成功过滤结果。但是当我单击页面上的过滤器按钮时,它什么也没做。它应该在按下按钮时使用传递的新参数刷新页面。关于为什么会发生这种情况的任何想法?我需要在控制器中添加任何内容吗?

查看:

= form_tag projects_path, :method => :get do
  Include:
- @all_users.each do |user|
  = user
  = check_box_tag "users[#{user}]", 1, @all_users.include?(user), :id => "users_#{user}"
= submit_tag 'Filter', {:id => "users_submit"}

加载主页后的控制台:

[2014-10-13 23:32:55] INFO  WEBrick 1.3.1
[2014-10-13 23:32:55] INFO  ruby 1.9.3 (2013-06-27) [i686-linux]
[2014-10-13 23:32:55] INFO  WEBrick::HTTPServer#start: pid=2718 port=3000


Started GET "/projects" for 127.0.0.1 at 2014-10-13 23:32:58 -0300
Connecting to database specified by database.yml
Processing by ProjectsController#index as HTML
Project Load (0.2ms)  SELECT DISTINCT user FROM "projects" 
Redirected to http://localhost:3000/projects?users%5BCoulson%5D=1&users%5BFitz%5D=1&users%5BMay%5D=1&users%5BSimmons%5D=1&users%5BWard%5D=1
Project Load (0.2ms)  SELECT "projects".* FROM "projects" ORDER BY id
Completed 302 Found in 9ms (ActiveRecord: 0.4ms)


Started GET "/projects?users%5BCoulson%5D=1&users%5BFitz%5D=1&users%5BMay%5D=1&users%5BSimmons%5D=1&users%5BWard%5D=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Processing by ProjectsController#index as HTML
Parameters: {"users"=>{"Coulson"=>"1", "Fitz"=>"1", "May"=>"1", "Simmons"=>"1", "Ward"=>"1"}}
Project Load (0.2ms)  SELECT DISTINCT user FROM "projects" 
Project Load (0.2ms)  SELECT "projects".* FROM "projects" ORDER BY id
Rendered projects/index.html.haml within layouts/application (11.8ms)
Completed 200 OK in 61ms (Views: 59.3ms | ActiveRecord: 0.4ms)
[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /jquery.js - 304 Not Modified (5ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /application.css - 304 Not Modified (1ms)
[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true
[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /application.js - 304 Not Modified (4ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-10-13 23:32:59 -0300
Served asset /jquery_ujs.js - 304 Not Modified (1ms)

[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

[2014-10-13 23:32:59] WARN  Could not determine content-length of response body. Set  content-length of the response or set Response#chunked = true

单击提交按钮后,控制台中没有任何反应,或者浏览器似乎没有做任何事情。 导轨 3.2.14 Ubuntu 12.04 LTS

【问题讨论】:

  • 您的代码在这里没有正确缩进吗?我的意思是,check_box_tag 在您的实际应用程序中缩进了 form_tag 吗?点击提交后可以粘贴服务器日志吗?
  • 代码在应用程序中的缩进不同,我不确定如何保留它,因为 4 个空格决定了这里的代码块。单击提交时,控制台不会产生任何输出,因此我添加了正在加载的主页以查看是否有帮助。还有其他地方可以获取更详细的日志吗?
  • 代码格式现在显示在文件中。
  • 好的,您的表单正在按照您的指示工作:它正在使用以下参数向您的 ProjectsController 的索引操作发出 GET 请求:{"users"=>{"Coulson"=>"1 ", "Fitz"=>"1", "May"=>"1", "Simmons"=>"1", "Ward"=>"1"}} 所以,我认为问题在于您如何期望控制器中的参数,以及您如何发送它们。当您说您手动修改URL中的参数时,您是怎么做的?检查请求如何在您的手动编辑版本和表单版本中发送参数。清楚吗?
  • 每当我单击过滤器按钮时,它都不会在控制台中显示 GET 请求。我发布的输出是如上所述第一次加载页面。我的地址栏如下所示:localhost:3000/… 如果我删除 &users[Ward]=1 之类的参数之一,该框将被取消选中,并且用户的项目将不会显示在列表中。这是故意的,但是当我取消选中 Ward 框并单击过滤器时,它不会将其删除并使用新参数触发刷新。

标签: ruby-on-rails ruby-on-rails-3.2 haml


【解决方案1】:

使用适当的缩进修复了代码:

= form_tag movies_path, :id => "ratings_form", :method => :get do
  Include: 
  - @all_ratings.each do |rating|
    = rating
    = check_box_tag "ratings[#{rating}]", 1, @ratings.include?(rating), :id => "ratings_#{rating}"
  = submit_tag "Refresh", :id => "ratings_submit"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-18
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2018-09-30
    • 1970-01-01
    • 2017-05-30
    相关资源
    最近更新 更多