I have this code for a link that evaluates whether or not there are tags to determine what the link text should say:
<%= link_to "#{(#video.topics.count == 0) ? 'Add descriptive topics so people can find your song!' : 'Edit Topics'}", '#', :id => 'edit_topics_link', :class => 'edit' %>
The problem is that my tagging system updates completely with AJAX, and this link text does not, so it is only updated to the correct link text after a page refresh. How can I make the text update with AJAX?
You can do this easily using jQuery!
Just fetch the update data from the server (using $.get or $.post) and then update the link using the text() function. Here's an example:
$.get('http://some.url',{},function(response) {
$('#edit_topics_link').text(response);
}
Pretty simple, isn't it?
Related
Rails newbie here...please be kind...
I have an app that generates a unique paragraph of text (sampled from arrays and made unique based on criteria entered in _form.html.erb, rendered from the criteria/new page). On the criteria/show page, I have 2 links, one to generate a new unique paragraph using the same criteria, and one to return to the form and edit the criteria.
For the second link, I want to retain the criteria previously entered, as the user may want to only change one entry and not have to re-enter all of it. I've found plenty of information regarding ajax calls, respond_to, remote: true, but haven't found this exact answer or if I have, my brain is TIRED and failed to comprehend.
I've seen the suggestion in a similar question (How to come back to form without reset the values?) which talks about repopulating the data: "Just add the parameters (/myForm?param1=1¶m2=2¶m3=3) to the button url that leads back to the search form. Then populate the fields using the parameters. No need to use session variables." Sadly, I'm unclear about how to do implement this.
Would someone please be so kind as to walk me through either (preferably the simplest!) way of doing this?
Current links on show page (commented things are those that I tried unsuccessfully:
<%= link_to 'Generate Another!', criteria_path %> #regenerates text with current criteria
<%= link_to 'Enter New Information', new_criterium_path %> #this is the link I'm trying to change
<%#= link_to 'Enter New Information', new_criterium_path, :remote => true %>
<%#= link_to 'Enter New Information', { :controller => 'Criteria', :action => "new" } :remote => true %>
Controller new action (commented until I can make it work):
def new
#criterium = Criterium.new
#testing ajax stuff
# respond_to do |format|
# format.html
# format.json
# end
end
There's lots of talk about needing a new.js.erb file (or would that be show.js.erb?) and a div tag in my show(?) page but again, brain TIRED. I've attempted some of these things, then deleted from the code so as not to gak things up too much and be unable to remember what I did.
Please tell me what else you need from me in order to answer. Many thanks in advance!
Looks like you need to pass back params to your 'new' action, and then use them when instantiating Criterium:
<%= link_to 'Enter New Information', new_criterium_path(criteria: params[:criteria]) %>
def new
#criterium = Criterium.new(criteria: params[:criteria])
end
In my homepage, I have an input box that user can type in a search query. Then I have a link_to which will make a get request to a different page (search page) with the search query. By design, I can't use Rails form_for.
How can I insert my query dynamically to "link_to" after detecting a change in my input box?
Here is a mock:
%input{:type => "text", :id => "my_input"}
= link_to "Search", posts_path(query: "my_query_here")
:javascript
$(function(){
$("#my_input").change(function() {
... do something here ...
});
});
Thank you.
You can't. Your javascript can't hack into your server-side code. However, it can hack the result of your server-side code, which is HTML.
To make sure the query for your posts link matches properly, you should add a specific ID to your <%= link_to %>:
<%= link_to "Search", posts_path, :id => "search_link" %>
Then your javascript should be like:
$("#my_input").change(function() {
$("#search_link").attr("href","/posts?q=" + encodeURIComponent( $(this).val() ) );
});
Thanks to user #beck03076 for suggesting that you could simply execute a location change on click rather than updating the link every time the input changes:
$("#link").click(function() {
$(this).attr("href", "/posts?q=" + encodeURIComponent($("#my_input").val()));
});
Depending on what exactly you want to do, this may not exactly match your expectation. For example, if you have some other code that inspects the link URL to update something else on the page, this second method won't work. If, on the other hand, all you want is to execute a search on click, this will be much more efficient.
I want to add auto-complete to my search bar. But when I submit the completed user I get this url :
/users?utf8=✓&search=Thomas
Instead of /users/23
My controller
#users = User.find(:all, :select=>'name').map(&:name)
The Javascript
<%= javascript_tag "var autocomplete_items = #{ #users };" %>
<script type="text/javascript">
jQuery(document).ready(function() {
$('#auto_complete').typeahead({source: autocomplete_items});
});
</script>
My form
<%= form_tag users_path, :method => :get do %>
<%= text_field_tag :search, params[:search], :id => "auto_complete" %>
<% end %>
Thanks for your help
From what I can glean from the Bootstrap docs, typeahead is only designed to do just that - type ahead of text. It just takes your input, sees if there's something in its source that matches the input and offers some alternatives for completing the input. If the user chooses an alternative, typeahead fills in the selected string.
So the only thing you get is a string in an input field.
Typeahead does not know how to construct a link like the one you need: /uses/23
Event if Bootstrap typeahead knew how to do that, you haven't provided it with the needed information since you're only providing it with the names of the users.
I think what you want to do is:
Send both user names and user paths to the view
Use some kind of autocomplete widget that can trigger a callback when an item is selected.
Use the callback to navigate to the selected user path when a user is selected
I'm not entirely familiar with Bootstrap's typeahead - you may be able to do this stuff with it.
But I argue for using something that's better documented, like jQuery UI:s autocomplete.
This is the code i'm using to get value of the checkbox & set the flag accordingly,
:checkbox, :collection => ["No, thanks", "Yes, of course!"] %>
<% if (post.content == "true") %>
<%= #flag=1 %>
<%else %>
<%= #flag=0 %>
But i'm not getting the new value of the checkbox (value changes by clicking on it) without refreshing a page
The changing check box is client-side... the server (rails and your erb template) knows nothing of it changing. You will probably want to use a client-side technology like javascript to watch for the change and manage the behavior you are looking for. I would likely use jQuery to do this -- I'd help with the jQuery, but I'm a little unsure of what you are up to. Hope this helps.
I'm about to finish the final part of my thread messaging system for users. All deletion works great however before I move on to my next feature I'd like to give users the ability to delete selected messages.
Here's a way I've thought of doing it so far.
Add a check box tag to the each loop that loops through each message.
Have a "delete selected" link that goes to my messages controller "destroy_selected_messages" action.
What I need to do is some how grab an array of all the selected messages id's. Then pass it to the path as an argument. The delete all links path.
<%= link_to 'Delete Selected', messages_destroy_selected_messages_path(ARRAY_WITH_IDS), :method => :delete, :confirm => "Are you sure?" if #current_thread_messages.any? %>
This delete selected link won't be part of the loop because I don't want it showing for every message but at the top of the thread instead.
I need to figure out how to pass the array with all the selected messages ideas into that argument. How do I get them from the each loop without going into my messages helper and writing some funky method.?
I have the checkbox tag e.g. check_box_tag ... how do I setup an empty array and then so I can pass in the messages id? e.g.:
<%= check_box_tag ......., :value => message.id &>
Help would be appreciated. I looked at an old screencast in railscasts but it's from 2007 I think.
Kind regards
You can make name="message_ids[]" for multi select inputs. It will get passed as an array through HTTP server to your params[:message_ids].
From the HTML side of the problem, I think that form helper <%= check_box_tag "message_ids[]", :value => message.id %> should suffice.
In the controller action, log the params[:message_ids] and look it up, it should be an Array.