I have an MVC application which has news that i can create in the database and display for my users. I want to provide users with the ability to "dismiss" a news article and never see the article again. What would be a clever and sensible way to store / provide this functionality?
Some rough guidance would be great
Thanks
Chris
Assuming you have a User table and an Article table, I'd suggest creating a many-to-many UserArticle table that contains a composite primary key of the User Id and Article Id, as well as a datetime field to record when the user has dismissed the article.
In your front-end code, when the user clicks the "dismiss" link/button, hook it up to a Jquery ajax call which calls a controller action which adds an entry to your UserArticle table with the current user Id, article Id and current datetime.
Then in your query that retrieves the list of news articles, add to the WHERE clause an additional clause that filters out articles dismissed by the current user.
Related
I'm pretty new with Ruby on Rails. I have done tutorials and an online class. I know enough to have my way around the development environment, to create tables, controllers, views etc. However, the classes and tutorials don't really show you in depth things you may have to work with in a real software development situation.
I'm in the process of converting a piece of software to the Ruby on Rails platform. Here is the situation. I have a Salesman controller that has salesman information. Then i have a client controller that handles client information. A salesman has many clients and clients have one salesman. I already coded the REST architecture for the Salesman object. Now, i have to create the client. But before i can get to the new action in the controller, i have to select a salesman first. This select salesman page is going to be needed on many other occasions as i will have other controllers such as commissions , purchase orders etc that have to be tied to a salesman first. How do i proceed to code such a "temporary select" page before actually going to the new action in the controller. Can we define custom actions after which i would just redirect_to the new action passing the salesman id as a param?
You need to design the app similar to a multi-tenant app. In your case, Sales Person is like a tenant.
Here is the conceptual design:
add current_id class attribute to SalesMan
cattr_accessor :current_id, instance_writer: false, instance_reader: false
In all your models that belong to a sales person, define the default scope:
default_scope { where(sales_man_id: SalesMan.current_id) }
Ensure that current sales man is set before executing any actions that depend on sales man being pre-selected. There are multiple approaches for achieving this and it also depends what kind of user experience you want to provide. Couple of options:
Only show the navs such as New Client only if a salesman is pre-selected. Provide a menu option to select a salesman. In that action set SalesMan.current_id.
If the salesman is going to be different across many subsequent transactions, you may want to implement a salesman selection (step 1 of workflow) through a controller before action. For example, when user selects New Client nav option, the before action can redirect to sales man selection and set a flag. SalesMan controllers selected action should redirect to New Client action and ensure that proper flags (parameters) are set to avoid cycles. Define a new controller SalesManSelectionController and subclass all the controllers whose models depend on salesman.
I am just starting MVC and I would like to know the best practice to pass sensitive information like IDs across views ...
Let's assume that I have a scenario.
I have a car service managing MVC application which allow users to choose product for their registered car.
The user have to register their car first before they choose a product for their service.
In register view, they fill out the car detail and it redirects to purchase product page when they click the submit button. At the time when they click the submit button, we store car details with user ID (which I can get from Identity) and generate unique car ID from the database. I want to pass this newly created car ID to next view.
In purchase product page, they can choose different product A or B and when they choose, it redirects to checkout page.
What I want to achieve now is then in checkout page, how securely we can carry the car ID that user get after they have registered their car and product ID from previous product view so I can process transaction with userID, carID, and productID.
Is Session way to go with this ? Or any other better way to tackle this problem .?
Someone with small example will be great help for me.
Thanks,
In your example given I would certainly recommend storing the ID in a session. The web is a stateless beast, and what you're essentially after doing is recording state for the duration of the user's visit to the website/application - this is essentially what sessions are designed to do.
Creating, storing and retrieving data from a session is simple and can be done like so:
Setting a variable in the session object
[HttpPost]
public ActionResult Login(int carId)
{
...
Session["carId"] = carId;
...
}
Retrieving a variable from the Session object
public ActionResult Load()
{
...
int carId = Session["carId"];
...
}
Whilst this is a basic example, it gives you an idea as to how to store/retrieve simple types of data from a session.
For storing more information such as large objects you can use the [Serialize] class attribute outlined in my answer in this post.
Say I have Author and Book domain classes (one Author has many Books). The user goes to book/create to enter the details for a new Book. One of the fields in the form is a select with all Author instances in the database, but this Book belongs to an Author who is not in the database.
What is the best way to implement the interface to add a new Author in grails, but keep the whole process relatively painless for the user? This is what I am thinking:
add Author fields to Book create view in a hidden div
add a link which will show the above mentioned fields and a save button
post the Author fields via Ajax to save them
update the author select to include the newly created Author
auto select the new Author in the select
I will work, but it seems a bit of a nasty approach (hacking the Book create view with non-Book fields). Is there a nicer way?
I don't think placing non-book fields in Book view is nasty.
What I don't understand is the need for separate Ajax call to save new Author - why not do this in the same action that the book is created in? If user chose an existing Author, set up the book as usually. If not, retrieve the author field values, create new Author instance, save it and then set book's author field to it.
Im building an admin interface for a medical records management app.
My client has asked me for a way to easily select the patient the user wants to work with without having to select the patient everytime he wants to perform an action.
So, say for instance he wants to store a record for the patient's current status (weight, size, etc) and then assign the same patient to a different doctor or change the company the patient currently works for.. he doesnt want to select the same patient all three times... he wants a select dropdown for patients and perform the different actions for that patient.
Im thinking this should be somehow stored in a session variable.. I have a table of patients and Im using LinqtoSql classes.... what do you reccommend?? help please.
Sounds like you want to put something into Session--perhaps some of the basic "recent patient" information, such as a patient ID, patient name, etc.
Definitely take a look at this post on how to do it in a very graceful way.
You could use the session to keep a list of recently active patient records for a given users session. Every time the user selects a new patient simply add that patient's name to the "Recent" list. Since you can control the length of the session you could just allow the list to expire when the user's session does. As far as not making the user select a customer again, just have it auto-select the most recent (last entry) on the list of recent customers.
Personally I would consider caching as an option here. By the sounds of it you want to load ALL data for ALL patients which is fine for a small amount of data but will not scale gracefully.
Consider going to the database the FIRST time you need the patient's data, and getting your data from the cache for subsequent queries...
The best way is to have the id in the route.
Use something like this when registering routes:
routes.MapRoute(
"Standard",
"{controller}/{action}/{PatientId}",
new
{
controller = "home",
action = "index",
PatientId = ""
}
);
When you select a Patient you post to an action that sets the PatientID in the RouteParameterCollection and redirect to the the action that displays the form for changing the patient. this way you always have the patientID in the URL.
Using session has some drawbacks:
If you have 2 windows open both use
the same session. This might confuse
users.
You can not bookmark a page
Session is usually stored in memory
on the appserver. This might lead to
performance problems if extensively used.
You may thick that it is a silly question but I am confused!
I have a situation that I have a Lecture table.
And I want to store attendance of who have registered for it.
I have master table of People who will register, lecture table, and Important one table is attendance that stores P.k. as f.k. of rest of the table.
On Index view of lecture operator will select Attendance and enter attendance information.
My problem It show only one page for attendance entry and that page can also open open in EDIT mode, for editing attendance.
So what would be the design of the page and process flow of taking attendance?
Some of the ways:
You can use same page for editing and listing if you define block where are you rendering partial/dynamic block(user control). You need to extend HtmlHelper so you can pass name of a UserControl and you have to from controller pass to view(with ViewData) name of user control you want rendered. This way you can use same template(View/Page) with different actions and different response.
You can have editing on client. You can use jQuery UI library to popup dialog box for editing. For posting editing information back, you can use jquery .ajax method. As things happening on client and you request different actions on controller, it doesn't matter how you will approach this in design sense. You can use dialogs, you can transform existing elements on page etc.
Hope this helps