SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please help a beginner

    Just strating out with both Ruby and rails, following the tutorial at http://developer.apple.com/tools/rubyonrails.html but adjusting it slightly for my own project.

    I have 2 tables, weddings and photos, a wedding can have multiple photos.
    I have added the following code at the end of my show.rhtml

    <%= start_form_tag :action => 'addPhoto', :id => @wedding %>
    <p>
    <%=@wedding.id%>
    Photo Title<%= text_field 'photo', 'photo_title', :size => 25 %>
    Photo Path<%= text_field 'photo', 'photo_path', :size => 25 %>
    Thumbnail path<%= text_field 'photo', 'thumbnail_path', :size => 25 %>
    </p>
    <%= submit_tag 'Add!' %>
    <%= end_form_tag %>


    Which displays fine, i have also added this to my controller.

    def addPhoto
    Wedding.find(params[:id]).photos.create(params[hoto])
    redirect_to :action => 'show', :id => params[:id]
    end

    When i try and add a photo i get the following error:

    Mysql::Error: #23000Cannot add or update a child row: a foreign key constraint fails (`ew/photos`, CONSTRAINT `photos_ibfk_1` FOREIGN KEY (`wedding_Id`) REFERENCES `weddings` (`id`)): INSERT INTO photos (`photo_title`, `thumbnail_path`, `wedding_Id`, `photo_path`) VALUES('gy', 'gi', 0, 'g')

    Can anyone tell me why the wedding id is not getting passed through? it should be 2 (thats what shows up on the URL) but the error says its 0.

    Many thanks.

    Penguin girl.

  2. #2
    SitePoint Enthusiast kyko's Avatar
    Join Date
    May 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There seems to be an issue with your model since this is an SQL Error so if you could post the code for both of your models i can see what is going on a bit better.
    One problem i see right now is the use of wedding_Id as opposed to wedding_id. When you use rails, it assumes that you do things according to the rails way of thinking unless you want to configure it yourself. The thought is convention over configuration. And this is why all the "magical" stuff happens behind the seen. But when you go off the convention (without configuring it) you lose the magic.
    Another non-railsish thing in your code is the use of addPhoto instead of add_photo. This isn't causing problems right now so it isn't a big deal but you may want to change your habit for programming on the rails framework.
    One last thing, you also may want to forget about using fk_constraints in your rails app (you may still want to us fk's however if there is another lang/framwork using the db or people are allowed to input data directly). Rails handles all that stuff in the model with :has_many, :belongs_to, :has_one, :has_and_belongs_to_many.
    Stop Global &nbsp;

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant! the upper case 'i' was the problem, thanks very much, and thank you for your additional suggestions, i will make the changes now.


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
  •