简体   繁体   English

如何在表单提交的Rails中动态创建URL

[英]How to make an url dynamically in rails on form submit

I have a form in rails : 我在rails中有一个表单:

<form class="clearfix" method="GET" action="<%= url_for hotels_path %>">
     <input type="text" name="country" />
     <input type="text" name="city" />
</form>

And i want on submit a redirect to the url : 我想提交重定向到url:

/hotels/:country/:city

Where country and city are the values of my fields. 国家和城市是我的领域的价值所在。

How can i manage this, whithout using javascript? 在不使用JavaScript的情况下,我该如何管理?

Thank you 谢谢

You could do the redirect on server side eg like this (untested): 您可以在服务器端进行重定向,例如这样(未经测试):

class HotelsController < ActionController::Base

    def index
       redirect_to hotels_path + "/#{params[:country]/#{params[:city]}"
    end

end

You cant manage this without using javascript because you set the action once while parsing your erb template and when you want to change it based on user action you must use javascript. 您无法使用javascript来管理此问题,因为您在解析erb模板时只设置了一次action ,并且当您要根据用户操作对其进行更改时,您必须使用javascript。 Thats it! 而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM