First of all, I'm new at Ruby on Rails, so if there's a better practice than what I'm doing, please let me know.
What I'd like to do is to have multiple ajax submit buttons to perform different actions for a list of items with check boxes. So, when I select as many check boxes as I want, then I can choose what to do with them.
I could partially solve this using ajax as follows:
remote_form_for :profile, :url => {:controller => 'announcements', :action => 'deactivate'}, :html => { :method => :put} do |f|
f.submit 'Publish', :confirm => 'Are you sure?'
#Then the list
This works great. What I'd like to do now is to add another "submit" or button, so I can perform several actions, so I guess the :url statement at the remote_form_for will be replaced for something like that per each button. For example:
Publish button: perform some action in some controller.
Deactivate button: perform another action.
Mark as Read: perform another action.
Is it clear?
add as many "submits" as you like. All submits have the 'name' in the input tag set to 'commit' and the value set to the label. So you can check by looking in the params submitted:
remote_form_for :profile, :url => {:controller => 'announcements', :action => 'deactivate'}, :html => { :method => :put} do |f|
f.submit 'Publish', :confirm => 'Are you sure?'
f.submit 'Something Else"
# Then the list
in the controller
def deactivate
case params[:commit]
when 'Publish' then do something
when 'Something Else' then do something else
end
end
Finally, I could solve it by myself.
It's well known there's a bug for multiple submit buttons on ajax forms. I used the hidden workaround as explained here.
Thanks,
Brian
Related
In my view I have 2 links:
one for edit, that works great, and one for destroy action.
metod looks like:
def create_ticket(ticket)
#ticket = ticket
#edit = edit_ticket_url(#ticket, :host => "localhost:3000", :guest_password => #ticket.guest_password)
#destroy = ticket_url(#ticket, :host => "localhost:3000", :guest_password => #ticket.guest_password)
mail(:to => #ticket.email, :subject => #ticket.subject)
end
in view template:
<%= link_to "Edit Ticket", #edit %>
<%= link_to "Delete Ticket", #destroy, :method => :delete %>
the last one simply getting path instead of destroying item.
How to solve this?
The reason is not working, i think, is because the :method => :delete is handled with javascript, and since the link is clicked from the email, javascript is not triggered. The way I would have try to solve this is by passing a parameter to the url like "delete=true" and handle it with that in the controller.
Okay so I'm building a really simple list with items app, pretty much exactly the same as your standard to-do list application. I've managed to ajax-ify the creation of new 'points' within a list (point belongs_to :list and list has_many :points) but I'm having trouble with the 'destroy' action.
When I click on the destroy link in the browser, nothing visibly occurs, and I get the error Error: Syntax error, unrecognized expression: /lists/10/points/125 obviously with different values depending on the id of the list and point.
If I refresh the page or look at the db, it's clear that the entry has indeed been deleted. Without ajax, my destroy action works just fine. I feel like I must be missing something obvious, any ideas?
fyi the 'pro' attribute is just a boolean associated with every point.
points_controller.rb
def destroy
#point = #list.points.find(params[:id])
#point.destroy
respond_to do |format|
format.html { redirect_to list_url(#list) }
format.js
end
end
lists/show.html.erb
<% #list.points.each do |point| %>
<% if point.pro == true and point.valid? == true %>
<li class="weight-<%= point.weight %>"><%= point.content %>
<%= link_to "×".html_safe, [#list, point],
:remote => true,
:method => :delete,
:class=> "close",
:data => {:dismiss => 'alert'} %>
</li>
And it doesn't seem to matter what I put in views/points/destroy.js.erb, because the code doesn't seem to be getting executed.
Update
I figured it out, I had to change the path in the delete link to list_point_url(#list, point). The other problem was that my invalid javascript was causing a server error, so I didn't realize what the problem was (turns out #<%= dom_id(#point) %> needed to be wrapped in quotes).
Thanks all!
Maybe check if the delete link routes to the destroy controller action, because list_point_path doesn't really seem like a delete route.
Edit
Sorry for the lake of knowledge but I'm not sure what [#list, point] will produce as a route. This is what I have for a view of my own, just for your reference:
link_to "Delete", admin_photo_path(photo), :method => :delete, :confirm => "Delete this image?", :class => "btn-trash"
My admin_photo_path is a singular path that route to a single Photo instance; not a collection.
Edit
Simple way could be sending delete to the point object, maybe this could help?
link_to "×".html_safe, point,
:remote => true,
:method => :delete,
:class=> "close",
:data => {:dismiss => 'alert'}
I wanted some code that would automatically reload the posts and insert them into the div post_container:
<%= link_to_remote "Update", {:action => 'ajax_update', :id => #posts, :update => 'post_container'}, :confirm => "Are you sure?" %>
This rails snippet in my home.html.erb actually takes the entire page (title, head, body tags) and places it inside of the div post_container. Why? Also, as far as I can tell, the ajax_update function doesn't even get called.
How would I do what I am trying to do? And why is this entire page loading happening? I'm using Rails 2.3.11
(edit: also, there is no confirm dialog when you click the link.)
EDIT 2:
the html output of the code snippet:
<a confirm="Are you sure?" href="#" onclick="new Ajax.Updater('post_container', '/home', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('uaqM0Ie8to5pprvE6WcF416DN0vNeyO7Xa+JM6VZFY4=')}); return false;">Update</a>
In your controller action, use :layout => false
def ajax_update
#posts = Post.find(#posts)
render :layout => false
end
Found the correct way to do it:
<%= link_to_remote "Update",:update => 'post_container', :url => {:action => 'ajax_update', :id => #posts, }, :confirm => "Are you sure?" %>
Based on information from the Rails 2.3.11 API documentation:
http://api.rubyonrails.org/v2.3.11/classes/ActionView/Helpers/PrototypeHelper.html#M002185
Also, the periodically_call_remote method found on that page is also useful for automatic calls that don't require clicking.
This is probably a very simple question. I am new to Rails and have just hit my first major roadblock.
I have two controllers, Groups and MembershipRequests. When someone submits a MembershipRequest, the admin of the Group has the option to accept or deny the request. In my MembershipRequests controller, I have two methods: accept and deny. They both work, but now I'm unsure of dealing with my routes.
This is the relevant part of my routes.rb:
resources :groups do
member do
get 'members'
resources :membership_requests do
member do
post 'accept'
post 'deny'
end
end
...
end
end
Okay, onto my real question: I want to be able to accept and deny requests without an additional page. I want to use JavaScript to use a button on my MembershipRequests index page where the admin can accept or deny the requests.
Here's my erb code for the accept and deny buttons:
<%= link_to "Accept", :controller => 'membership_requests', :action => 'accept', :confirm => 'Are you sure?' %>
<%= link_to "Deny", :controller => 'membership_requests', :action => 'deny', :confirm => 'Are you sure?' %>
Clicking either of these links give me the
No route matches "/groups/1/membership_requests/1/{accept,deny}"
just like I would expect. But I do not know how to get around this. Thanks!
I think, link_to should also have :method => :post because you're mapping it to a custom (non-RESTful) action (that's just an assumption) and :remote => true, to tell rails that this link uses unobtrusive JS.
This screencast might help, or at least give a starting point.
Oh, and yes. As Ryan suggests, you'd better use something like this:
#requests.each do |request|
link_to "Accept", membership_request_accept_path(request), :confirm => 'Are you sure?', :remote => true
end
Hi
I have asked a question similar to this before but never got it resolved. So I am trying again.
This seems like it should be so simple. I am not using Rails 3 yet BTW.
All I want to do is have a drop down menu and when a person chooses that location and presses "go" they go to that page.
<% form_tag installation_path([:id]), :url => { :action => "show" }, :method => :get do %>
<%= select_tag :id, options_from_collection_for_select(Installation.find(:all), :id, :name) %>
<%= submit_tag 'Go' %>
<% end %>
This becomes the issue: http://localhost:3000/installations/id?id=1&commit=Create. It can't find the :id. I just don't know how to route this correctly. It seems like this shouldn't be that difficult.
Any help would be great. Thanks.
I think there might be a problem with your form_tag. It seems you're defining the path twice.
Both
installation_path([:id])
and
:url => { :action => "show" }
are used to generate the path but I don't think you should be using both. Just go with
installation_path([:id])
or
:url => { :controller => "installations", :action => "show", :id => id }
You need to create and use a new "show" route that is not based on the installation id (and doesn't collide with Rails resource routes), and continue to send the installation id into the controller's show action as part of the params object.
In routes.rb,
get 'show_installation', to: 'installations#show'
In your view,
<% form_tag show_installation_path, :method => :get %>
...