简体   繁体   中英

How to make an url dynamically in rails on form submit

I have a form in 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 :

/hotels/:country/:city

Where country and city are the values of my fields.

How can i manage this, whithout using 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. Thats it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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