Rails routing problem, action being skipped - ruby-on-rails

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).

Related

Rails controller handling discrepancy development/production

Summary: in development, requests are routed to workouts_controller.rb but in production to workouts_controllerPrev.rb
In a Rails 5.2.3 app, I have the file workouts_controller.rb in the controllers/ folder with first line:
WorkoutsController < ApplicationController
I took a copy of workouts_controller.rb (to serve as a quick reference backup) which I renamed to workouts_controllerPrev.rb and retained in the controllers/ folder.
I then introduced some new functionality to workouts_controller.rb. I tested the new functionality locally (it worked as expected in development) and then I deployed to Heroku (v 7.42.0) (for production).
The new functionality, however didn’t work in production. After some debugging, I identified that in production, the WorkoutsController class in workouts_controllerPrev.rb was handling calls to the Workouts controller (rather than the Workouts controller defined in workouts_controller.rb (as anticipated and as happening in development).
I made a more dramatic name change to workouts_controllerPrev.rb, changing it to Xwurkouts_controllerPrev.rb, and changed the class name in this file to XWurkoutsController redeployed and it all worked fine.
What is happening here? Why would Rails function differently in this respect between the 2 environments? Is this a bug or an unsurprising consequence of a bad practice of having unused files loitering around? If a bug, where should I report it?
I am using SQLite in development, and PostGreSQL in production, but I don’t see this can be a database issue? The production webserver is Puma.
Thanks for any guidance
Daniel
The issue is that both files have the same class name in them, so the actions (methods) in whichever one is loaded last will override the actions defined in the one loaded first.
If you want to keep both files around and not have surprising results, change the class name in the old file to something else like PrevWorkoutsController.
Or, save it in a branch in Git so it doesn't clutter your current code.
To answer about why you got different results in different environments, it is because of the difference between autoloading vs. eager loading. Rails uses autoloading in development, but it eager loads everything up front in production, then turns autoloading off.
In other words, in development, Rails will reload the class from its matching file any time that file is saved. In production, it simply loads all files up front, so whichever one it loads last will win.
You can read more here.

Grails 2.0.1 controller action methods have stopped working

My project has always used grails 2.0.1 and my controllers define their actions as methods not closures. Previously these actions have always worked (i.e. the browser can render the correct gsp page first going through the action method in the controller).
Recently we've noticed that the bespoke actions no longer work, the browser reports a '404 resource not found'. We're still on grails 2.0.1, this is confirmed by 'loading grails 2.0.1' comments in every grails command that is run. Although there are some controller/domain/services changes, none of those changes should affect the use of action methods. From what I can tell, we've not downgrade the version of grails/groovy.
If I change the actions to be defined as closures, then it works fine. But I'm not happy with this as my solution as methods are the preferred way and it used to work.
I've tried the usual approach to grails weirdness: proper clean and rebuild, but no joy. Also, this is an issue in both eclipse and unix envs (project delivered as a war run by the grails command), so it must be something in the project files but I cannot spot anything that has changed.
Any suggestions what could have happened to my project and how to resolve?
I've finally tracked my problem down to an aspect I was using to monitor long running method calls. I had changed the pointcut to include 'within(com.mydomain.domain..*)' which is where my Controller class resides. This appeared to stop my bespoke links from working, not entiring sure why. I hadn't intended this, I just wanted my pointcut to include all domain class methods but I'm happy to sacrifice that in order to get controller method actions working again.
I've resolved this issue by amending my pointcut.

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.

Reusing Code from Another Rails App

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)

Resources