json autocomplete wrong url in rails - ruby-on-rails

I'm using jquery autocomplete on several places of my app, including in a form with url :
http://www.fonsiuris.net/fr/administration/actes/nouveau/122
app/views/administration/actes form has this field:
<%= lieux.text_area :place, :value => params[:acte] ? #place["place"] : "", class:"field_places", data: {autocomplete_source: lieux_enum_path} %>
in routes.rb:
get 'lieux/enum' => 'lieux#enum'
This is the very first url of my routes.rb
However, when I type letters in the text area, such as 'Dou' that has the autocomplete function, it points to this url :
http://www.fonsiuris.net/fr/biblios/trouver?utf8=✓&ch_bib[aut_titre_cherche]=Douai&commit.x=6&commit.y=2&term=Dou
and when I type in more than one term, that term gets added to the url like this :
/fr/biblios/trouver?utf8=%E2%9C%93&ch_bib%5Baut_titre_cherche%5D=Douai&commit.x=5&commit.y=11&term=Paris
(both Douai and Paris are added as search terms)
It should point to this url:
http://www.fonsiuris.net/lieux/enum?term=Dou
Which gives the right result. I'm using the same code, with the same javascript in several places of my application and it's working fine on all other places !
The output of rails routes | grep enum is :
lieux_enum GET /lieux/enum(.:format) lieux#enum
motclefs_enum GET /motclefs/enum(.:format) motclefs#enum
actes_enum_acte_mot GET /actes/enum_acte_mot(.:format) actes#enum_acte_mot
biblios_enum_rec GET /biblios/enum_rec(.:format) biblios#enum_rec
administration_archives_enum GET /administration/archives/enum(.:format) administration/archives#enum_archive
administration_biblios_enum GET (/:locale)/administration/biblios/enum(.:format) administration/biblios#enum_titres {:locale=>/fr|en|nl|it/}
administration_auteurs_enum GET (/:locale)/administration/auteurs/enum(.:format) administration/auteurs#enum_noms {:locale=>/fr|en|nl|it/}
What I have tried so far :
Reoganising routes.rb
rewriting the form.html.erb so it contains exactly the same code as the other places of my app where this does work
use the same javascript everywhere
reinstall jquery and jquery-ui : now using cdn instead of the gem - doesn't make any
difference.
I don't know where to start looking to resolve this.

I'm going to provide a slightly more articulate answer based on what I garnered from #thiebo.
<%= lieux.text_area :place, :value => params[:acte] ? #place["place"] : "",
class:"field_places", data: {autocomplete_source: lieux_enum_path} %>
This field appears twice with the same field name. Delete the field without the autocomplete member on the data attribute.

For the sake of posterity, the problem had nothing to do with rails, routes or jquery. I had put elsewhere in the form another field with the same class name as the field on which I put the autocomplete.

Related

Invalid route name for GET and POST

I'm working through an older tutorial that was done for Rails 3. I'm using Rails 4.1.4.
One of the instructions is to change the routes file to include the following:
get '/boards/:board_id/conversations/:id/reply' => "conversations#reply", :as => :reply_board_conversation
post '/boards/:board_id/conversations/:id/reply' => "conversations#save_reply", :as => :reply_board_conversation
Obviously that gives me an error:
Invalid route name, already in use: 'reply_board_conversation'
It seems to me that the route is somehow trying to replicate the behaviour of a new and create action. Get for new and Post for create with a single route.
The problem is I can't figure out how to rewrite the route so it works. I've googled for solutions but can't seem to find anything. If anyone could point me in the right direction it would be hugely appreciated.
It looks like the only issue is with the duplicated "named route" name reply_board_conversation. So you could simply change one. I'd probably rename the save version to save_reply_board_conversation. Then it should work. Just remember to refer to the route this way in the future. This would primarily be used in a form tag. So, for exmaple:
<= form_tag :url => save_reply_board_conversation_path do %>
Note the use of save_reply_board_conversation_path instead of reply_board_conversation_path given that the form would be submitting a POST request instead of a GET request.
The names for these routes should be different although since the composition of the URL is the same so you really only need a name for the first one.
The trick with named routes is they generate the URL only, they do not set the HTTP request method. That has to be done independently.
That means you can call the same named route two different ways:
<%= link_to('View', board_path(#board)) %>
<%= link_to('Delete', board_path(#board), method: :delete) %>
These actually render as the same URL but one will hit the GET route, the other the DELETE one.

Formatting a form using rails app

How can i format a form on a rails app?
I have my application.css file, no classes where gives or ids to the form tags inside of the form action page. but the form inherited the formatting in somehow but it is not what i wanted.
Also, how can i make every single page in my rails app have its own page and css file?
i don't want to make one master page and imbed the pages codes inside the body tag
like i want to have every single page its own application.html.erb and how to let rails know about this and not to load the default one?
I'm a php guy, so working with styling and templates very confusing to me.
when i want to format a form using css. i know how to do it, also i write the code like below:
<form id="FirstForm">
in my rails app tutorial i followed, there is no id tages or formatting inside the form tags and fields at all. don't know how this got formatted? is it allowed to write ids and classes inside of the rails form tag?
You can pass id and class as you desire.
Example:
<%= form_for #article, :url => { :action => "create" }, :html => {:id => "FirstForm", :class => "your_class"} do |f| %>
Check full doc.

Ruby on Rails: bbc-code and performance

I am using bb-code in a Rails application for postings and comments. At the moment, I have the following to put a post's content in a view:
<%= #post.content.bbcode_to_html.html_safe.gsub('<a', '<a rel="nofollow"') %>
What is the best way to convert the bb-code to html and add "nofollow" to all links?
Thanks!
The bb-ruby gem you are using allows for using custom BBCode translations passed as parameters to the bbcode_to_html method. However, if you really want ALL links to contain the rel="nofollow", I think your best bet is going to be monkey patching them gem itself. Based on the BBRuby source, you want to do this:
module BBRuby
##tags = ##tags.merge({
'Link' => [
/\[url=(.*?)\](.*?)\[\/url\]/mi,
'\2',
'Hyperlink to somewhere else',
'Maybe try looking on [url=http://google.com]Google[/url]?',
:link],
'Link (Implied)' => [
/\[url\](.*?)\[\/url\]/mi,
'\1',
'Hyperlink (implied)',
"Maybe try looking on [url]http://google.com[/url]",
:link],
'Link (Automatic)' => [
/(\A|\s)((https?:\/\/|www\.)[^\s<]+)/,
' \2',
'Hyperlink (automatic)',
'Maybe try looking on http://www.google.com',
:link]
})
end
This will rewrite the BBRuby translator to always include a nofollow attribute. I would put this in config/initializers with a descriptive filename such as bbruby_nofollow_monkeypatch.rb
As for the html_safe, I would leave that as is. As I understand it that is a preferred way of doing it and in my opinion it keeps your intent clear. The above monkey patch makes the line in your view more readable:
<%= #post.content.bbcode_to_html.html_safe %>

Ruby on Rails: form and radio button question

I have a form around a list of items. I would like to check a radio button on the list and then, after clicking 'Submit', go to the 'Edit' page of the selected item.
The problem is that if I am writing something like
<%= form_tag edit_item_path %>
but Rails complains that I didn't provided a proper id, which in turn is being set on my radio buttons as the following:
<%= radio_button 'item', 'id', item.id %>
So - how would I send a form with a selected id to a given action?
Doing it the way you describe would not work, since edit_item_path by default RESTful path definitions is a GET request. You are trying to make it a POST request. If you would like to stick to the RESTful way of doing things I would recommend simply looping through your items and provide links to edit, since you are planning to edit them one at a time anyways. Otherwise, you would have to define a new route if you would prefer doing things within the form with radio buttons. If you are, than you would add something like this to your config/routes.rb: (assuming you are using rails 2.3.x)
map.resources :items, :member =>{:edit_radio_form => :post}
Than your form would look like this:
<%= form_tag edit_radio_form_item_path do |form| %>
And it should work, but it not the right way of doing things for several reasons, the most anoying one of which is that you won't get a full URL on your edit page because you got there with a POST request, so if you refresh the page, your item id param will be gone and will produce an error.

Hard coding routes in Rails

Let's say I have this:
<%= link_to "My Big Link", page_path(:id => 4) %>
And in my page.rb I want to show urls by their permalink so I use the standard:
def to_param
"#{id}-#{title.parameterize}"
end
Now when I click "My Big Link" it takes me to the correct page, but the url in the address bar does not display the desired permalink. Instead it just shows the standard:
wwww.mysite.com/pages/4
Is this because I hard-coded an id into the page_path? It also does not work if I use straight html like..
My Big Link
I would appreciate it if anyone could verify this same behavior and let me know if this intended or not. I need the ability to hard code :id's to specify exact pages...
Just use page_path(page). I guess the path helpers don't access the database themself (which is good), but if they are being supplied with an object and that object has a to_param method this method is being used to generate an identifier.
<%= link_to "My Big Link", page_path(page) %>
It's because you are specifying the id:
page_path(:id => 4)
You could specify the path you want in this method:
page_path(:id => "#{id}-#{title.parameterize}")
Where have you defined the to_param method? In the model?
UPDATE TO MY QUESTION ---------------------->
Thanks all for the answers. This was kind of a one off situation. My solution was to simply go with html:
My Big Link
Which produced the desired:
wwww.mysite.com/pages/4-great-title-here
I didn't want to loop through page objects and waste a call to the database for this one link. Much appreciated for all the answers though!

Resources