1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Ruby on Rails database problem

Discussion in 'Mac Programming' started by white89gt, May 8, 2006.

  1. macrumors regular

    Here's the problem. I build a new rails app, set up the access to the database , and scaffold the controller. Everything works fine. Then I start rebuilding the list.rhtml file. Once I'm done I can add, view, and edit any entries that I make but when I try to destroy them nothing happens. In short I can't figure out how to delete rows from the db. I'm using the exact same syntax as the original list.rhtml file but nothing happens. I've made sure that the user has the correct rights several times. What am I missing? I can post code if I need to.
  2. Moderator emeritus


    Once you start customizing the scaffolding you often also need to go into the controller for the action and explicitly define your methods. Are you getting any errors (you're in development mode, I hope) or does the log have any info?

    Please post code, too. :)
  3. macrumors regular

    Yup, I think you're going to need to post the code (both the .rhtml and the controller .rb). Whilst I can't claim to be a Rails expert, I have done a simple catalogue app that successfully deletes rows from the table.

    As the first post says, I'm guessing that the problem is in the controller. Does your .rhtml send the correct message to the controller object? Does the controller object have the receiver defined?
  4. macrumors regular

    I figured it out. Here's what I was doing:

    for some reason it won't work unless you add a confirm and a post like this

    I can't figure out how to diable smilies...the :p is actually a : followed by the word post.
    I was using InstantRails a few days ago and then I downloaded the full blown deal and I guess there are some subtle differences.
  5. Moderator emeritus


    Glad to hear you sorted it out. :)
  6. macrumors regular


    Hmmm, seems a little odd.

    I know you have already got something to work, but the solution is not exactly obvious... So I'm going to suggest a couple of things...

    Firstly, it's a bit odd that you have an instance variable, @workorder, in your rhtml code. Have you actually declared a class on the page???? That would be rather wierd, architecturally speaking. Classes are normally declared in a controller, or in a model, not in a view...

    Secondly, did you try the original form of the command, except putting the braces around the hash, giving:
    <%= link_to 'Delete', { :action => 'destroy', :id => @workorder }%>

    My guess is that putting the braces around the hash somehow allows Rails to correctly deal with the '@', which it was unable to do without the braces. Is @workorder a vector maybe?
  7. macrumors regular

    The reason for the instance variable is because I moved the delete function from the list.rhtml to the show.rhtml file. When it is on the list page, the id is generated per line when the code is parsed. When it is on the show page, the only link to that particular instance is stored in the instance variable from the controller file. I know it isn't necessarily the best way to do it, but for the program that I'm building the delete function won't be needed in the end since I'm going to archive all closed workorders. I just kept it around so I could clean things up without having to rake the database.

    And no, I didn't think to try <%= { link_to 'delete', :action => 'destroy', :id => @workorder }%> . I may give it a try, but I actually kind of like having to confirm the delete. That way I don't goof up.

Share This Page