passing text_field values in ajx on rails - ruby-on-rails

I'm using
<%= text_field 'person', 'one',:id => 'test', :onchange=>remote_function(:url=>{:action=>"update"}, :update=>"test") %>
<div id="test"></div>
Now I just want to send the value of text_field with :action
i.e :url=>{:action=>"update(value_of_text_field_entered"}
Don't want to use params[:person][:one].
Any suggestions?or how can I use <%= observe_field %> to send the value with action?

You can try this one also:
<%= text_field 'person', 'one' %>
### Paste following code in your javascript
$("#person_one").live("change", function(){
$.ajax({
complete:function(request){},
data:'person_one='+ $(this).val(),
dataType:'script',
type:'get',
url: '/update'route
})
});

maybe you can use a javascript code, "this.value()"

Use
<%= text_field 'person', 'one',:id => 'test' %>
<%= observe_field 'persone_one',
:url=>{:action=>"update"} ,
:frequency => 0.25,
:update=>'test',
:with => 'person_one'
%>
<div id="test"></div>
you get value of textfiled in params[:person_one]

Related

Defaults not set in Rails multiple-select form

In my application, one article can have multiple tags. When creating an article, I am able to add multiple tags to an article using a multiple-select dropdown and the Rails Select2 gem.
The problem is that when I edit the article, none of the tags are selected, and a user has to select them all over again.
<%= form_for(#article) do |f| %>
<%= f.select :tags, options_for_select(#tags.collect {|t| [ t.id, t.title] }),{:selected=>#article.tags}, :multiple => true, :id => "article_tags", :class => "form-control" %><br>
<%= f.submit 'Update', :class => "btn" %><br>
<% end %>
<script>
$(document).ready(function() {
$("#article_tags").select2({multiple:true});
});
</script>
Any suggestions?
This ended up working when I put it at the bottom of the page:
<script>
$(document).ready(function() {
$("#tags").select2({multiple:true});
$('select').select2().select2('val', 'tag one, tag two, tag three');
$('select').select2();
});
</script>

How do you add an extra query string parameter via a form submit in Ruby on Rails?

I would like to be able to add an additional query string parameter on submit that is the same as the value of the classrooms_search_textbox that the user will type. How do I do this?
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
Do I need to add a hidden_tag (and if so, how would I go about doing this?) or can I just add to the classrooms_path somehow?
Thanks!
Since you're sending it your controller first, then you can just manipulate the params in your controller method before sending it off:
params[:classrooms_query] = params[:classrooms_search_textbox]
And then go ahead and use those params to send off to the other service. There's no need to add hidden field tags or use some fancy JS code.
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= hidden_field_tag "classrooms_query" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
$('#classrooms_search_form').submit(function() {
$('#classrooms_query').value($(classrooms_search_textbox.value());
});
That would achieve what you want. Nonetheless, maybe it is in your interest to refactor the controller or view so that it doesn't have this kind of conflicts.

Passing submit input parameter in a AJAX post request

I'm refactoring a form into an AJAX post request.
Here's the existing form:
<%= form_for([#group, #invitation], :remote => true, :html => { :'data-type' => 'html', :class => 'fbinvite_form', :id => friend[:identifier]}) do |f| %>
<%= f.hidden_field :recipient_email, :value => "facebook#meetcody.com" %>
<div class = "fbinvite btn_list_right" id = "<%= friend[:identifier] %>">
<%= f.submit "Invite", :class => "btn btn-medium btn-primary", :name => "fb" %>
</div>
<% end %>
In the controller I have logic that checks for the name of the submit input:
if params[:fb]
...
elsif params[:somethingelse]
...
end
That's the background. I am now trying to replace the above form with the following ajax (added dummy data to make it clearer):
$.post('/groups/7/invitations', {
"recipient_email": "facebook#meetcody.com",
"commit" : "fb",
"fb" : "fb"
}, function(response) {
alert("invite created!");
});
This successfully hits the controller, but doesn't evaluate as true on the condition if params[:fb]. As you can see I tried some really basic ways of passing that parameter in, but it obviously didn't work.
How can I change my ajax request so that params[:fb] evaluates as true?

How to pass collection_select value with link_to_remote in rails?

I want to pass collection_select drop down list value with link_to_remote.
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => true}) %>
<%= link_to_remote 'Show calendar', :url => {:controller => 'calendar', :action => 'trainer_view'} %>
I want to pass the selected trainer_id value to the trainer_view method. How can I do this?
Hi :) I would recommend using jQuery and AJAX to accomplish your goal, this is how I would try to do it:
First, I would drop the <%= link_to_remote %> under the collection_select so it would look like this:
<%= collection_select("event", "trainer_id", #trainers , :id, :name, {:prompt => 'Select a Trainer'}) %>
<div id="trainerCalendar"></div>
Then, put this JavaScript in your application.js, when the DOM is ready:
$('#trainer_id').live('change', function() {
$('#trainerCalendar').html.empty;
$.ajax({ url: '/trainer_view/',
data: 'id=' + this.value,
success: function(data) {
$('#trainerCalendar').html(data);
}
})
});
You can have your controller respond with a partial of the calendar.
Hope this helps!

Rails - I need a checkbox to change a field in the DB

I know I have done this before, but for the life of me I can't figure it out.
I have a table with a "called" field in it. I need to use a checkbox to update the db and check if "called" is "true" or not. Doesn't need to be AJAX, just needs to update the field.
table: rsvp
field: called
Thanks a ton.
A simple approach without ajax could be using a checkbox inside a form and submitting the form with the checkbox javascript onclick event.
Example:
View:
<% form_for #rsvp, :id => "rsvp" do |f| %>
<%= f.check_box :called, :onclick => "$('#rsvp').submit()" %>
<% end %>
this if you are using JQuery... with prototype the onclick string will be:
$('rsvp').submit()
Controller:
#rsvp = Rsvp.find(params[:id])
if #rsvp.update_attributes(params[:rsvp])
# success
else
# fail
end
Reference:
check box
In view:
<% form_for :rsvp, :url => {:controller => "rsvp", :action => "update"} do |f| %>
<%= f.check_box :called %>
<%= f.submit "Update" %>
<% end %>
In rsvp controller, update method:
#RSVPobject.updateAttribute(:called, params[:rsvp][:called])
If you just want to do this just by clicking the checkbox, you need to go the Ajax road.
Try using an "observe_field" in your view.
<%= observe_field ":called",
:frequency => 0.25,
:update => 'feedback_to_user',
:url => {:action => :mark_as_called},
:with => 'called',
:on => 'click' %>
All the details here.
Don't forget to adjust your routes so that the "mark_as_called" action can be found.

Resources