# Pass the current controller to the model

**URL:** <https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235>\
**Category:** Rails\
**Created:** [July 24, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235 "2007-07-24T15:08:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 24, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/1 "2007-07-24T15:08:58Z")

</div>

In the end under the model I need to beable to do this  
dirname = “/public/shows/#{controller.controller\_name}/”

Thats it really. I am hacking a swfupload script and need to be able to  
break it into lots of controllers so duplicating files is just insane if  
I can not get this to work

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 24, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/2 "2007-07-24T16:33:32Z")

</div>

I have no idea what you’re saying… but if you need to pass things to  
the  
model, make an attribute accessor in the model.

class MyModel \< ActiveRecord::Base

attr\_accessor :some\_var

end

Then you can do

my\_model = MyModel.new  
my\_model.some\_var = “test”

my\_model.some\_var  
(should return “test”

That’s how I pass lots of things to my models. Either that, or make a  
whole  
method in the model that does what you want and have it take parameters.

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 24, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/3 "2007-07-24T17:05:05Z")

</div>

i will try this thank you.

What I am trying to do to change the string block in my Model to swap  
out the code for the controller that is accessing it\> Example code  
#model  
def dirname  
dirname = “/public/shows/#{controller.controller\_name}/”  
end

I need it to turn into this  
#model  
def dirname  
dirname = “/public/shows/caves/”  
end

I cant get it to see the controller so far. The controller is simply  
def upload  
@box = Box.new  
end

Infact it does not need the object creation. @box = Box.new  
Just the view. Everything seems to happen in the Ã¸ne attachment model  
and the huge lib file I am trying to hack out the need for the model.

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 24, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/4 "2007-07-24T17:41:54Z")

</div>

Ok I tried it. I am getting

Ok I tried it, but it it just puts the file one directory above

#model

class Attachment \< ActiveRecord::Base  
belongs\_to :attachable, :polymorphic =\> true  
attr\_accessor :seemee  
#def self.get\_upload\_dir(controller)

# upload\_dir = “public/attachments/#{controller}/”

#end

def dirname  
@seee = seemee

```
##### This is working without the name.... weird....

```

# dirname = “public/attachments/#{self.controller}/”

```
dirname = "public/attachments/#{@seee}/"

```

end

end

#Controller  
def new  
@fish = Fish.new  
@box = Attachment.new  
@box.seemee = “public/attachments/fish/”

# @fish.seemee = “public/attachments/fish/”

#@fish.get\_upload\_dir(controller)  
end

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 24, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/5 "2007-07-24T18:58:58Z")

</div>

Brian H. wrote:

> You might actually want to look at the attachment\_fu or file\_column  
> plugins. They do what you want to do.

Ah but mine is a Flash swfupload tool  
[http://swfupload.mammon.se/](http://swfupload.mammon.se/)  
it is the only one I can find that has a working progress bar  
[http://developer.assaydepot.com/?p=6](http://developer.assaydepot.com/?p=6)

But my issue is that I have a lot of separate shows that have to keep  
there files separate from the other shows.

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 24, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/6 "2007-07-24T18:38:11Z")

</div>

You might actually want to look at the attachment\_fu or file\_column  
plugins. They do what you want to do.

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 25, 2007, 12:05am UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/7 "2007-07-25T00:05:41Z")

</div>

I understand that… what I meant is look at their source code and see  
how  
they handle this. file\_column has /public/model\_name as the default file  
location, and the model handles the actual receiving and writing of the  
file to the right location. Check it out.

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 25, 2007, 2:57am UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/8 "2007-07-25T02:57:44Z")

</div>

Brian H. wrote:

> I understand that… what I meant is look at their source code and see  
> how  
> they handle this. file\_column has /public/model\_name as the default file  
> location, and the model handles the actual receiving and writing of the  
> file to the right location. Check it out.

Okay so I did do as you said, it seems to just be  
File.join(model, attr) the controller has nothing to do with it at all.

As for the controller. I have tried more things

#controller  
def new  
@fish = Fish.new  
@box = Attachment.new  
@box.seemee = “public/attachments/fish/”  
end

#model  
def dirname  
@seee = self.seemee  
dirname = “public/attachments/#{@seee}/”  
end

But no matter what I do it acts like by the time it creates the model  
for operation it is to late to send the next line @box.seemee =  
“public/attachments/fish/”  
It’s like it HAS to reload the model or stall or insert the data before  
making the model, or during.  
The before\_filter does not do the trick though… BAH headaches!

---

<div class="post-metadata">

**Author:** ![tripdragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tripdragon](https://www.ruby-forum.com/u/tripdragon)\
**Post date:** [July 25, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/pass-the-current-controller-to-the-model/110235/9 "2007-07-25T16:21:42Z")

</div>

Trip Dragon wrote:

> But no matter what I do it acts like by the time it creates the model  
> for operation it is to late to send the next line @box.seemee =  
> “public/attachments/fish/”  
> It’s like it HAS to reload the model or stall or insert the data before  
> making the model, or during.  
> The before\_filter does not do the trick though… BAH headaches!

So far no luck still. Now I am trying the Mongrel tool again , but might  
anyone have any other clues ?
