开发者

Need some advice on doings things the Rails way

开发者 https://www.devze.com 2023-03-24 22:15 出处:网络
I\'ve built a Rails app that\'s pretty simple... you send a request to a certain web page, it automatically generates a report and saves it to a file locally on the server. The report that generates i

I've built a Rails app that's pretty simple... you send a request to a certain web page, it automatically generates a report and saves it to a file locally on the server. The report that generates is by default created for the current day. What I would like to do is allow the user to enter the date in the url which they would like to have the report generated for turning my little one trick pony into something a little more useful.

As it stands right now I've built all of the useful report generation code into the index action of the the home controller but can't help but feel that it really belongs in a model instead of the controller, but I'm not quite sure how to make that transition.

Any advice from you seasoned rails vets would be greatly appreciated.

Thanks.

The code in my index action.... Basically the index action get's called and my program figures out what day it is, then uses the date to make a request to another API which then returns the necessary information in the form of an object. I then simply go through the object, gather the info I need and generate a text file with that information. The view for the home controller just prints a message saying the report has been generated and provides the path to it.

I would like to stress that I would like to use a url like mysite.com/report/20110803 to provide my controller with the information about the date the report should be run f开发者_如何学Goor.


You are thinking correctly that this code belongs to a model. Ideally, the Report model. A controller should generally just feed the view with instance variables, handle flash messages and so on. The logic should be in the model.

That said, i would do it like this: Create a model method that generates the report you want. Then, execute that method in the controller and get back the report you want based on the timestamp that is passed into the model method.

Then, would have this in the controller:

redirect_to new_report_url, :stamp => the_stamp_value

I think you get the idea. You just need to have a simple route that expects a stamp value and you are good to go :)

0

精彩评论

暂无评论...
验证码 换一张
取 消