Model-View-Controller (MVC)
Yesterday I was reading a book. they talk about MVC pattern is becoming very popular in these days. I think Rails is the really really first web application framework that implemented MVC pattern. For me if I didn’t meet rails, I wouldn’t know about MVC either.
So in this post I will briefly talk about MVC pattern in and easy way to understand more than reading a paper works in the web site. I will try my best though.
Just for the beginners (Me too!!) alright I will explain it one-by-one.
Model (M)
Of course M is standing for Model. I think the easy way to know what exactly mean model is it’s represented things that you want to see. imagine of a super model that walking on the cat walk in fashion TV. I really want you to see this point. okay you want to see all the winter fashion. So you select to go to the winter fashion(winter_fashion table). But how are you going to see the clothes(data). So you need someone to put it on, we called that a model. Now I think you see the point now.
Model is just the object that represent the data in the table.
Note: Clothes (Data) need to represent it by Super Model (Model)
View (V)
For View, This one kinda easy to understand. I will keep going on that Fashion example. Okay the only one rule of this example is you can not go to see the fashion at the place but you can just watch it at home. So how can you see it at home? You need a television which is like a view in MVC pattern. Because TV just let us see what they look like. You can only see things only by TV (view).
Controller (C)
Controller is the one that kinda busy all the time, because it need to deal with both sides (View and Model). Controller control every action the user make it from the view and send the request to controller. After controller know what user need then it goes get data from the model. Back to our example. In our TV we have a lot of channels (tables). not only just the winter fashion that we want to see. some time we want to change the channel. After we press the remote button. The Station will have to check what channel that we requested for. after that the station(controller) need to give as a channel (model) that we want to see
When all of these 3 combine it together. it will look like this
I hope you all will get better with MVC pattern. and if I explain something wrong please leave a comment for me. I need to know it. because we share, right?
By the way Thanks for google and all the web-site that related to those pics up there
undefined method `assert_valid_keys’
Today when I try to do a multiple relations, I end up with this error.
undefined method `assert_valid_keys' |
First I checked on my relations. But everything seems to be okay with it.
This is made me so crazy. I don’t know what is happening here. Finally I found a reason why?
** You shouldn’t put multiple relations on the same line
Before:
| belongs_to :user, :expense_report_status |
After:
| belongs_to :user belongs_to :expense_report_status |
I don’t know why? but I will find out later. Maybe someone can give me a suggestion with this.
How To: Delete record in join table.
Yesterday I was playing with how to do the authorization in rails? So I got a book called “Rails Recipes” page 141. they have a pretty nice tutorial talking about how to authorize.
But I have a little problem when I need to cancel the rights from role. because we don’t have a model represented rights_roles table. So how can we delete the record from that table in our controller. After I had a great help from my friend and looked up in Rails API a little bit. We found out that we can also do things like this
But first I’m going to show you the structure of role and right models.
Right
| class Right < ActiveRecord::Base
has_and_belongs_to_many :roles end |
Note: this tell us that Right has and belong to many roles
Role
| class Role < ActiveRecord::Base
has_and_belongs_to_many :users end |
Note: Role has and belong to many users and rights
After we assigns right for role. it will record it into the table called “rights_roles”
Rights_Roles
| Role_id (fk) | Integer |
| Right_id (fk) | Integer |
Okay now is how you can delete record in rights_roles table without need a model.
| def cancel_right @right = Right.find(params[:id]) @role = Role.find(params[:role]) @role.rights.delete(@right) => get rights in the role object and delete which has been selected redirect_to :action => “view_assign_right”, :id => @role.id |
*สำหรับภาษาไทยกด 1 ครับ อิอิ
Include?
One of the very helpful method in array is include?
if you want to know what is your array contained? you can try to doing this:
| @my_array = ['ruby', 'on', 'rails']
@my_array.include?(“rails”) # This will return “true” |