Reusing Code from Another Rails App - ruby-on-rails

I am trying to reuse some code from another rails application I had worked on earlier. I copied over all the models / views / controller / migrations and ran rake db:create and migrate. Now when I try to run the application the initial page for this view (the one that has the list edit/delete) loads fine and shows there are 0 records. When I click new, however, it displays an error message on a bit of code created by the scaffolding in my other application...
<%= link_to 'New comment', new_comment_path %>
undefined local variable or method `new_comment_path' for #<ActionView::Base:0xb67c9690>
Should I be able to reuse this code or is their something else I need to do to make sure that I have everything moved over. I tried using grep to find where the 'new_comment_path' was being defined in my other project and I only saw it used in a similar context to what is listed above. Any help would be appreciated.

This error shows that you are missing routes.
Check in the conf/routes.rb file in the App you're copying from.
Most probably all you need to do is add
map.resources :comments
to your routes.rb
And add that for all the controllers that you copied.

If you have time, or expect to do this again, you might consider making the reuse candidates into a plugin, or use the engines feature in 2.3 (or install the rails_engines plugin for slightly older versions)

Related

RoR undefined method `attr_accessible' after any code edit

I'm implementing a great plugin I found for awarding points/badges/ranks to users based on user behaviors. It is called Merit and can be found here: https://github.com/tute/merit
I have two models: "Post" and "Tag". Associations are: posts have many tags, tags belong to a post.
I am awarding points to users when they create a "Post." I configure the plugin to award points whenever the "create" action of the posts controller is called.
The problem:
When I start the server, I go through my flow for creating a new post with any associated tags. This works perfectly. It will work perfectly until I edit my code (in controller or model) in any way. After the edit, when trying to create a new post, I will get this error:
undefined method `attr_accessible' for Merit::Action:Class
It doesn't seem to matter what kind of edit I make, as long as I change the fundamental content of the file (this includes simply adding a debug "puts" statement). At this point, I can no longer create posts at all, as every attempt to create a post will result in the same error. Then, once I restart WEBrick, it will work again (until I edit code again).
What is happening here? Howcome the simple act of editing my code causes attr_accessible to suddenly become undefined in the Merit plugin until I restart my server?
This seems to be a ruby class reload issue.
I think your plugin is not being reloaded along with the files you change, so the gem's behaviour is not re-injected into the new code. When you restart the rails server the app's code and the gem's code are in sync and gem's behaviour can be injected in app's code.
Does it make sense?

Rails generate scaffold creates blank controller

The last couple of times that I've used 'rails generate scaffold [ModelName]' everything has been generated except that the controller is blank. It contains no methods at all. It's easy enough to copy that in from other sources, but I'm wondering what is going on.
The only unique thing about this application for me is that it's using the ActiveAdmin gem.
Any suggestions for how I could get this working as expected again?
+1 to hajpoj, but there are a couple additional steps you could use to troubleshoot.
What does rails generate scaffold_controller give you? My first suggestion to be to isolate the controller generator and go from there.
Following that, I would actually look in the Rails generator code at the point of controller generation and work backwards from there. Here is (I believe) the entry point, from there, you can follow the code to where things are failing. This is, obviously, not the easiest path, but would probably teach you a lot about the rails internals.

Rails routing problem, action being skipped

I'm having a bizarre issue where it seems as if Rails is skipping the run of my particular Action. I have two environments that I am running this in. One (development) works fine and runs the action. The other (staging) is not running the action.
The error is that Rails can't find a template in the views directories for my given action, which is only supposed to respond with JSON (no template). I've done logging in the action and it just simply isn't being run. Rails immediately fails saying that the view doesn't exist.
Just to cover my bases, I've verified that the code is indeed the same, that my routes file is exactly the same, and that my rails version (3.0.1) is exactly the same between the two env's. Any help would be excellent here.
Apparently this comes in the department of facepalm. One of our developers had committed a new controller with a different file name but the same controller class name as another. It must be that in development rails was loading the new controller first, and so the old controller would override it and the issue was hidden. In stage however, it seems that the new controller was loaded last, which cannibalized our controller class and method, screwing everything up.
I'd be interested to know if others have encountered this problem in rails. May need to patch the controller loading code to always use the same sort mechanism (seems like file name would be most natural).

ruby on rails - open flash charts

I'm trying various ways to implement pretty graphs in my app. I followed http://pullmonkey.com/projects/open_flash_chart2 steps in my app, but instead of creating test_it, I just added to my own controller. In my routes.rb, I have
resources :my_controller_name do
collection do
get 'graph_code'
end
end
also, since I don't think it could find the action my_controller_name/graph_code otherwise (I'm kind of confused as to why most tutorials leave the routes part out? Am I doing something wrong?). (I also used #graph.html_safe in my views for Rails 3)
Anyway when I go to /my_controller_name, I get the error in the flash box:
Open Flash Chart
JSON Parse Error [Syntax Error]
Error at character 0, line 1:
0: #<OpenFlashChart::OpenFlashChart:0x000001043c4b78
I don't know why this is happening. I tried creating a new app and following the guide, generating a controller named test_it. It worked (provided I corrected the routing). I can't think of how my controller is any different than test_it, except I initially generated it using a rails scaffold (so it has all the MVC parts), whereas I only generated the test_it controller. (I've also tried using/not using the json gem conflict fix, but it didn't make a difference--https://github.com/klochner/open_flash_chart/commit/00cf531387880af8c49ed5118737f0492b437f75) Thanks for any insight, I'm stumped as to why it's easy to implement on a new app but I can't add it to mine...
Thanks.
Nevermind, it seems to work if I use the old lib/base.rb instead of the json gem fix. Ah well.
Don't waste your time with Flash based charting libraries. Highcharts is definitely the way go to. It works in pretty much any browser, including those that don't support Flash. Much better performance on OS X than Flash will ever provide.

Renaming controllers in Rails and cleaning out generated content

I was following along with the railscast regarding the restful_authentication plugin.
He recommended running the command:
script/generate authenticated user session
Which I did, and everything generated "fine", but then sessions wouldn't work. Checking the site again, he mentions a naming standard and listed updated code which stated:
script/generate authenticated user sessions
With sessions being pluralized.
So now I have session_controller.rb with a SessionController in it, but I guess by naming standards, it is looking for SessionsController, causing the code to fail out with the error "NameError in SessionsController#create "
I see the problem, which is pretty obvious, but what I don't know is, how do I fix this without regenerating the content? Is there a way to reverse the generation process to clear out all changes made by the generation?
I tried just renaming the files to sessions_controller with e SessionsController class, but that failed.
While writing this, I solved my own problem. I had to rename session to sessions in the routes file as a map.resource and rename the view directory from session to sessions, and update session_path in the html.erb file to sessions_path.
So I solved my problem, but my answer regarding removing generated content still remains. Is it possible to ungenerate content?
Actually, script/destroy works for any generator - generators work by reading a script of sorts on what files to create; script/destroy just reads that script in reverse and removes all the files created, as long as you give it the same arguments you passed to script/generate.
To sum up: script/destroy authenticated user session would have removed all the generated files for you, after which you could have run script/generate user sessions without a problem.
I've never tried script/destroy, but if you're reverting changes that you just made, the generate command should give you a list of files added and changes made. If you're using a version control system of some sort, running status/diff might help as well.
You can just roll back to the previous revision in subversion, and start again, right? right? :-)
rails has script/destroy for 'ungenerating' stuff, but I suspect that will only work for the stuff rails ships with, not the restful authentication plugin.
I'd say your best bet is find-in-files (or grep -R if you're not using an IDE) - find everything that refers to your old SessionController and change it

Resources