PDA

View Full Version : Ruby on Rails database problem




white89gt
May 8, 2006, 10:32 PM
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.



Rower_CPU
May 8, 2006, 11:13 PM
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. :)

demallien
May 9, 2006, 03:55 AM
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.

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?

white89gt
May 9, 2006, 09:45 AM
I figured it out. Here's what I was doing:

<%= link_to 'Delete', :action => 'destroy', :id => @workorder %>

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

<%= link_to 'Delete', { :action => 'destroy', :id => @workorder }, :confirm => "Are you sure", :post => true %>

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.

Rower_CPU
May 9, 2006, 09:53 AM
Glad to hear you sorted it out. :)

demallien
May 10, 2006, 06:45 AM
for some reason it won't work unless you add a confirm and a post like this



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?

white89gt
May 10, 2006, 02:17 PM
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?

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.