How to use acts-as-commentable-with-threading in Rails - ruby-on-rails

I am developing my first rails site (yup, i am a rails idiot).
I'm writing a blog, and i got to the comments part.
I installed acts-as-commentable-with-threading ( GitHub ), i made and ran the migration like the install instructions said.
I have added acts_as_commentable to my Posts model and i have a Comments controller
When i add
#comment = Comment.build_from(params[:id],1, params[:body] )
I get the error.
undefined method `build_from' for #
Clearly i am doing something terribly wrong, and i don't really get the example. What should i be feeding to build_from? Can somebody explain this plugin step by step? :)
Or is there an easier way to get simple threaded comments?

Did you by chance define your own comment model? If so that is going to completely override the model from the plugin that defines build_from in the first place. I ended up getting around this by creating a module with the extra stuff I wanted then creating an initializer to include it, which works perfectly.
As an aside, the first parameter to build_from needs to be the actual commentable object the comment is to be connected to, not just an id.
I'm currently using this plugin in production and can assure you it works :)

Besides the reason of not restarting server (btw you shouldn't use nginx + passenger for development, simple mongrel or thin will do the job better in this case) I can think of two more:
You didn't install plugin (or something wrong happened during installing). However this is unlikely as you could run migration ok right?
You have comment model in app/models and rails doesn't load it from plugin. In this case you might want to try requiring file with plain old require.

Related

Add a basic UI layer to existing ruby app

First, I am fairly new to Ruby/RoR and so you'll have to forgive me for any wrong terminology, but hopefully I'll get my point across.
I built an ruby app that I am needing to add an extremely simple UI layer using rails. Read up on a previous post of mine that explains the project thoroughly to give you good an idea of what it does. Specifically take a look at the tree outline that I pasted in so you see the existing file structure for the project.
What I need to know, is how to convert this existing project into a rails app? My experience in building something with rails has always started out with rails new app_name, but never anything like this. Any tips would be appreciated.
I saw your parser script, and it is not a daemon (a program that keeps running indefinitely in the background), right?
If I'm right, then you have several options:
The easiest option
Just build a rails application using rails new app_name, and inside some controller action, make a system call to run your script
class SomeController
def some_action
succeeded = system(:ruby, '/path/to/main.rb', '/path/to/some.txt')
# Do some rendering stuff here based on the result of the system call
end
end
This approach is somehow nasty for me, and it's not performant because each system call reads your ruby script and compiles or interprets it then runs it.
The harder option
Refactor your script so that it's features can be wrapped into a gem.
Then you install that gem, require it in your rails app, and use it.
I saw your original ruby script is almost there, it shouldn't be that hard to make it become a gem.
Rails is just "something" on top of Ruby. Especially, you can use any plain ruby objects inside of Rails, anywhere, and this is nothing unusual (google "PORO").
In your case, I would make a simple Rails app in the way you have mentioned yourself with rails new. Then trivially refactor your existing code until you have a simple, standalone class that does what you need to be done but takes its input/output from simple ruby data structures (i.e., method arguments, return values, no global state, no file operations). Then you can use that class from inside your Rails controller (taking input from a HTML form, rendering output to HTML), and also from inside your script (reading input from a file or STDIN, rendering output to STDOUT).
Where you put that class is up to you. In the MVC paradigm, it is not "C" or "V", and one could argue about whether it's "M". So put it into app/models/ or lib/, whatever you like more.
These were great answers and I'm sure they would have worked perfectly. However, they were a little bit more complex than what I was looking for.
What I ultimately ended up doing was just cd into the directory above where the ruby app was located and then just simply ran rails new app_name. Rails will ask if you'd like to overwrite any files that exist already. From there I just integrated my script into the controller actions and created the views.

Models not reloading in development in Rails (3.2.11) project

I've searched fairly extensively for any advice and have yet to find it so, here goes:
My Rails project fails to automatically reload models in development. Reloading them currently requires a full server restart.
Previous instances of this issue have been related to non-activerecord files placed in the models directory, though this is not the case for me.
config.cache_classes is properly set to false in my development config file. Views and controllers reload without issue.
All of my rails components are version 3.2.11. I have tried disabling all of my development-specific gems to no avail. This is obviously not a productivity stopper, but it is quite an annoyance. Any help appreciated and I am happy to provide more information if it would help, though I am not using any exotic gems.
Thanks!
Some possibilities:
You are not really running on developement environment
You are changing a model within a namespace and didn't told rails to autoload the path
You are changing a file that is included in your class, not your class directly (or any of the many variants for this)
You are caching classes
Considerations:
Things might change according to the webserver you are using
How do you know it's not reloading?
I ask my question because I was having the exact same issue when I was trying to insert a debugger into what I thought was a piece of code that was being executed. I assumed the model wasn't being reloaded since it was not hitting the debugger but it was actually a call back that was redirecting me around the code with the debugger line in it.
So, it might be something other than your models not being reloaded.

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.

Starting out with vote_fu

Trying my luck with the vote_fu rails plugin. The functionality looks like exactly what I need for a project of mine, but I have hit a roadblock. I have followed the github readme to the letter, installing it as a plugin.
I have put acts_as_voteable on my "Event" model and acts_as_voter on my User model.
In the console, when I try:
>> event.votes
or
>> user.votes
it successfully returns an empty array.
but when I try to do the following:
user.vote_for(event)
I get
"NoMethodError: undefined method `user_id' for #<Vote:0x7f5ed4355540>"
Any ideas? I'm probably just missing something obvious, but maybe something is missing from the plugin's readme.
Thanks.
[Update]
I created a blank application and the plugin works fine, so I think that this problem might be being caused by the use of the "desert" plugin, as my User class is split over two files. If I find the answer, I'll post it so that in the off chance someone else runs into this it may be some help.
Cheers.
Ok, I haven't found a definitive reason why this problem occurred, but I do have strong suspicions that it is due to the use of the "desert" plugin, as my User class is split over two files (the project is using the CommunityEngine plugin as a base),
Anyhow, I found a work around (something I should have tried before posting here). Instead of installing vote_fu as a plugin, I installed it as a gem. And now vote_fu seems to be humming along nicely.
So let that be a lesson to you all!
:-P

has_many_polymorphs tagging - works on development machine, not on production!

I'm having a weird problem, where tagging works fine on my development machine, but when I deploy to the production server, I get this error in the log:
ActionView::TemplateError (undefined method `tags' for #<Person:0x98bb9d4>) on line...
There is an entry in the production.log file that states that has_many_polymorphs is loaded, so it's not like the plugin isn't available on the production machine.
My Google-fu has failed me trying to find the answer, so if anyone knows what could be wrong it would be greatly appreciated!
Edit: I should have mentioned that on both production and development I'm using the same database. I downloaded the production one, and used it on the development machine and it works fine.
cap deploy:migrations
I've seen similar problems to this in which the polymorphic type field is not getting correctly filled in, or when there was some existing data prior to the polymorphic type tag getting added. Is person a subclass? does the _type field contain any null values on the polymorphs table?
Just stabbing in the dark here, but has_many_polymorphs doesn't natively add tagging functionality to your models. Instead, you use a generator to create a tagging extensions module that goes into lib/tagging_extensions.rb. The module file has helper methods that add tagging functions, built on top of the has_many_polymorphs base functionality.
So, is it possible that you have the plug-in installed, but not the tagging extensions file?
I spent some time with a consultant tracking this down, and eventually we discovered that for reasons unknown, the Tagging stuff just wasn't being loaded.
By adding a single line of code, just three letters, to the end of environment.rb, it was resolved. I commented it so that we'd never forget wtf was going on:
# Magic begins here.
# We need to force Rails to load the Tag record, or
# has_many_polymorphs doesn't work properly. I don't know
# if there's a better fix, but this one seems reasonable. :-/
Tag
That was it. I'm sure there's an elegant and proper solution to this, but this works. Weird.
I hope this helps someone out there.

Resources