Rails :confirm does not show dialog box - ruby-on-rails

I have a rails app with the following code in one of my views:
<% if #present.taken == false %>
<%= link_to "I want to buy this present", :confirm => 'Are you sure you want to buy this present?', :action => "taken_toggle", :id => #present.id %>
<% end %>
However, I don't get a javascript dialog box showing - it just seems to skip that bit (the calling of the action works).
My application layout has the following:
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
So I think I have the necessary javascript loaded.
Any ideas why this isn't working?

As the documentation shows
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
in
Be careful when using the older argument style, as an extra literal hash is needed:
try using like this
<% if #present.taken == false %>
<%= link_to "I want to buy this present", { :action => "taken_toggle"}, :confirm => 'Are you sure you want to buy this present?', :id => #present.id %>
<% end %>

Related

Rails : Linking an anchor from a different controller/view

<%= link_to (:controller => "company_stuff", :action => "index", :anchor => :menu), :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
I am having difficulty linking a page which is on a different controller and also the link is an anchor. Basically the controller is called company_stuff the action is index and the anchor is called #terms
The problem was that the :controller :action :anchor was not being passed through as a hash, separate from the CSS class
Below is the solution
<%= link_to "Terms Of Use", {:controller => "company_stuff", :anchor => "terms"}, :class => "links" %>
I believe you can try something like this
<%= link_to index_company_stuff_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Or
<%= link_to index_company_stuffs_path + "#terms", :class => 'links' do %>
<li>Terms of Use</li>
<% end %>
Depending on your controller name and route.
You can find more information on this question How to create an anchor and redirect to this specific anchor in Ruby on Rails

Rails 3 - button_to update same page with controller#method

In my project I have the following form_tag to select a Site
<%= form_tag({:controller => "hvacs", :action => "index"}, :method => "get") do %>
<div class="field">
<%= select :p, :site_sel, #user_sites.map{|s| [s.name, s.id]} %>
</div>
<div class="actions">
<%= submit_tag("Select site") %>
</div>
<% end %>
This form_tag updates the index page through calling its method in the controller again.
I have the following button_to
<td><%= button_to 'Select', {:controller => "hvacs", :action => "select"}, :method => "get" %></td>
I would like to achieve a similar update with this as above rather than redirect to a new page with "select_path" etc, but the above does not seem to work.
How can I achieve this? Cheers!
OK, this looked so much like my AJAX problem, I tried to make it one!
I think all you need is a simple render statement in your select action
render :index
or
render :action => 'index'
But see http://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect_to for more.
The following solution worked. Apologies if I was not so clear on what I was looking for.
<%= button_to 'Select', review_hvacs_path(:h => hvac, :a => params[:a], :s => params[:s]) %>
I was trying to pass parameters with the button, while staying on the review page.

Micropost Delete Method won't work

Can't seem to get my delete method to work on a micropost, heres the code:
Code for delete link:
<%= link_to "delete", micropost, :class => "delete_link",
:method => :delete,
:confirm => "You sure?",
:title => micropost.content %>
The micropost controller:
def destroy
#micropost.destroy
redirect_back_or root_path
end
end
Any ideas?
Rails 3.1 uses unobtrusive javascript now. Now the javascript has been moved out of the link, and into external js files. Make sure you have this in your layout:
layout/application.html.erb
<%= javascript_include_tag :all %>
or
If you use ':defaults' like following, in layout/application.html.erb
<%= javascript_include_tag :defaults %>
Then you should specify following in application.rb
config.action_view.javascript_expansions[:defaults] = %w(jquery.min jquery_ujs)

adding a class to a link_to is breaking the link

I'm using link_to in RoR 3
When I use it like this, it works fine:
<%= link_to "Add to your favorites list",:controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}" %>
But I would like to pass in a class as well
however, this is not working for me. The class works, but it breaks the link. Any ideas?
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
try this
<%= link_to "Add to your favorites list", :controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
{ :class=>"ui-button-text button_text" } %>
Since the :class should be in :html_options (refering to API)
link_to(body, url, html_options = {})
The proper way of doing what you have is as follows:
link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"
As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:
link_to "Foo", favourite_companies_path(#company), :method => :post
What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:
link_to "Foo", :company_id => #company.id, :company_name => #company.name
As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.
I'm using a link_to do-end block so the above previous solutions didn't work for me.
If you want to embed other tags in your a tag, then you can use the link_to do-end block.
<%= link_to favourite_companies_path(:company_id => #company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
<i class="fa fa-star"></i>
<%= #company.company_name %>
<% end %>
In this case it's
<%= link_to path(url_params), html_options = {} do %>
<% end %>
Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like this
<%= link_to "Add to your favorites list",
{ controller: "favourite_companies", action:"create"},
company_id: #company.id,
company_name: #company.company_name,
class: "ui-button-text button_text" %>

Associated models in Rails?

In my rails application I have two models called Kases and Notes. They work in the same way comments do with blog posts, I.e. each Kase entry can have multiple notes attached to it.
I have got everything working, but for some reason I cannot get the destroy link to work for the Notes. I think I am overlooking something that is different with associated models to standard models.
Notes Controller
class NotesController < ApplicationController
# POST /notes
# POST /notes.xml
def create
#kase = Kase.find(params[:kase_id])
#note = #kase.notes.create!(params[:note])
respond_to do |format|
format.html { redirect_to #kase }
format.js
end
end
end
Kase Model
class Kase < ActiveRecord::Base
validates_presence_of :jobno
has_many :notes
Note Model
class Note < ActiveRecord::Base
belongs_to :kase
end
In the Kase show view I call a partial within /notes called _notes.html.erb:
Kase Show View
<div id="notes">
<h2>Notes</h2>
<%= render :partial => #kase.notes %>
<% form_for [#kase, Note.new] do |f| %>
<p>
<h3>Add a new note</h3>
<%= f.text_field :body %><%= f.submit "Add Note" %>
</p>
<% end %>
</div>
/notes/_note.html.erb
<% div_for note do %>
<div id="sub-notes">
<p>
<%= h(note.body) %><br />
<span style="font-size:smaller">Created <%= time_ago_in_words(note.created_at) %> ago on <%= note.created_at %></span>
</p>
<%= link_to "Remove Note", kase_path(#kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>
</div>
<% end %>
As you can see, I have a Remove Note destroy link, but that destroys the entire Kase the note is associated with. How do I make the destroy link remove only the note?
<%= link_to "Remove Note", kase_path(#kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>
Any help would, as always, be greatly appreciated!
Thanks,
Danny
<%= link_to "Remove Note", note_path(note), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>
you also will need the following entry in config/routes.rb (check if it already exists)
map.resources :notes
and check for following method in your NotesController
def destroy
#note = Note.find(params[:id])
#note.destroy
.... # some other code here
end
there's also another way of doing that if you don't have a NotesController and don't want to have it
You're calling the delete method on a kase -t hat's why it's deleting a kase. There's nothing in this link
<%= link_to "Remove Note", kase_path(#kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>
apart from the text that even mentions a note - so why would it delete a note? Try
<%= link_to "Remove Note", note_path(note), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>
This assumes you have the standard restful routes and actions set up.
As an additional point, you should never use link_to for non-get actions, because
google spiders and the like will
click on them. You might say 'they
can't because you need to be logged
in' and that's true but it's still
not a good idea.
if someone tries
to open the link in a new tab/window
it will break your site, or go to
the wrong page, since it will try to
open that url but with a get instead
of a delete.
generally, in web
design, links should take you
somewhere and buttons should 'do
stuff', ie make changes. A
destructive action like this
therefore belongs on a button not a
link.
Use button_to instead, which constructs a form to do that same thing.
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002420&name=button_to

Resources