I'm trying to use the button widget of jquery-ui in my ror project.
I added the jquery-1.5.1.min.js file in the public/javascripts folder, and included it in the application.html.erb file as below:
#application.html.erb
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag "jquery-1.5.1.min" %>
<%= javascript_include_tag "jquery-ui-1.8.14.custom.min" %>
Then one of my routes stops working.
# routes.rb
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
#application.html.erb
<%= link_to image_tag("Arrow Left 2.png"), logout_path, :method => :delete, :class => "barlink" %>
The button effect works, but When I click on the logout link, it gives the following error:
Routing Error
No route matches "/logout"
I also tried to switch the order of the scripts as:
<%= javascript_include_tag "jquery-1.5.1.min" %>
<%= javascript_include_tag :defaults %>
For this case, the route works, but my button effect disappears.
Any idea?
<%= link_to {:controller => 'sessions', :action => "destroy"}, :method => :delete, :class => "barlink" do %>
<%= image_tag("Arrow Left 2.png") %>
<% end %>
I've problem similiar like you and solve by that snippet code... may it help you, thx
Solved.
add the following line in GemFile
gem 'jquery-rails'
run
sudo bundle install
run
rails generate jquery:install -ui -force
then i can either use the jquery-min provided or the jquery-1.5.1.min without any problem.
Related
I am building a link to delete:
I tried 3 different forms but all of them redirect to Show action:
<%= link_to 'delete',{ :controller => 'admin/brands',:action => 'destroy',:id => brand.id } %>
<%= link_to 'delete',brand,:method => :delete %>
<%= link_to 'delete',:id => brand.id,:method => :delete %>
The problem is when I click the delete link it redirects to Show action. I have searched and found something like:
//= require jquery_ujs add in aplication.js - Correct !
metatags added in layout:
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Correct!
I also found this option:
config.serve_static_assets = false
I'm using Development enviroment(I think) and not found this option; therefore I don't know why when I click the delete link app redirects to action Show.
UPDATE:
My routes are:
DELETE /en/admin/pages/:id(.:format) admin/pages#destroy {:locale=>"en"}
DELETE /admin/pages/:id(.:format) admin/pages#destroy {:locale=>"pt-BR"}
When I click on any Delete link it redirects me to action Show
Solve this executing rake assets:clean, thanks all.
i have an app with a SearchController and index action.
on the views i have index.html.erb and in the file i wrote this code:
<h1>Search#index</h1>
<%= form_tag(search_path, :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
and my routes file is this:
Ti::Application.routes.draw do
get "search/index"
root :to => "search#index"
end
when i runed the app i got a error that i did not set the "search path" on the routes file so i added this line:
map.search "search", :controller => "search"
but that line dont work, what shoud i write ?
10x
Try:
match 'search', :to => 'search#index'
And don't be afraid to have a play in your routes.rb file. I'd really recommend spending a couple of hours adding dummy routes and running:
rake routes
and see what comes out the other end. If you're just starting out with routes in Rails 3 you should check out this page, it's a pretty good starting point.
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/
Been staring at this problem for a while now. Here's the error I'm getting when I try to view the page.
No route matches {:action=>"confirm", :controller=>"locations"}
This is what I have in the view.
<%= form_for(#location, :url => { :action => :confirm }) do |f| %>
<% end %>
And I think my routes file is set up correctly.
Finder::Application.routes.draw do
resources :locations do
member do
post :confirm
end
end
root :to => 'locations/index'
end
Any ideas?
Updated:
Ran rake routes and get what I think is correct.
confirm_location POST /locations/:id/confirm(.:format) {:action=>"confirm", :controller=>"locations"}
You can debug your routes easily in the future by running $ rake routes and looking at the output. ;)
I think what is happening is that your post :confirm isn't registering the route you're expecting. In the guides, match and it's brethren accept a string as a URL segment like so:
resources :locations do
member do
post 'confirm'
end
end
Note that "confirm" is now a string instead of a symbol.
If this doesn't help, run $ rake routes and tack the output onto your question.
Update
After seeing your rake output, I think that you just need to specify the POST method on your form_for:
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
You can also make this more readable using that helper method that Rails defines:
<%= form_for(#location, :url => confirm_location_path(#location), :method => :post) do |f| %>
<% end %>
Did you define the confirm action in your LocationsController?
Try adding a :method => :post to your form_for
<%= form_for(#location, :url => { :action => :confirm }, :method => :post) do |f| %>
<% end %>
Make sure that form_for doesn't sneak in a hidden field with _method=put if you have declared the route as accepting only post in your routes file.
I'm having trouble with my verbs in Rails...
viewing a page for a resource (Dog) which has_many (Fleas). Embedded in dog's show.html.haml is a call to render #dog.fleas which automatically(?) finds & uses the template in "fleas/_flea.html.haml" to list each flea associated with said dog.
this displays correctly. whew! Now, next to each flea I've put a "Kill Flea" link that goes to a url: //localhost:3000/dogs/1/fleas/7. Which is generated by:
= link_to("Kill Flea", [ flea.dog, flea ], :method => :delete, :confirm => "Sure? A bunny will die")
but every time that link is clicked there is no confirmation... and it renders the flea's show.html page. it's as if it's using GET on /dogs/1/fleas/7 instead of DELETE?!?
ps- not worried about spiders & robots deleting things in my database... i'm just trying to learn Rails..and understand what's happening
Rails 3 uses unobtrusive javascript now. In Rails 2.3, erb would just shove all that messy javascript right into the link itself, in an onClick event. Now the javascript has been moved out of the link, and into external js files. Make sure you have this in your layout:
<%= javascript_include_tag :all %>
If you do have this, there might be deeper problems keeping your javascript from running, but this is the place to start. Let me know how it turns out.
Hi you can also try this:
application.html.erb:
<%= javascript_include_tag 'jquery_ujs' %>
OR
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %>
Check your <%= javascript_include_tag %> in application.html.erb
Perhaps you are missing the "application" js
It must look at least like
<%= javascript_include_tag "application" %>
Here I am not using the javascript_include_tag :all, :application etc.
I am using my own js files, with another custom name: javascript_include_tag :my_custom_file and I also had this issue. And I am not using '= require jquery' in the file, but javascript/jquery is working. I think Rails includes jquery by default. What I done is I changed the
link_to "Logout", destroy_user_session_path, :method => :delete
to
form_tag destroy_user_session_path, :method => :delete, :id => "logout_form"
link_to 'Logout', "#delete", :onclick => "$('#logout_form').submit();"
Now its working fine.
check if application.html.erb
<%= javascript_include_tag :application %>
not as:
<%= javascript_include_tag :default %>
Looks like your link_to method isn't quite right. Try using this code instead:
<%= link_to 'Kill Flea', [flea.dog, flea], :confirm => 'Sure?', :method => :delete %>
I am using rails to build my application and I want a thickbox on clicking a particular link..
I use the below code in my partial which is displayed in a division of a page and on clicking the user.name, i need the thickbox.. when i use the below code, nothing happens..
<%= link_to_function user.name,remote_function(:update=>"result", :url => {:action => :resultsuser}, :with => "'id='+#{user.id}+'&height=400&width=600'") %>
i have included the
<%= javascript_include_tag 'jquery' %>
<%= javascript_include_tag 'thickbox' %>
jQuery.noConflict()
<%= stylesheet_link_tag("thickbox") %>
and also replaced all $ by jQuery in the thickbox.js file.. nothing happens..
please help me out.
Did you put the
jQuery.noConflict();
into a scripttag?
Try adding the class option in the link_to_function helper method.
<%= link_to_function user.name,remote_function(:update=>"result", :url => {:action => :resultsuser}, :with => "'id='+#{user.id}+'&height=400&width=600'", :class => "thickbox") %>