Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine Show and Edit actions in Ruby on Rails?

I was just wondering if anybody has ever built a Rails application without any Show actions.

In my application, if a user picks, say a Project, from the list he will probably want to not just show it but also edit it.

So is it good practice to not implement any show actions and instead link all records to edit forms?

The only caveat I see is that a user might accidentally edit a project. But this could be prevented by setting a default attribute disabled on all form fields using jQuery. That way, a user will have to click a button "Edit" in order to unlock the form fields for editing.

Does that make sense or am I completely nuts?

Who knows, maybe this causes conflicts with Rails' RESTful architecture?

like image 660
Tintin81 Avatar asked Sep 29 '12 12:09

Tintin81


2 Answers

First thought:

In my opinion this could confuse your users because: The usual url for a show action looks like this:

products/:id

but the edit looks like:

products/:id/edit

So if one of your users wants just to take a look (by clicking show) into a project it might get confused by the displayed url. You could fix this by using named routes but in my opinion this will definitely conflicts with REST.

Second thought:

Before implementing jQuery logic just for enabling an edit action on an object you should use the built in mechanisms with a show- and an edit page/method, because the maintainability and readability will stay easier and clean with using default behavior.

Please correct me if I am wrong :)

like image 168
Flo Avatar answered Oct 28 '22 08:10

Flo


I think having a show action is usually pointless if you're building a simple, namespaced CMS backend. Django-admin omits it, and a path like /admin/blogs/post/241 goes directly to the edit form. I think this way makes sense, using the front-facing website as the show action.

It does go against Rails' default routing conventions, but not so much that you risk breaking something with an upgrade.

You can override the routes like so:

resources :posts, except: [:show] do
  get ':id' => 'posts#edit', on: :member
end

However, I would say just leave the routes as the resources method defines them by default, and throw the except option in there to avoid missing template errors if someone tries to go to /posts/241. Then you can just always link to the edit page.

EDIT: I will say that when building a CMS, I do like to use the show action, but not to just list the attributes. I like to use it as a sort of summary for that record, displaying some of the key information, when it was created/last updated, a list of its versions and who edited it (if you're into that sort of thing), as well as links to Edit, Destroy, or visit the record on the front-end website.

like image 41
bricker Avatar answered Oct 28 '22 07:10

bricker