Is this a good solution to use if you have a model that needs to 'has_many' photos? its not clear how you would adapt this to the situation where you need to have a variable number of photos associated with a model.
We use paperclip on a current project and it works good except for:
1. When you delete an attachment it does not delete the directory it created for the file. Thus over time you are left with hundreds of empty directories on your server.
2. Take an Outlook email and drag it to your desktop, it'll create a .msg file. Now try to upload that file using a webkit based engine (Chrome, Safari) and Paperclip will crash. No problem with FF or IE.
I've tried to get a solution from Paperclip Google group but the developers have been unresponsive to the second problem and don't think the first problem is a problem.
Great screen cast! I have one problem I came across. I want to add the condition "published_at IS NOT NULL" but I can't find a way to do this as a hash and just adding the string doesn't work. Any ideas?
Let me be more specific(apologies). I have :multiple => true in a :collection_select helper, however can't get multiple choices to be selected after I submit. (I can however get one to be highlighted if that's all I initially choose) I am sure it's some hash/array problem with my collection_select(model, object, collection) but can't figure it out.
If I can see params[:model][:object] data inmy controller, shouldn't @model = Model.find_by_id(params[:model][:object] be ok as my instance to set the selected with an :object accessor method?
Hi Ryan! One question! Is it possible to make Paperclip work if I need my model to have many attachments? If it's not possible, maybe you know some other plugins?
Thanks Ryan for that railscast! A few weeks ago I was build a function like this in my app with attachment_fu and ImageScience. Are there any drawbacks in relation to your solution?
I am having problems with collection_select and getting :selected to work correctly. I am trying to use it as a search filter for a model that has associations through a join table (second model) with a third model used in the collection_select. I am very new to all of this, but enjoy the casts, no doubt.
Thanks for posting the topic, it's great idea! I'm getting this in the log file smiliar to comments above, any ideas?
# Logfile created on Sun Nov 02 11:16:11 -0800 2008 by /
*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
#<Gem::Exception: can't activate activesupport (= 2.0.2, runtime), already activated activesupport-2.1.0>
*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
#<NoMemoryError: failed to allocate memory>
#<SystemStackError: stack level too deep>
#<fatal: exception reentered>
#<LoadError: no such file to load -- active_support>
Ryan,
you said you had problems stubbing class methods. I have similar issues. Other tests fail because stubbing seems to "carry over".
If I run a single rspec, it passes, but rake rspec fails, which makes me think something isn't being reset.
Do you have any suggestion what to do instead?
For example, in my controller, I do
Product.find params[:id]
it's tempting to do the following in a rspec
Product.stub!(:find).and_return(some_product)
This was a great post. Our app has a large number of tutorial flash videos in the public folder. I've already started building in the separate asset uploads. Thanks for the tips.
I am trying to use the redirect_to :back from an action, but I am using restful_authentication, and the action requires the user to be logged in (I have a before_filter :login_required in the controller), so when I am already logged in the :back works fine, but when it redirects first to the login page, when it gets to the redirection in my action, it tries to go back to /session/new...
So I started using this. I needed a persistence service, so I stuck an object on the queue and had the worker save it. Nothing was happening though. It turns out that I needed to add to my load path the model I was trying to use (require 'event'). Does the workling_starling_client not include models from rails??? It does not look like the rails cast does this? Is there some config I am missing?
@Anlek I hit the same problem with loading helpers into emails. After some googling it turns out that you can load additional helpers into action_mailer and ar_mailer. You just have to add a:
I found this cast extremely useful. There have been a number of times when I had to call a helper from the controller. Up till now I've been loading the helpers directly in, which is not ideal. I'm definitely going to be modifying my approach in the future.
Thanks for the great advice and keep up the good work.
Great screencast, one of many uses I have found for Capistrano is the ability to "automate" copying production database data to the development machine. Credit the code I re-used to http://marklunds.com/articles/one/339 although there are multiple other URLs with examples. It sure is useful to be able to automate repetitive tasks on the remote servers.
@Marcio, it's hard to say what the problem is without knowing the error message or the code. I recommend making a thread on a forum or mailing list where you can go into these details. It may have to do with a missing gem or config file.
Hey ryan, is there a good way to modify the will_paginate helper if you want to page or action cache the pagination.
rails envy had a tutorial on adding to the routes.rb the necessary route and what the new pagination link should look like, but is there a way to do that automacially in the view?
One problem I am having is creating a Capistrano for Workling/Startling. I have created a task to start workling, everything executes properly but the process is never started.
Anybody has succefully created a Workling client start capistrano recipe ?
Ryan,
Your screencasts are great. Like you, I had trouble understanding Capistrano at first. I found Ezra Zygmuntowicz's Deploying Rails Application book's Chapter 5 on Capistrano to be very helpful in getting the concepts straight, and after reading that last night had no trouble implementing a deploy script that I had been struggling with all week-end.
Bharat
Ryan, i totally agree - tests should be completely isolated from anything else and using fixtures (other than to clear required data) can be a real pain.
Thanks again for a great screencast :)
I'd also be keen to hear your views on testing models with and without database connectivity - I've written an article about it here: http://www.kirkbushell.com/articles/rspec-model-tests
Maybe you can talk a little about webistrano next time. So you can use capistrano in a website so you can easily manage a bunch of websites and you can share symlinks.
@Javan: Usually you don't copy from release to release. Anything that needs to be shared between releases should be put in the shared directory and symlinked from the releases/current directories.
Is there an easy way to access something like previous_release_path in the case that you want to copy something from the last release to the new release?
I've just been checking out ruby on rails. Looks nice but I think I would need this sort of thing for an efficient dev/testing framework, as I would not want to go live until I was confident in what was happening.
Excellent screencast! I was using an "after deploy" task to regen restart.txt and couldn't figure out how to get rid of the "missing pid" error. Moving it into the :deploy namespace was just what I needed.
Capistrano also has built-in support for git submodules, which won't get checked out by default. This is handy if you are tracking plugins or rails as submodules. Turn it on with this setting:
Awesome railscast Ryan. Thanks. Used it right away on a project.
I didn't have beta invitations previously in the project, but wanted to switch to them. I ran into a problem with updating the contents of old users (since they didn't have an invitation id and since the validation still required it).
For those of you having a similar problem my solution was to add an unless statement Proc on the validation.
I'm trying to expose a form in an admin interface with all fields and in the normal user interface with just a subset. To stay as dry as possible, I'd like to include (in the C sense) a file with the common fields defined. Rendering a partial doesn't cut it because:
1. Inside the partial, it blows up on every f.field reference because it doesn't know what f is.
2. I'd like to come back to the original form declaration to pick up the submit button declaration specific to the use.
Does anyone know how/if I can do this?
Hey, I have a great suggestion, what about mention on every episode that it is obsolete (if it is)? So, there won't be anymore stupid guys like me, trying to do like you describe in the episode and finding out that your code doesn't work with latest stable version. I can help you starting ranging the episodes: this one is obsolete!
Here's how to enable wildcard (*) or star searching with Thinking Sphinx: http://www.artofmission.com/articles/2008/10/24/wildcard-searches-with-thinking-sphinx
@chris: Good point, I can certainly see that -some- business logic can be applied in the model. Given your example however, wouldn't this be applied in the controller or view? (Unless of course it's a query limitation)
@pjam : The way build works is that it stores the child objects in an array in memory and it links that array to the parent object. When the parent object is saved, it saves all of the child objects at the same time with the proper parent id in place. So, for each of the Tasks, they are stored in an array and after the Project is saved, Rails recognizes the associated array of child objects and saves them as well, tacking on the Project's id in the project_id column (all auto-magically!); gotta love Rails!
What is the solution for a has_many :through association between models? I have Movie has_many Actors through Roles - do I build on the join-table Roles?
Someone enlighten me please, as I get stringify_keys error when i try to adapt this to the join-table model attributes!
Great work Ryan, have learnt so much from your work!
How would you use your app helpers in a mailer body. Assuming I had a signup email, and I wanted to use a helper from my users_helper.rb inside my signup.html.erb. The docs say I can use action pack helpers but make no reference to using application helpers.
Is this a good solution to use if you have a model that needs to 'has_many' photos? its not clear how you would adapt this to the situation where you need to have a variable number of photos associated with a model.
Thanks for any insight on this.
We use paperclip on a current project and it works good except for:
1. When you delete an attachment it does not delete the directory it created for the file. Thus over time you are left with hundreds of empty directories on your server.
2. Take an Outlook email and drag it to your desktop, it'll create a .msg file. Now try to upload that file using a webkit based engine (Chrome, Safari) and Paperclip will crash. No problem with FF or IE.
I've tried to get a solution from Paperclip Google group but the developers have been unresponsive to the second problem and don't think the first problem is a problem.
Great screen cast! I have one problem I came across. I want to add the condition "published_at IS NOT NULL" but I can't find a way to do this as a hash and just adding the string doesn't work. Any ideas?
@PanosJee - You can change the file type validations if you want to upload pdfs or docs. Paperclip can handle pretty much anything you want to upload
@Aditya Sanghi - If you want to attach multiple uploads to a model just create an assets model using paperclip and use a has_many relationship
Let me be more specific(apologies). I have :multiple => true in a :collection_select helper, however can't get multiple choices to be selected after I submit. (I can however get one to be highlighted if that's all I initially choose) I am sure it's some hash/array problem with my collection_select(model, object, collection) but can't figure it out.
If I can see params[:model][:object] data inmy controller, shouldn't @model = Model.find_by_id(params[:model][:object] be ok as my instance to set the selected with an :object accessor method?
I really really want to stress that uploading files is _easy_!
http://gist.github.com/20698
That gist is just a file upload, though. No file size and/or mime type validations there. Or imagemagick fanciness.
As far as I know there is a polymorphic paperclip plugin. Maybe that's what you are looking for, Alex.
I liked paperclip plugin, but I need to sort out what to do with many attachments.
@Alex
Have a look at this tutorial: http://www.practicalecommerce.com/blogs/post/432-Multiple-Attachments-in-Rails
It's for attachment_fu but it's the same concept for paperclip.
Also there is a polymorphic paperclip plugin, see this tutorial
http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/
Hi Ryan! One question! Is it possible to make Paperclip work if I need my model to have many attachments? If it's not possible, maybe you know some other plugins?
Thanks Ryan for that railscast! A few weeks ago I was build a function like this in my app with attachment_fu and ImageScience. Are there any drawbacks in relation to your solution?
I am having problems with collection_select and getting :selected to work correctly. I am trying to use it as a search filter for a model that has associations through a join table (second model) with a third model used in the collection_select. I am very new to all of this, but enjoy the casts, no doubt.
Thanks for posting the topic, it's great idea! I'm getting this in the log file smiliar to comments above, any ideas?
# Logfile created on Sun Nov 02 11:16:11 -0800 2008 by /
*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
#<Gem::Exception: can't activate activesupport (= 2.0.2, runtime), already activated activesupport-2.1.0>
*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
#<NoMemoryError: failed to allocate memory>
#<SystemStackError: stack level too deep>
#<fatal: exception reentered>
#<LoadError: no such file to load -- active_support>
Ryan,
although it has been said many times i have to say this one more time:
Your screencasts are absolutely outstanding and helped me A LOT.
Ryan,
you said you had problems stubbing class methods. I have similar issues. Other tests fail because stubbing seems to "carry over".
If I run a single rspec, it passes, but rake rspec fails, which makes me think something isn't being reset.
Do you have any suggestion what to do instead?
For example, in my controller, I do
Product.find params[:id]
it's tempting to do the following in a rspec
Product.stub!(:find).and_return(some_product)
Sorry the problem got fixed. It was a silly mistake. I had a space between || and =.
I am getting a
syntax error, unexpected '='
at params[:project][:existing_task_attributes] ||= {}
Can you help me out?
This was a great post. Our app has a large number of tutorial flash videos in the public folder. I've already started building in the separate asset uploads. Thanks for the tips.
I am trying to use the redirect_to :back from an action, but I am using restful_authentication, and the action requires the user to be logged in (I have a before_filter :login_required in the controller), so when I am already logged in the :back works fine, but when it redirects first to the login page, when it gets to the redirection in my action, it tries to go back to /session/new...
Is there any easy way to fix this?
So I started using this. I needed a persistence service, so I stuck an object on the queue and had the worker save it. Nothing was happening though. It turns out that I needed to add to my load path the model I was trying to use (require 'event'). Does the workling_starling_client not include models from rails??? It does not look like the rails cast does this? Is there some config I am missing?
@Anlek I hit the same problem with loading helpers into emails. After some googling it turns out that you can load additional helpers into action_mailer and ar_mailer. You just have to add a:
helper :name_of_helper
to the top of you email model class.
I found this cast extremely useful. There have been a number of times when I had to call a helper from the controller. Up till now I've been loading the helpers directly in, which is not ideal. I'm definitely going to be modifying my approach in the future.
Thanks for the great advice and keep up the good work.
Ryan,
Great screencast, one of many uses I have found for Capistrano is the ability to "automate" copying production database data to the development machine. Credit the code I re-used to http://marklunds.com/articles/one/339 although there are multiple other URLs with examples. It sure is useful to be able to automate repetitive tasks on the remote servers.
@Marcio, it's hard to say what the problem is without knowing the error message or the code. I recommend making a thread on a forum or mailing list where you can go into these details. It may have to do with a missing gem or config file.
Hey ryan, is there a good way to modify the will_paginate helper if you want to page or action cache the pagination.
rails envy had a tutorial on adding to the routes.rb the necessary route and what the new pagination link should look like, but is there a way to do that automacially in the view?
You must put map.static '*permalink', :controller => 'pages', :action => 'show' to ride this.
Using the helper method link_to with the :back option would be the best for displaying the "Continue shopping" link.
link_to('Continue shopping', :back)
This is better because it will link to the referrer if it is present or "javascript:history.back()" when there is no referrer.
One problem I am having is creating a Capistrano for Workling/Startling. I have created a task to start workling, everything executes properly but the process is never started.
Anybody has succefully created a Workling client start capistrano recipe ?
Ryan,
Your screencasts are great. Like you, I had trouble understanding Capistrano at first. I found Ezra Zygmuntowicz's Deploying Rails Application book's Chapter 5 on Capistrano to be very helpful in getting the concepts straight, and after reading that last night had no trouble implementing a deploy script that I had been struggling with all week-end.
Bharat
Ryan, i totally agree - tests should be completely isolated from anything else and using fixtures (other than to clear required data) can be a real pain.
Thanks again for a great screencast :)
I'd also be keen to hear your views on testing models with and without database connectivity - I've written an article about it here: http://www.kirkbushell.com/articles/rspec-model-tests
Thanks for the screencast.
Maybe you can talk a little about webistrano next time. So you can use capistrano in a website so you can easily manage a bunch of websites and you can share symlinks.
@Javan: Usually you don't copy from release to release. Anything that needs to be shared between releases should be put in the shared directory and symlinked from the releases/current directories.
You lost me on the last step... I snapped my fingers, but my code didn't auto-document. Am I missing a gem or something? ;-)
Thanks for this screencast.
Is there an easy way to access something like previous_release_path in the case that you want to copy something from the last release to the new release?
I've just been checking out ruby on rails. Looks nice but I think I would need this sort of thing for an efficient dev/testing framework, as I would not want to go live until I was confident in what was happening.
Thanks for the apache script.
Excellent screencast! I was using an "after deploy" task to regen restart.txt and couldn't figure out how to get rid of the "missing pid" error. Moving it into the :deploy namespace was just what I needed.
Capistrano also has built-in support for git submodules, which won't get checked out by default. This is handy if you are tracking plugins or rails as submodules. Turn it on with this setting:
set :git_enable_submodules, 1
Seems a little complicated first, but when reading it carefully, it works...
Awesome railscast Ryan. Thanks. Used it right away on a project.
I didn't have beta invitations previously in the project, but wanted to switch to them. I ran into a problem with updating the contents of old users (since they didn't have an invitation id and since the validation still required it).
For those of you having a similar problem my solution was to add an unless statement Proc on the validation.
http://pastie.org/300700
<code>
validates_presence_of :invitation_id, :message => 'is required', :unless => Proc.new { |user| user.state == 'active' }
validates_uniqueness_of :invitation_id, :unless => Proc.new { |user| user.state == 'active' }
</code>
wasn't using rails 2.1.2! Updated rails and installed nifty_generators and now its working smooth.
Thanks!
I'm trying to expose a form in an admin interface with all fields and in the normal user interface with just a subset. To stay as dry as possible, I'd like to include (in the C sense) a file with the common fields defined. Rendering a partial doesn't cut it because:
1. Inside the partial, it blows up on every f.field reference because it doesn't know what f is.
2. I'd like to come back to the original form declaration to pick up the submit button declaration specific to the use.
Does anyone know how/if I can do this?
Hey, I have a great suggestion, what about mention on every episode that it is obsolete (if it is)? So, there won't be anymore stupid guys like me, trying to do like you describe in the episode and finding out that your code doesn't work with latest stable version. I can help you starting ranging the episodes: this one is obsolete!
Hi Ryan, I noticed that you have switched to new shortcuts key tools from this episode. Can you tell me what it is?
Here's how to enable wildcard (*) or star searching with Thinking Sphinx: http://www.artofmission.com/articles/2008/10/24/wildcard-searches-with-thinking-sphinx
@chris: Good point, I can certainly see that -some- business logic can be applied in the model. Given your example however, wouldn't this be applied in the controller or view? (Unless of course it's a query limitation)
@pjam : The way build works is that it stores the child objects in an array in memory and it links that array to the parent object. When the parent object is saved, it saves all of the child objects at the same time with the proper parent id in place. So, for each of the Tasks, they are stored in an array and after the Project is saved, Rails recognizes the associated array of child objects and saves them as well, tacking on the Project's id in the project_id column (all auto-magically!); gotta love Rails!
Figured out my own question, see this link: http://www.railsonwave.com/railsonwave/2007/8/13/use-helper-functions-in-you-email-view
What is the solution for a has_many :through association between models? I have Movie has_many Actors through Roles - do I build on the join-table Roles?
Someone enlighten me please, as I get stringify_keys error when i try to adapt this to the join-table model attributes!
Great work Ryan, have learnt so much from your work!
How would you use your app helpers in a mailer body. Assuming I had a signup email, and I wanted to use a helper from my users_helper.rb inside my signup.html.erb. The docs say I can use action pack helpers but make no reference to using application helpers.
Thanks for the great podcast!
This could not be more useful! I'm looking at writing some code that handle delicate data at the moment.
Your screen casts are incredibly useful, keep up the fantastic work!
Russ
lolcatz, that didn't work for me