SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add photos to parent model

    Ok, this should be pretty simple, but i'm new to rails. I have a class person with normal people attributes (name, eye colour etc) and they have_many : photos. I'm using attachment_fu as my file uploader. Here's what I need, when I show the person, I'm going to also show their attributes + uploaded photos, AND have an upload field that a user can add another photo, then redirect back to showing that person. Only, I don't know how to do this in the show action of the person controller. Do I need a controller for my photo class, or can this all be achieved through the parent/child relationship of people to photos. Also, I'm using InPlaceEditing for changing the person attributes in the show action, so I don't have an edit or update page, just want it all done through the one show page. Can someone give me a brief breakdown of what I want, ie. routes, controller action, and the form code in the view etc? I'm totally stumped and this should be super simple.

  2. #2
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You already have a photo controller to upload, don't you?

    In the people/show template you include the upload form and this form sends to PhotosController::create which, once the upload is successfull directs back to people/show or renders photos/new if there was an error.

    You could have a look at how mephisto solves the problem. A very app with good testing as well: http://github.com/emk/mephisto/tree/
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  3. #3
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so I want to have photos listed as a resource in routes i guess then right?
    Also, I don't really have a photos/new, because the only place I want a person to add photos is in the show of the Person. Is this a problem?

    In my PersonController::show I added: @photo = Photo.new and in my show view I created form_for(@photo, :html => {:multipart=>true}) but, how do I get it to pass the person_id in the params as well, this form just passes the photo to PhotosController::create

    I'm interested in the same thing for delete as well then, how do I pass the person_id to PhotosController::destroy

  4. #4
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can add any text_field to the form you want. and as the photo has a person_id it should be text_field_tag @photo, erson_id
    BUT you need a PhotoController with a create and a destroy method, at least. The trick is to keep your controllers as simple as possible. anything complicated should go into the model or a helper cuz they are a lot easier to test.
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  5. #5
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick response, I actually figured this out, and used this in my controller:
    def create
    @photo = Photo.newuploaded_data => params[hoto][hoto])
    @person = Person.find(params[hoto][erson_id])
    @photo.person = @person

    if @photo.save
    flash[:notice] = "Photo successfully uploaded"
    redirect_to(@person)
    else
    flash[:notice] = "Something went wrong"
    redirect_to(@person)
    end
    end
    with this in my view:
    <% form_for @photo, :html => { :multipart => true } do |f| %>
    <%= f.file_field hoto %><br />
    <%= f.hidden_field erson_id, :value => @person.id %>
    <%= submit_tag 'upload photo'%>
    <% end %>

    Now, I'm still now sure how I can remove a photo AND redirect back to the Person, since the destroy is just a link to PhotosController::destroy, again I need to pass the Person.id somehow... but in a link?

  6. #6
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    photos_contoller#destroy is like this

    @photo.destroy
    redirect_to person_path(@photo.person_id)
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  7. #7
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya sorry, i realized after how incredibly stupid that question was


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •