2

Both my Rails model and controller code need to write files to the file system.

I would like to consolidate the logic into one method.

What's the best way to share this method across models and controllers?

Thanks!

2 Answers 2

2

I think the controller would defer the actual execution of writing a file to the file system to the model. While the controller is allowed to decide when to execute that code, it should not be responsible for it's implementation, So that code should really only be in the Model.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks, matthew. the problem is, the model needs to write a file that contains the contents of a render call. in other words, i'm writing a static version to disk of the page rendered by Email::Show. what do you recommend?
I'm not sure I completely understand what you are trying to accomplish, but it seems as though your controller knows what to write and that it should be written, which is fine. All you need to do is to use a File Writing method in your model that can take the data passed in from the controller and write it to the file system. If I am completely missing the point, let me know and I will do my best to correct my response.
OK, that could work. My question is: why should the email model know file paths and how to write out files, yet not know how to generate URLs (say, the email required a special token and therefore unique URL for the user to click in order to delete the email)? In the URL case, I'm supposed (I think) generate the URL from the controller or from the view, and pass in necessary data from the email model. Yet with the file system, I'm passing in data to the model and supposed to generate the file path (and the file) within the email model. Does this make sense?
First, I wouldn't let my model know where to save a file either, I would place that in a configuration file or something of that nature so that it can be changed without a recompile.
Second, I think the difference is in defined responsibilities. The role of the controller is to take the request from the user and issue the appropriate commands to the model and view to achieve the desired results. It can be argued that part of those responsibilities is putting together the URL. Otherwise the Controller is your commander on the field, it knows what to do for a given user action, and issues orders to the other two layers, but it should not actually be concerned with how that work is done.
|
2

If you really need to do this, you could place a module in /lib and include it where needed.

However, if possible you should have your model take care of it. If you can supply some more details, it would be easier to steer you in the correct direction.

3 Comments

thanks, matthew. the problem is, the model needs to write a file that contains the contents of a render call. in other words, i'm writing a static version to disk of the page rendered by Email::Show. what do you recommend?
I would recommend doing it as Radar recommends, and passing the string representation of the render as the parameter to the model, which would then write it out to the file. That way the controller decides what to render, the model decides what to do with it.
thanks, guys. the problem has shifted: i'm handling an inbound message through ActionMailer::receive. this method gets invoked through a script run by qmail, when a certain user receives email. unfortunately, from within the ActionMailer model, i have no way of accessing render() to render_to_string() ... how can i write the static file to disk w/o duplicating code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.