3

I feel like there's no easy way to do this in rails, but since I'm fairly noob in rails I decided to ask for solutions:

I have a form in a view that contains a single (text) input. How can I specify the form url such that it will do a GET to /something/<input> ?

I know I could:

  1. use custom javascript code
  2. post to an endpoint that would do the redirect

Is there any cleaner way?

(using rails 5.2.1 if relevant)

2 Answers 2

0

I think you could use something like this:

<%= form_tag(route_path method: :get) do %>
  search <%= text_field_tag :search %>
  <%= submit_tag 'Search' %>
<% end %>

It depends on which form helpers you use.

Sign up to request clarification or add additional context in comments.

1 Comment

form_tag is deprecated as stated on m.patrikonrails.com/…
0

Maybe you can try this

In your form:

<%= form_with(url: "path_name/input_field_name", method: :get, local: true) do |f| %>
  <%= f.text_field :input_field_name %>
  <%= f.submit 'Search', input_field_name: nil %>
<% end %>

In your controller:

unless params[:input_field_name].blank?
  @results = ModelName.where('input_field_name iLIKE ?', "%#{params[:input_field_name]}%")
end

1 Comment

This becomes messy when you have nested resources, is there a better way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.