Paperclip with albums
I'm struggling with paperclip and adding the开发者_如何学运维 attached images to albums. Should I be writing an album_id to the images row in the database? Right now, I'm trying to use an album model, which belongs_to :user and has_many :photos. The photo model belongs_to :album and has_attached_file. Is that the way to go about it?
I'm really new to Rails so i'm still getting hung up on things like...
<%= form_for @album, :html => { :mulitpart => true } do |f| %>
because I have no idea how @album is suppose to point to the right controller/action. Another thing is how the h*ll is photo suppose to know which album its apart of? Normally I would have said save the album_id in the photo row but I feel like I'm passing up paperclips functionality.
Rails still hasn't "clicked" for me =/ I think PHP ruined me...or the auto-magickness is too powerful for my mind.
There is no Magic in Rails . It all about how you understand the concepts . In order to understand the rails following things are essential
Basic Knowledge on Ruby
Agile web development will be kick start (Understand MVC Architecture)
Debugging Rails application and
script/console
Coming back to your above Paperclip application .
Here @album is an instance variable which holds the value of the form fields like the title ,image etc.
<% form_for(@album,:url => {:controller => "albums" , :action => "create" }) do |f| %>
correct syntax is this , which points to the albums controller create action , if you are familiar with the routes you can usenew_album_path
will also routes as same as above.h*ll is photo suppose to know which album its apart of? Answer is through specifying Associations in the Model between album and Photo and User. Based on your requirement. set the associations. Associations are the relationship between to two tables , Like in PHP you do foreign key and relate two tables . Best book to learn this is Pro Active Record
Here is a nice tutorial by Jimneath
Hope it helps !
精彩评论