gpt4 book ai didi

ruby-on-rails - 初学者 rails : submit_tag issue

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:36 24 4
gpt4 key购买 nike

好吧,基本上我正在做一个 Web 应用程序,我希望用户为名为 @radius 的常量输入一个给定值,我稍后将使用它。

假设我在名为“business/index.html.erb”的 View 中有以下代码

<%= form_tag root_path do %>
<%= number_field_tag :radius %>
<%= submit_tag "add" %>
<% end %>

然后我的 business_controller 中的索引操作下有以下代码:

@radius= params[:radius] || @radius || 1

我的路线文件:

root :to => 'business#index'
post "business/index"

基本上, View 看起来不错,但是当我实际更改半径并提交它时,它仍然默认为 1,就好像 params[:radius] 为 nil 一样。对我所缺少的有什么想法吗?我敢肯定这是非常初级的。

编辑:

这是日志文件的一部分,我相信这是我被要求的

Started POST "/" for 127.0.0.1 at 2013-06-20 02:00:31 -0430
Processing by BusinessController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CaD40O9gAedbeDepeDHtrMNNWzD/uBznHZ30ZGy+c6Q=", "radius"=>"2", "commit"=>"add"}
[1m[35mBusiness Load (18.0ms)[0m SELECT businesses.*, 6371.0 * 2 * ASIN(SQRT(POWER(SIN((10.5083644 - businesses.latitude) * PI() / 180 / 2), 2) + COS(10.5083644 * PI() / 180) * COS(businesses.latitude * PI() / 180) * POWER(SIN((-66.83536699999999 - businesses.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(businesses.longitude - -66.83536699999999), RADIANS(businesses.latitude - 10.5083644))) + 360 AS decimal) % 360 AS bearing, ( businesses.reviews_count / (nullif(businesses.reviews_count + MAX(businesses.reviews_count) OVER(),0)) ) * businesses.rating + (MAX(businesses.reviews_count) OVER() / (nullif(businesses.reviews_count+MAX(businesses.reviews_count) OVER(),0))) * AVG(businesses.rating) OVER() as ranking FROM "businesses" WHERE (businesses.latitude BETWEEN 10.490377967881624 AND 10.526350832118375 AND businesses.longitude BETWEEN -66.85366024099285 AND -66.81707375900713 AND 6371.0 * 2 * ASIN(SQRT(POWER(SIN((10.5083644 - businesses.latitude) * PI() / 180 / 2), 2) + COS(10.5083644 * PI() / 180) * COS(businesses.latitude * PI() / 180) * POWER(SIN((-66.83536699999999 - businesses.longitude) * PI() / 180 / 2), 2))) <= '2') GROUP BY businesses.id ORDER BY distance ASC, ranking DESC
Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/gmaps4rails-1.5.6/app/views/gmaps4rails/_gmaps4rails.html.erb (1.0ms)

最佳答案

我认为您误解了 @radius 变量及其工作原理。这是 Controller 的一个实例变量,因此它将在每次请求之间重置为 nil。如果您想在请求之间保留半径的值,您需要在数据库或用户 cookie 中执行此操作。

session cookie 实现可能如下所示:

session[:radius] = params[:radius] || session[:radius] || 1

然后,对于您的 View 和表单,您需要直接引用 session ,或者在您的 Controller 操作中定义 @radius = session[:radius]

关于ruby-on-rails - 初学者 rails : submit_tag issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17206447/

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