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.
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
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:
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).
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).
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.