How to clear the search results in a Rails app? - ruby-on-rails

I'm a Rails noob, and am looking for assistance in clearing the results of a search.
I've built a sample application which lists cafes, and have implemented searching using elasticsearch and searchkick. The search function appends parameters to the URL as in http://localhost:3000/cafes?search=sydney&commit=Search and is working correctly.
Now I'd like to add a button to clear the search results:
The Clear button's link is to the cafes_path (http://localhost:3000/cafes), so it basically reloads the page without the search parameters:
= form_tag cafes_path, :method => :get do
= text_field_tag :search
= submit_tag value = "Search"
= link_to cafes_path
Is that an acceptable way to clear the search?
Is it an acceptable Rails approach to use JavaScript logic to enable/disable the Clear button, based on what's in the search box?
Please let me know if you need to see more of the model or controller code to make sense of the question.

This way is pretty standard
sure, use javascript - or you can also google for other ways it's been done and see what they did.

Related

Use data-disabled-with on a non form submit button

Rails provides data-disabled-with for button tags on form submits. Super awesome. However, link tags don't get this desired behavior. They're great to prevent users from clicking a button too many times and producing an unwarranted effect.
Is there a way I can do something like:
Purchase me
Rails provides this functionality via the :disable_with parameter of the link_to helper. See docs.
For example:
link_to("Create", create_post_path(#post), remote: true, disable_with: "Creating...")
Recognizing of course that creating a resource via a GET request isn't idiomatic Rails/REST... but this hopefully illustrates how it could be used.

good tutorial on how to use auto_html filters for rails?

I know it's supposed to be easy, but I just can't figure out how to modify an existing auto_html filter to get the output I need. I found this Creating filters for auto_html
but I need to modify an existing filter.
for example, I have a list of youtube links in the db in this format (http://youtu.be/UfQC1h-EANI) and printed out in my view:
%li
= link_to link.title, link.url, :class => "youtube title_link"
When the the link is clicked, I need the http://youtu.be/UfQC1h-EANI converted to http://www.youtube.com/embed/UfQC1h-EANI?rel=0
I've managed to get a conversion working using auto_html, however, it's coming with all of the html attached to it. I'm thinking if I can modify the filter, I can adjust it to just give me the url without all the html. I can't for the life of me figure out how to modify auto_html's youtube filter. Is this even the best approach for this?
Any help is muchly appreciated!
Just do it with regex
link_to link.title, "http://youtube.com/embed/#{link.url.to_s.match(/\/\/youtu.be\/(\S+)$/)[1]}/?rel=0", :class => "youtube title_link"
If you want it onclick only, you can do it in JavaScript.

Rails Link_To doesn't add a CSS Class correctly

So I'm trying to use link_to to create a link in my Rails app and trying to add a CSS class to certain links. The problem that I have is that when I add my html options to the link_to arguments, the links don't get created and I end up with nothing. Here's my code:
<%=link_to( image_tag(#beetle.icon_img, :width=>30, :alt=>"Beetle", :border=>0) , beetle, :html=>{:class=>"work"}) %>
I also tried variations of this and it still didn't work. For example,
<%=link_to( image_tag(#beetle.icon_img, :width=>30, :alt=>"Beetle", :border=>0) , beetle, :class=>"work") %>
The method also exhibits some strange behavior, which I think might be the culprit. If I go straight to the page, no POST or GET requests, link_to works properly and the links and images render correctly, which is to say that they actually DO render. However, the way that I would like to get to the page is by form POST request in a previous page whose action is the results page I'm trying to get to.
Thanks for any help you can provide!
EDIT: I'm not sure exactly what the problem was, but I solved it by changing the form's method to GET instead of POST.
It's most likely because the POST request is hitting a different method (new), rather than (show). You must supply the proper instance variables to the view. You seem to be referencing both #beetle and beetle. Have a look at all of those variables, since that appears to be the POST problem.

Rails 3.0 . How to pass the select box value through link_to

I knew that Rails3 no longer supporting link_to_remote.... instead of that we can modify
the link_to method by using different syntax as follows :
link_to "Send",{:action =>"send_mail",:question_id =>question.id},:remote => true,:class =>"button small"
My problem is, in my view i keep the select box which contains the list of user's name near by send link button (which user the above syntax)... i want to pass the selection box value to link_to when user click the send button
Here is my View code :
"send_mail",:question_id =>question.id,:user_value
=>encodeURIComponent($F('user_id'))},:remote => true,:class =>"button small" %>
I don't know how to achieve this ....can any one please suggest me on this.
edit: ok now is see your last comment... never mind
What I got from the question, you are looking for something like this:
http://marcgrabanski.com/articles/jquery-select-list-values
It's not really a Rails problem since you can't change the Ruby code from within the rendered HTML (at least that would be very risky if it's possible). The code from above can be easily changed to your needs so that the user gets redirected to the URL that matches the button value.

Rendering field data as a link in Ruby on Rails

Ok, I think this is probably an easy question but for the life of my I can't figure it out. I have created a table called ugtags and in that table I have two columns (beyond the basics), 'name' and 'link'.
I am trying to allow a user to add a link to a page. Ideally they would enter the link title (name) and the url (link) and in the view it would display the title as a link to the url that was entered in the link column.
I there a way to do it by simply affecting the <%= link_to h(ugtag.name) %> code?
You should just be able to do:
<%= link_to h(ugtag.name), ugtag.link %>
See the documentation for all of the relevant options.

Resources