I have 2 buttons sitting in an if else method and the first button is suppose to allow the user to change a boolean from false to true. Then if the model shows it is true it will show the other button which is a delete button. I cant get the toggle button (top one) to work right the delete is fine. Here is how it stands now.
def which_button(reportapproval)
if reportapproval.user_approved == false
button_to "Approve this Manager?", { controller: "users/reportapprovals", id: #reportapproval.id, action: "#not sure what goes here" }, method: :#not sure what goes here, data: {confirm: "Are you sure you want to give this manager access to your report?" }
else
button_to "Remove this Manager", { controller: "users/reportapprovals", id: #reportapproval.id, action: "destroy" }, method: :delete, data: {confirm: "Are you sure you want to remove this manager's access to your report?" }
end
end
But I keep getting the error Post route no found which i know is not right. I want this to toggle a boolean in the reportapproval model. Please show me what I need to do.
For this you need to make a new action in the users/reportapprovals controller which will set reportapproval.user_approved to true when the user click on the first button. In the first button in the action attribute add the new action created. Also take care to mention the same action in routes. To avoid mentioning the controller and action you can also specify the path of new route created. So this is what you need to do. This can be done using AJAX too or just normally by page reloading.
Related
(New to RoR)I have a link_to in my erb file.
<%= link_to 'Delete', workflows_path+"/destroy/#{workflow.id}?folder=#{#folder_id}", :confirm => "Are you sure you want to delete '#{workflow.name}'?"%>
This will prompt once asking the confirm message.
I want to prompt another alert box on Confirm(On pressing OK) asking "Do you want to delete sub-folders? " with a 'Yes' or 'No' option and pass it to my controller as a boolean(or any type)
(If they choose no, I would redirect the subfolders to parent directory)
Is there an easier way to do this?
in order to have a double-confirmation you would need some javascript.
give your link a CSS class, like 'double-check-workflows'
then add a javascript to your page
$(document).ready(function(){
$("a.double-check-workflows").on('click', function(){
if confirm("first thing to ask"){
target_url = $(this).attr("href")
if confirm("second thing to ask"){
target_url = target_url+"&your_other_param=1"
}
window.location = target_url
}else{
return false
}
})
})
remove the :confirm option from the link helper though cause the js wil handle that
edit
you should really use the routes helper to create the full url instead of adding params as a string as you posted
you could then add a 'delete_sub_folders' param that is default to false/0 and then replace it in the js with .replace() to set it to true/1 if second confirmation returns true
I want to add a confirmation box before destroying an object in a controller. Basically, create the
data: {confirm: "You sure" }
but in a controller during object.destroy Thank you
you can only interact with the user in the view layer, so you need to make that confirmation before you go into the destroy method
You can warn the user by simply add the confirm option to the link, and it will pop up a window to notify the user, same as using alert function in javascript.
<%= link_to 'delete', delete_obj_path(obj), :method=>'delete', :confirm=>'You sure?'%>
In a view file of mine I have a code like this:
<td><%= link_to 'Destroy', link, method: :delete, data: { confirm: 'Are you sure ?' } %></td>
So after clicking on this it's deleting that particular data form database. But this code is in a file named profiles/index.html.erb which is generated with the profile controller. Now after clicking on destroy link the control is going to the links_controller.rb not profiles_controller.rb. But I want this to get redirected to profiles_controller.rb, so that I can do certain operations while deleting in my destroy action in my profiles controller and then get redirected to profiles/index.html.erb, not links/index.html.erb.
How to do this??
Thanks...
The second parameter to link_to is the destination of the link, ie. what you're linking to - it sounds like you're linking to the wrong place.
If you want to link to the profiles controller you need to do that (ie. change link to whatever your route to the correct action of the profiles controller is).
I have an application, which contains calls. I want to be able to cancel the call and supply a reason for the call cancellation. So far I have my cancel action working in the controller, but I'm trying to figure out how to expand it so before it posts "cancel" to the call_status field it will also populate a cancel_reason field based on a drop down.
Here's what I have so far:
view code: cancel button
<%= link_to 'Cancel',
cancel_call_path(call),
confirm: 'Are you sure you want to cancel the call?',
:method => :post,
:class => 'btn btn-danger btn-mini' %>
controller code: cancel action
def cancel
#call = Call.find(params[:id])
attrs = {
call_status: 'cancel',
incharge_id: #call.units.first.incharge_id,
attendant_id: #call.units.first.attendant_id
}
attrs.merge!({ incharge2_id: #call.units.second.incharge_id, attendant2_id: #call.units.second.attendant_id }) if #call.units.count == 2
if #call.update_attributes(attrs)
#call.units.each do |unit|
CallMailer.cancel_call(unit.incharge, #call).deliver
CallMailer.cancel_call(unit.attendant, #call).deliver
end
redirect_to calls_url, :notice => "Call was successfully cancelled"
else
redirect_to calls_url, :error => "Whoops."
end
end
I want either the confirmation pop-up shown, with the reason for cancellation, or the cancel action tied to a different view with a small form, that includes a reason.
By default the confirm attribute in link_to uses a JavaScript window.confirm which is a simple yes/no that returns true/false.
If you want to do it all on the same page you You'll need to use JavaScript and Ajax to accomplish this. Something along the lines of adding an event handler on the Cancel link that will show a modal with a drop down. The result of this modal will then Ajax a POST request to the Rails app. There are a lot of jQuery Plugins that you can use to help you accomplish this in Rails.
The second option is the one you described which would be to use a separate action in your controller. In terms of UX I think the first option is a better route to go and isn't as scary as it sounds.
Rails simulates a DELETE on a resource by using POST and a hidden field in the Form. But that makes it a must to use a form to delete a record.
However if I wanted to delete a row via a Anchor tag 'Delete' displayed at the end of the row how would I achieve the same? So say I am displaying people records and my rows looks like:
||First Name|| Last Name||
||Foo || Bar || Delete
||Baz || Foo || Delete
...
And on the delete click I want to route to /people/:id/destroy without using a Form and Hidden field?
Rails' link_to method supports the GET, POST, PUT, and DELETE HTTP verbs. For example, you could do something like:
link_to "Delete", person_path(#person), method: :delete, confirm: "Are you sure?"
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to