2

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

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

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

然后我在 index 操作下的 business_controller 中有以下代码:

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

我的路线文件:

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

基本上,视图看起来不错,但是当我实际更改半径并提交它时,它仍然默认为 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)
4

1 回答 1

2

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

会话 cookie 实现可能如下所示:

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

然后,对于您的视图和表单,您将需要直接引用会话,或@radius = session[:radius]在控制器操作中定义。

于 2013-06-20T06:26:01.253 回答