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” %>

<% @project.issues.each do |c| %> <% end %>
Outstanding Tasks for <%= @project.pj_name %>
<%= link_to c.title, :action => "show", :id => c.id -%> <%= link_to 'edit', :action => 'edit', :id => c.id %> | <%= link_to 'delete', :action => 'delete', :id => c.id %>
<%= link_to 'show projects', :action => 'list' %> | <%= link_to 'post new task', :action => 'new' %>

Thanks

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

<%= 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:

And that’s what Restful Routing does for you automatically.

On Jan 10, 2008 9:19 AM, Jeremy Weiskotten
[email protected]
wrote:

You might also want to defend against GET requests to the delete
resource, just in case. (Google for why it’s a good idea.)

Posted via http://www.ruby-forum.com/.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

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.

On Jan 10, 2008 9:30 AM, Kevin R. [email protected]
wrote:

project id has been passed back.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

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

I’d handle the error condition of an invalid issue ID as a 500 error (or
a 404 might be better) because you’re trying to access an invalid
resource.

You might also want to defend against GET requests to the delete
resource, just in case. (Google for why it’s a good idea.)

Kevin R. 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