destroy question (best practices?)

Hey everyone, I'm pretty new (really new) to RoR, actually rails brought
the coder back out in me.

I've been learning how Rails works by creating a Project/Issue's
application, which I will expand as my knowledge grows. Right now I
have 2 Model's under one controller. Issue and Category laid out with
Issue belongs to Category and Category has many issues. My basic
functionality works perfect as I can created edit both models the way I
wish.

The initial screen is my list or projects (list.rhtml), once you click
on the project you get a list of issues that are part of that
project(show_issues.rhtml) which is passed and expects a project_id.
ie: localhost:3000/isssues/show_issues/3 for project id #3)

I would like some idea's our best practices, on how I can delete an
issue and be redirected back to the show_issues/#project_id to list of
issues. Right now my delete definition in my controller is just
redirected back to the project list.

Here's the code from my show_issues

<%= stylesheet_link_tag "style" %>
  <table class="Show">
    <th class="ProjectList" colspan="2">
      Outstanding Tasks for <%= @project.pj_name %>
    </th>
<% @project.issues.each do |c| %>
    <tr>
      <td class="ShowRub">
        <%= link_to c.title, :action => "show", :id => c.id -%>
      </td>
      <td class="ShowRight">
        <%= link_to 'edit', :action => 'edit', :id => c.id %>
         >
         <%= link_to 'delete', :action => 'delete', :id => c.id %>
    </tr>
  <% end %>
    <tr>
      <td class="ShowNull" colspan="2">
        <%= link_to 'show projects', :action => 'list' %> |
        <%= link_to 'post new task', :action => 'new' %>
      </td>
    </tr>
  </table>

Thanks

Hey everyone, I'm pretty new (really new) to RoR, actually rails brought
the coder back out in me.

I've been learning how Rails works by creating a Project/Issue's
application, which I will expand as my knowledge grows. Right now I
have 2 Model's under one controller. Issue and Category laid out with
Issue belongs to Category and Category has many issues. My basic
functionality works perfect as I can created edit both models the way I
wish.

The initial screen is my list or projects (list.rhtml), once you click
on the project you get a list of issues that are part of that
project(show_issues.rhtml) which is passed and expects a project_id.
ie: localhost:3000/isssues/show_issues/3 for project id #3)

I would like some idea's our best practices, on how I can delete an
issue and be redirected back to the show_issues/#project_id to list of
issues. Right now my delete definition in my controller is just
redirected back to the project list.

In your controller:

def delete
   issue = Issue.find_by_id(params[:id])
   if issue.nil?
    #toss an error maybe?
   else
     issue.destroy
     redirect_to :action => :show_issues, :id => issue
   end
end

Here's the code from my show_issues

<%= stylesheet_link_tag "style" %>
<table class="Show">
   <th class="ProjectList" colspan="2">
     Outstanding Tasks for <%= @project.pj_name %>
   </th>
<% @project.issues.each do |c| %>
   <tr>
     <td class="ShowRub">
       <%= link_to c.title, :action => "show", :id => c.id -%>

It doesn't really matter, but it's simpler to just say :id => c

That way you could put this in your Issue model:

   def to_param
     "#{id}-#{title.gsub(/[^a-z0-9]+/i, '-').downcase}"
   rescue
     id
   end

and you're url for that issue would be pretty.

Instead of:

def delete
issue = Issue.find_by_id(params[:id])
if issue.nil?
#toss an error maybe?
else
issue.destroy
redirect_to :action => :show_issues, :id => issue
end
end

I would do:

def destroy
Issue.find(params[:id]).destroy
rescue ActiveRecord::RecordNotFound
flash[:notice] = “The issue you were looking for could not be found.”
ensure
redirect_to issues_path
end

Try to keep yourself from repeating words in your code, such as the word “issue”, you have /issues/show_issues which may just be an index page (one that shows all the issues, right?). This URL could be shortened down to just /issues/1 using Restful Routing: http://frozenplague.net/2008/01/06/restful-routing-an-overview/

Thanks for the info on some other things to look at, I'll be reading up
on most of it as time goes on. The next chapter in the book I'm
somewhat following goes in to the routing which sounds interesting.

But..

Here's the big issue. show_issues is expecting the project_id from the
issue table or also as you know the id from the projects table. A
simple redirect_to show_issues will give me an nil error since no
project id has been passed back.

show_issues will then display back all issues thats tied to that
project.

Maybe I designed this wrong (wouldn't be surprised as it's my first
real shot at it).

I’m going to sound like a broken record here, but restful routing fixes that too!

You can do redirect_to project_issues_path(@project) and it will show you all those issues from that project.

Kevin Rie wrote:

Thanks for the info on some other things to look at, I'll be reading up
on most of it as time goes on. The next chapter in the book I'm
somewhat following goes in to the routing which sounds interesting.

But..

Here's the big issue. show_issues is expecting the project_id from the
issue table or also as you know the id from the projects table. A
simple redirect_to show_issues will give me an nil error since no
project id has been passed back.

show_issues will then display back all issues thats tied to that
project.

Maybe I designed this wrong (wouldn't be surprised as it's my first
real shot at it).

def delete
  issue = Issue.find params[:id]
  issue.destroy
  redirect_to :action => :show_issues, :id => issue.project_id
end

That should do what you want.

However, you should consider a more RESTful design like Ryan suggested.

Jeremy Weiskotten wrote:

However, you should consider a more RESTful design like Ryan suggested.

Thanks guys, that worked, I thought I tried that before but I guess
not. And yes I'll check out restful routing, I just want to get the
basics down fairly well before I bring on more on