好的,基本上我正在做一个Web应用程序,我希望用户输入一个名为@radius的常量的给定值,我稍后会使用它。
假设我在名为“business / index.html.erb”的视图中有以下代码
<%= 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"
基本上,视图看起来不错,但是当我实际更改半径并提交它时,它仍默认为一个,就好像params [:radius]为零。我缺少什么想法?我确信这是非常基本的。
编辑:
以下是日志文件的一部分,我相信是我的要求
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)
答案 0 :(得分:2)
我认为你误解了@radius
变量以及它是如何工作的。这是控制器的实例变量,因此它将在每个请求之间重置为nil
。如果要在请求之间保留radius值,则需要在数据库或用户cookie中执行此操作。
会话cookie实现可能如下所示:
session[:radius] = params[:radius] || session[:radius] || 1
然后,对于您的视图和表单,您需要直接引用会话,或在控制器操作中定义@radius = session[:radius]
。