remote_function keeps adding authenticity token on GET requests - ruby-on-rails

I got the problem similar to this post here: https://rails.lighthouseapp.com/projects/8994/tickets/106-authenticity_token-appears-in-urls-after-ajax-get-request
routes.rb
map.namespace(:admin, :active_scaffold => true) do |admin|
admin.resources :regions, :shallow => true do |region|
region.resources :birds, :collection => {:search => :get}
end
end
view
<%= javascript_tag %Q(
#{remote_function(:update => 'bird_search', :url => search_admin_region_birds_path(#region.id), :method => :get)}
) %>
It displays url like:
http://localhost:3000/admin/regions/7/birds/search?authenticity_token=F43BcQUM4z3bl7s21kLZQrqwGkuErF7C9jiNMKFTZTo%3D
which should be:
http://localhost:3000/admin/regions/7/birds/search
Without this working my Ajax pagination won't work... help!

what version of rails are you using?
that ticket says it was closed out, maybe you are on earlier version
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001653
the example output does not have the auth token

Fixed this by using Javascript instead of using RJS.
Many times RJS methods aren't very dependable when your apps get more complicated, so take care there.
Anyway for this problem I changed the code to:
<%= javascript_tag %Q(
new Ajax.Updater('region_birds', '#{of_region_admin_region_birds_path(#region.id)}', {asynchronous:true, evalScripts:true, method:'get'});
) %>

Related

Rails 4, Route with multiple ids

I want to make a route like this /p/:id/:ph_id. and the :ph_id also has limitation on length /\d{7}/
This is what I have for the first id
scope '/p', :controller => 'people' do
scope '/:id', :id => /\d{7}/ do end
end
In your config/routes.rb try following code:
get "/p/:id/:ph_id" => "people#show", as: :my_route, id: /\d{7}/, ph_id: /\d{7}/
You can use it like:
<%= link_to "Check this link!", my_route_path(id: 1234567, ph_id: 7654321) %>
Hope that helps! Good Luck!

Can't use link path

Hello i'm new one in ruby on rails. I faced with strange behavior. I have in my routes
get 'diary/add_record', to: 'diary#add'
I add link
<%= link_to 'Добавить запись', diary_add_record_path, :remote => true, :'data-type' => 'html', :id => 'new-record-link' %>
and get this
undefined local variable or method `diary_add_record_path'
But when i use different route (main diary) it works fine. Can anyone tell me what wrong with it.
There are two options
i) add a custom route name and use it
get 'diary/add_record', to: 'diary#add', :as => add_diary
your link becomes
<%= link_to 'Добавить запись', add_diary_path, :remote => true, :'data-type' => 'html', :id => 'new-record-link' %>
ii) Do rake routes and find out the route rails generated for your path
rake routes | grep 'add_record'
and use that in your link

Problem with jQuery in rails

How create a link_to_remote in a jQuery script where url need a parameter that is a javascript variable.
I need create a link_to_remote in pure jQuery.
Thanks in advance
You'll want to use the :with parameter with link_to_remote:
link_to_remote( args[:title],
:update => args[:update],
:url => { :action => args[:action],
:id => id,
:starting => args[:starting]
},
:with => "'filter[viewer_id]=' + $('filter_viewer_id').value",
:loading => "Element.hide('#{args[:update]}');Element.show('#{args[:loading]}')",
:complete => "Element.show('#{args[:update]}');Element.hide('#{args[:loading]}')" )
Notice how I am sending the filter_viewer_id by getting it's value from a form field with jQuery. If you don't need that level of detail, just pass the name of your javascript variable.
Like this:
<%= link_to_remote('Hello', :url => "/test?id='+ id +'&test=true") %>
This will result in:
# => Hello
Very little info on your post. Are you using Rails 3? If so, did you check the jquery-rails gem out?
Best regards,
-- J. Fernandes

Ruby on Rails: form_for works, but remote_form_for doesn't

<% remote_form_for(#media, :url => url_for(:controller => :attachments,
:action => :add_from_disk, :object_id => #object),
:html => { :multipart => true, :id => 'new_media', :onsubmit=>'unsaved_changes = false' } ) do |f| %>
but if I change the remote_form_for to form_for, I don't get this error:
ActiveRecord::RecordInvalid (Validation failed: Document file name must be set.):
Why would it work with form_for, but not the AJAX version?
You can't upload a file using AJAX.... You'll need to implement a flash uploader to send files in the background. It's not pretty, but Uploadify is pretty cool
Recommend keeping file uploading as regular form for.
Or you can use the jQuery form plugin, which works great. In this case you'd do a normal form_for, and inside of your javascript file (once you've included jquery and the jQForm lib in your html) you'd do something like this:
$(function() {
$("#myFormDomID").ajaxForm({ iFrame : true });
});

Auto_complete_for question

I've recently installed this plugin, and I meant to create a Tag field with it, like StackOverFlow does.
When I put the following syntax on my AnnouncementsController(I want to tag announcements) it works great:
auto_complete_for :announcement, :title
protect_from_forgery :only => [:create, :delete, :update]
Also, I had to add the routes syntax as well to make it work:
map.resources :announcements, :collection => {:auto_complete_for_announcement_title => :get }
Now, when I try to accomplish the same with the tags, at the time I create a new announcement, I simply replace the word "announcement" for "tag" and "title" for "name", and it won't work. Tag makes reference for my Tags table at the database.
The error says the following:
<h1> ActiveRecord::RecordNotFound
in AnnouncementsController#show </h1>
<pre>Couldn't find Announcement with ID=auto_complete_for_tag_name</pre>
Can anybody tell me what I'm doing wrong?
Thanks,
Brian
In your view you probably want to change:
<%= text_field_with_auto_complete :announcement, :title %>
to:
<%= text_field_with_auto_complete :tag, :name %>
to make it work, take another look at the error it's giving, it's still calling announcement.
--- edit:
from autocomplete source:
def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
Well, I finally got the answer to my problem.
I was missing the following at routes.rb:
map.auto_complete '/:controller/:action',
:requirements => { :action => /auto_complete_for_\S+/ },
:conditions => { :method => :get }
My new question now it works is the following:
What if I wanted to multi-tag an announcements, for example: "Ruby, C#". Should I change the plugin's logic or is there a functionality to make this work? Cause right now, it will check for the text_field text, not discriminating a new word after a comma or any kind of separator.
Thanks,
Brian

Resources