add new author to book - grails

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.

Related

Coding / Database Pattern - Dismiss News

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.

Proper way of creating object using a form in its parents view?

Let's say I have two classes:
News (hasMany Comment)
Comment (belongsTo News)
In single news object view (news/show.gsp) I'm listing comments belonging to it after it's content.
Then I want to have a form which will allow some user (but security isn't a case here) to add new comment, and then show the same (news) view again.
Seems easy, but I couldn't find the anwser:
What is the proper way of doing it in Grails?
If you run grails generate-all News and grails generate-all Comment on the command-line, Grails will generate views and controller actions that allow you to CRUD each entity. If you read the generated code for the create and save actions and views of CommentController, that should set you straight.

MVC with View Model - Create an object optionally

I use MVC with View Models (I create separate View Model for each View).
I have 2 objects; Product and Category; Product can have a category.
I have separate view models (productCreateVM, CategoryCreateVM)
I can create product or category;and user can choose one of current categories while creating a new product, no problem.
What I need to do is that ; users can choose one of current categories or create a new category than choose it, while creating a new product.
If I add CategoryCreateVM in ProductCreateVM, then CategoryCreateVM's mandatory fields need to be filled in, in order the model state to be valid.
but if user already found a category for their product and didnt create a new one; I can not fill all the mandatory fields of CategoryCreateVM.
On the other hand; if user is creating a new category from product create view; then all the validations should be applied (category name lenght etc).
Any suggestions how to approach to this?
If you want to use more complex validations see if http://foolproof.codeplex.com/ will be sufficient.
It it should provide you with the conditional validation you need
You should consider rendering the Create Category View as a partial view within the Create Product View.
Its visibility can be toggled by e.g. the selection of Create Category within a Category drop-down menu.
For example:
#Html.Partial("Category/Create")

How to model many-to-many relationships?

I have a problem with a model in a asp.net mvc3 application.
First of all, i have a table with items, each item has e.g.: id,name and description
where i also have a user with: id, name
It looks somewhat like:
class item {int id, string name, string description}
class user {int id, string name}
Now i also have a table which maps these two things to each other, with foreign keys.
so each user can have * items and each item also can have * users.
Now i need to create controller and view for the item. this works fine, the problem is, that i just create a item, but i want to create a item and the mapping to a user in one step.
how to solve this problem in mvc? (e.g. i just can add one model in a view and this is the item in the create process)
Short answer: you need to add drop-down of users while creating an item. This will help to link each item to a user.
However, your particulate issue seems to be in building many-to-many relationship in the model. How to do this? Well, this is already nicely explained here - ASP.NET MVC, Entity Framework, One-to-Many and Many-to-Many INSERTS
In addition, I would strongly suggest to go through - NerdDinner Tutorial. It will give you good start-up and solid background for development in asp.net mvc framework.
You may also refer to Tutorial samples in official website to learn and practice development in mvc.
Here is a link to complete code of the tutorial - NerdDinner 2.0 Complete ASP.NET MVC Sample App

ASP.NET MVC ViewModel to Hold Lists & "Images or YouTube URL's"

I'm posting this question because I do not know the best/correct way of doing the following.
My team-mate (the designer) sent me a good looking design that includes a wizard for adding new items (for auction). The user has to fill in all the required details which include the title, description, starting price...etc AND a list of tags (up to 4 tags - chosen from the database, will use auto complete) as well as a list of up to 3 images/youtube url's (for the sake of better explanation check this image out: http://i55.tinypic.com/2v11zzr.png)
Ok so I figured out how I should do the wizard ( reference: how to make a wizard with ASP.Net MVC) but I'm not sure about how to collect the lists and the images/url's. Here's what I'm thinking:
For the images/url's, I should create a parent view model from which two sub-classes (ImageViewModel & YoutubeUrlViewModel) would inherit from and then in the controller action when I parse the post data, I would check to see the instance of the parent view model and act accordingly.
Now about the lists, I'm not sure whether I should include a List in my view model or whether I should include 4 string properties representing the tags (the same will apply to the list of images/url's).
So what's the best way of doing this?
And Haacked to the rescue: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
:)

Resources