[英]Passing a hash to a controller in Ruby on Rails
Using Geocoder gem, i have a helper method that can show current location. 使用Geocoder gem,我有一个可以显示当前位置的助手方法。 I would like to show users events that belong to their home country and nothing else.
我想向用户展示属于他们本国的事件,而没有其他任何事件。 I can't figure out how to pass the info to the controller.
我不知道如何将信息传递给控制器。
Helper 帮手
def myplace
@myplace = request.location.country
end
Print/Show 打印/显示
<%= myplace %>
I am trying to pass myplace to the controller 我正在尝试将myplace传递给控制器
Controller 调节器
@events = Event.where('start_at > ?', Time.zone.now).where(country: params[:myplace]).order('end_at ASC').limit(4)
Can you please explain to me how i can query events based on user's locale. 您能否向我解释我如何根据用户的语言环境查询事件。
If you want a method to be available to both your views and controllers, it doesn't go in your helper files. 如果您想让视图和控制器都可以使用该方法,则该方法不会出现在您的帮助文件中。 You need to put it in your controller, and make it available as a helper to your views with
helper_method
: 您需要将其放在控制器中,并通过
helper_method
将其用作视图的帮助helper_method
:
class MyController
helper_method :myplace
def my_action
@events = Event.where('start_at > ?', Time.zone.now)
.where(country: myplace)
.order('end_at ASC').limit(4)
end
protected
def myplace
@myplace = request.location.country
end
end
您的控制器有权访问该请求,因此您可以:
@events = Event.where('start_at > ?', Time.zone.now).where(country: request.location.country).order('end_at ASC').limit(4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.