ActiveAdmin: Can I redirect the user to the index after they create/edit something? - activeadmin

My customer (who is nontechnical) has asked me if I can change an ActiveAdmin form to redirect to the index instead of the show-item page after she submits (assuming no form errors).
Is that possible? How would I do that?

Well, that was a lot easier than I expected.
For my Articles model,
ActiveAdmin.register Article do
...
controller do
def create
create! { admin_articles_url }
end
def update
update! { admin_articles_url }
end
end
...
end
That's it!
This is implemented by the inherited_resources gem, which Active Admin uses. My code changes are actually right from that project's readme.

Related

Autosave Feature in MVC

Can you please let me know how to implement auto save fetaure in asp.net mvc using jquery and ajax. I have a form as shown below
#using (Html.BeginForm(null, null, FormMethod.Post, new { name =
"frmStudentDetails", id = "frmStudentDetails" }))
{
}
The above forms having few controls like textbox, dropdown etc. Present logic is that as soon as the user clicks on save button it will go to the controller "Student" and trigger the action "UpdateStudent"
Is it possible to invoke the same controller and action automatically in a period of 1 minute. If so can you please let me know.
There are so many approaches to do this. The first one is you can make another Model for a Student-View like StudentViewModel. In which, you inherit the actual Model Class of Student and then add the extra field of searching, traversing, and updating the records that you are required. You must need to add script-tag-handlers to bind the data from your StudentViewModel.
For more dig in deep, you must read how the Dependency Injection and Repository Pattern works in MVC.

Avoid Resubmit data cakephp page when refresh

i am fighting from some days about avoing resubmit data in a cake view when i refresh the page.
I will explain the data process
One view with an to /quotes/select/
saving data in function select
When i arrive to the page select.ctp i have the problem on the refresh. Everytime i refresh a new quote is saved into the DB.
In this case no forms. Is there a solution?
Working on this matter i found the Security Component and i would like to use for the "Forms". I tried to use but i get the following error:
Missing Helper
Error: SecurityHelper could not be found.
Error: Create the class SecurityHelper below in file: app/View/Helper/SecurityHelper.php
Where can i find it? Thank in advance.
Considering that you have something similar to this:
if ($this->Quotes->save($quote)) {
$this->Flash->success(__('Your quote has been saved.'));
}
You can add a redirect to the same page which will clear the POST request that is left in the browser
if ($this->Quotes->save($quote)) {
$this->Flash->success(__('Your quote has been saved.'));
return $this->redirect([]); // <----- Redirects to same page
}

How to delete an item in expressJS view

I am building an ExpressJS app and want to add a delete icon on a collection to delete individual items.
I am a bit confused how to do this.
One method I thought of is binding a click event to the icon in the express view and doing an ajax call to the server when clicked.
Another method is to create a form around the icon and the icon will be a button that when clicked submits the form.
I am not confident of the two approaches, anybody have thought on an elegant way to do this the express way
i recommend the second method because it's more easy to understand at this moment.
from your words i understand the delete button could be vulnerability or security hole if you did in the wrong way. Sure it's delete button on any way.
the most easy way to do it with more secure is to use session variable.
the user can't delete unless he is authorized (logged in). if so then you open session on your server and give the user the key of that session.
In the session you can store securely data about the user who interact with the server via providing the session key you gave him at login process.
at this step the user will click the button to delete document but guess what he should be authorized to delete this document. This the time for session key he provide to you to inform you his identity. then the decision is up to you either to delete or reject his request.
all of the above word are concept of what will happen in the code.
i will write two part one part for the login controller to give the user authorization. And the second is the delete document controller.
login:
if(var user = isUser(username, password){
//open session
req.session.user_id = user._id
}
delete document controller:
if(req.session.user_id){
//if true that means he is logged in authorized user
//you can also to check by his user_id if he has the privilege to delete the document
document.delete();//in mongoose model.remove();
}
this solution is for security in deleting any document.
There are multiple ways you can achieve the result you seek.
You could use a link that has the id of the item you want to delete YourURL.com/item/delete/id and attach a click event to it. When the link is clicked your handler should be able to get the id query params and use it to make an AJAX call to the server.
Also, you can use a button like you said, which is basically the same thing as the one above
Bottom line is, both methods are pretty standard, some people could use hidden elements, HTML elements, almost anything that can store an ID or a value, but you will find that most people also use the above methods as well, Which IMHO is pretty standard.
Below is a snippet of how it should work, I am not sure if you are using any Javascript libraries so I scripted it with Vanilla Javascript, if you are not using any Javascript libraries or frameworks I strongly advice you do, it helps reduce a lot of headaches you get from browser difference in the way the handle Javascript.
PS: Include codes you have tried to help give context to how your question could be answered.
var makeDeleteCall = function(e, id) {
e.preventDefault();
console.log(id);
// Make AJAX Call here to server to delete item with the ID
//After call to remote server you can then update the DOM to remove the item
document.getElementById(id).remove();
}
<!-- Using Link -->
<a href="#/delete/1" onclick="makeDeleteCall(event, 1);" id="1">
Delete Item
</a>
<br/>
<br/>
<!-- Using a Button-->
<button onclick="makeDeleteCall(event, 2);" id="2">
Delete Button
</button>
Link to JSFiddle

CollectionAction route being skipped, action treated as ID

I'm trying to create a collection_action in ActiveAdmin which allows me to Import a CSV file and generate Subscribers from it. I want to be able to click an action_item link and be taken to a form in which I input the CSV file, and then do some work with it.
This is what I have so far:
ActiveAdmin.register Subscriber do
collection_action :import_csv, :method => :post do
render "import_csv"
end
action_item do
link_to "Import from CSV", import_csv_admin_subscribers_path
end
The view is also created, just blank at the moment. I've restarted the rails server, and rake routes outputs:
import_csv_admin_subscribers POST /admin/subscribers/import_csv(.:format) admin/subscribers#import_csv
batch_action_admin_subscribers POST /admin/subscribers/batch_action(.:format) admin/subscribers#batch_action
admin_subscribers GET /admin/subscribers(.:format) admin/subscribers#index
POST /admin/subscribers(.:format) admin/subscribers#create
new_admin_subscriber GET /admin/subscribers/new(.:format) admin/subscribers#new
edit_admin_subscriber GET /admin/subscribers/:id/edit(.:format) admin/subscribers#edit
admin_subscriber GET /admin/subscribers/:id(.:format) admin/subscribers#show
PUT /admin/subscribers/:id(.:format) admin/subscribers#update
DELETE /admin/subscribers/:id(.:format) admin/subscribers#destroy
However when I click the action item I get the error Couldn't find Subscriber with id=import_csv
If I change the method to :get it renders the view fine. I'm assuming the problem is my use of :post? Is it not possible to render a view if you're calling a controller action with that method?
edit Ok, yeah, it doesn't make sense to render a view with a post but then why does the ActiveAdmin doc suggest that you do the action this way for CSV imports? How are you supposed to # Do some CSV importing work here... without generation a form?
You need to add the method: :post option to the link to invocation, since there's no get action for that url.

SharePoint itemDeleting evert is not working

I have a site in SharePoint and I want to custom delete from a list. So, I'm creating the
public class ListItemEventReceiver : SPItemEventReceiver
{
public override void ItemDeleting(SPItemEventProperties properties)
{
if (properties.ListTitle.Equals("Projects List"))
{
Projects pr = new Projects();
string projectName = properties.ListItem["Project Name"].ToString();
pr.DeleteProject(projectName);
}
}
}
Where 'Projects' class has 'DeleteProject' method who deletes the item.
But it's doing nothing :(
I mention that everything it's ok in Feature.xml
Where am I wrong?
Edit (from 'answer'):
Yes, I've tried this:
properties.ErrorMessage = "projectName :" + projectName;
properties.Cancel = true;
in if clause and the event it's firing and displays the project name corectly.
I'm the farm administrator, and site administrator with full control over this site.
DeleteProject method it's right, because I've tried it in some other application (c#) and it's works fine.
A couple of things to check:
Is your list item reciever connected to the list, so that it fires?
Does the user that causes the trigger to fire have the the right to delete items?
Is there any programming error in DeleteProject?
Try putting in some logging to see how far it is running.
Edit
Could the problem be here:
string projectName = properties.ListItem["Project Name"].ToString();
Is the list item called "Project Name" with a space in the name?
Edit 2
From your comments, the combination of authentication and connection string means that it is the security context of the logged on user that is being used against the database. Check the rights of your user.
If event is firing and the only method pr.DeleteProject(projectName); is not working properly then it is difficult to guess what is wrong. If it is not confidential, please post your code and then I shall be in better position to identify what is wrong.
By the way, are you calling .Update() Method on list?
Please check out this link http://msdn.microsoft.com/en-us/library/ms431920.aspx
One more thing to care about is Itemed and Iteming events. It is better to use Before or After properties as appropriate in case of Item*ing events.
Regards,
Azher Iqbal

Resources