# MVC architecture

**URL:** <https://www.ruby-forum.com/t/mvc-architecture/156985>\
**Category:** Rails\
**Created:** [January 6, 2009, 5:10am UTC](https://www.ruby-forum.com/t/mvc-architecture/156985 "2009-01-06T05:10:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 6, 2009, 5:10am UTC](https://www.ruby-forum.com/t/mvc-architecture/156985/1 "2009-01-06T05:10:54Z")

</div>

Dear all

In the MVC architecture framework, the Model is actually communicate  
with database. In my rails project, there is a model/ssh.rb that is not  
connect with database. Is it a good practice for this arrangement in my  
project? Am I violate the rules of MVC architecture? Or is it better to  
move all the codes from models to controllers? What do you think? Thank  
you.

models/ssh.rb  
$LOADED\_FEATURES[$LOADED\_FEATURES.length] =  
‘net/ssh/authentication/pageant.rb’  
require ‘net/ssh’

class Ssh

def self.execute(cmd)  
ssh=Net::SSH.start(“ahnibm71”, “testlib”, :port =\> 19207, :paranoid  
=\> false, :auth\_methods =\> [“publickey”], :passphrase =\> “hellokitty”,  
:keys =\> [“C:/testing/id\_rsa”])  
output = ssh.exec!(cmd)  
ssh.close  
if output.nil?  
return “NULL”  
else  
return output.gsub("\n", “  
”)  
end  
end

end

controllers/ssh\_controller.rb  
class SshController \< ApplicationController  
def main  
end

def test  
render :text =\> Ssh.execute(params[:command])  
end

end

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 6, 2009, 6:13am UTC](https://www.ruby-forum.com/t/mvc-architecture/156985/2 "2009-01-06T06:13:40Z")

</div>

On Jan 5, 11:10 pm, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Dear all
> 
> In the MVC architecture framework, the Model is actually communicate  
> with database. In my rails project, there is a model/ssh.rb that is not  
> connect with database.

That’s fine. The model is the only part of the MVC architecture that  
is _allowed_ to communicate with the database, but it certainly does  
not _have_ to do so. The job of the model is simply to model the  
data. If it needs access to the DB to do that, fine. If it doesn’t  
need access to the DB to model the data effectively, also fine.

> Is it a good practice for this arrangement in my  
> project?

Probably, although I’m not certain about your particular case.

> Am I violate the rules of MVC architecture?

Not at all.

> Or is it better to  
> move all the codes from models to controllers?

Probably not. The trend in Rails development at the moment seems to  
be “skinny controller, fat model”.

In other words, you’re doing fine. Don’t worry.

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 6, 2009, 6:31am UTC](https://www.ruby-forum.com/t/mvc-architecture/156985/3 "2009-01-06T06:31:46Z")

</div>

It would also be appropriate to place the ssh.rb file in the lib/  
directory. Placing it in app/models/ is fine. It’s just a matter of  
preference.

On Jan 5, 10:10 pm, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 7, 2009, 2:46am UTC](https://www.ruby-forum.com/t/mvc-architecture/156985/4 "2009-01-07T02:46:23Z")

</div>

Valentino L. wrote:

> In the MVC architecture framework, the Model is actually communicate  
> with database. In my rails project, there is a model/ssh.rb that is  
> not  
> connect with database. Is it a good practice for this arrangement in  
> my  
> project? Am I violate the rules of MVC architecture? Or is it better  
> to  
> move all the codes from models to controllers? What do you think?  
> Thank  
> you.

MVC has two meanings. The most important meaning is a set of Guidelines  
(_not_  
“Rules”) for where to put what logic. For example, if you put HTML code  
in the  
Controller or Model, you had better be prepared to answer why.

MVC’s other meaning is the answer to these questions for most GUIs:

1. do all your tests pass?
2. is your code simple and easy to read?
3. are you as DRY as possible without violating 2 (or 1)?
4. is your code minimal, without run-on modules?

The game is to apply each item, in order, while not breaking the  
previous items.  
Don’t DRY your code (meaning Don’t Repeat Yourself) if you break tests,  
or make  
your code unreadable. Don’t remove lines of code if tests break? (That  
is  
“refactoring”.)

The ideal of MVC is that a good design that follows those guidelines  
ought to  
automatically fall out as three layers, with the data accessors in one  
layer;  
the “switchboard operators” in another layer, plugging everything  
together, and  
the views in another layer, stretching it all out for the users. So, in  
theory,  
if you apply those Simplicity Guidelines an MVC pattern ought to emerge  
for you.

Put anything you like in the models folder, so long as it’s something  
that  
controllers mix and match together.

> models/ssh.rb  
> $LOADED\_FEATURES[$LOADED\_FEATURES.length] =  
> ‘net/ssh/authentication/pageant.rb’  
> require ‘net/ssh’
> 
> class Ssh
> 
> def self.execute(cmd)  
> ssh=Net::SSH.start(“ahnibm71”, “testlib”, :port =\> 19207,  
> :paranoid  
> =\> false, :auth\_methods =\> [“publickey”], :passphrase =\>  
> “hellokitty”,  
> :keys =\> [“C:/testing/id\_rsa”])  
> output = ssh.exec!(cmd)  
> ssh.close  
> if output.nil?  
> return “NULL”  
> else  
> return output.gsub("\n", “  
> ”)

Firstly, this mixes HTML and Model. That   
 is not DRY, because it  
repeats the  
concept of a view down here. Remove that gsub, and render your output  
like this:

```
<%=h output %>
```

Nextly, all your code should be pure XHTML (google assert\_xpath for more  
on this  
subject!), so you should have used   
. XHTML makes your pages  
super-easy to  
test, and this, in turn, keeps your code quality extremely high. Lack of  
tests  
is a much, much bigger problem than any variations in MVC ideals!

> ```
> end
> 
> ```
> 
> end
> 
> end
> 
> controllers/ssh\_controller.rb  
> class SshController \< ApplicationController

Does this control SSHs? What is the concept on the SSH wire that’s so  
important?  
Is this a “RemoteCommandController”?

> def main

If you have a main.html.erb file, and if it takes care of itself, you  
don’t need  
this action in here.

> end
> 
> def test  
> render :text =\> Ssh.execute(params[:command])

Exactly. Models are typically things that respond to Hash options, and  
yours  
does. But put the

```
tag up here, like this:

```
  render :inline => "<pre><%=h Ssh.execute(params[:command]) 

```

%\>
```
"

Why did I use :inline, instead of :text with a #{} masher? Because h()  
is not  
visible at this level, and :inline opens up a little bitty View inside  
your  
Controller.

Yet your existing design was already very DRY, so these are only  
suggestions.  
The only technical fix is the h().

Further, shouldn’t some other Model object intermediate between the raw  
SSH  
layer and the Controller? What concept is is the SSH object?

–  
Phlip
