concept edit in category rails

appears as: edit, edit, n edit
Rails 4 in Action: Revised Edition of Rails 3 in Action

This is an excerpt from Manning's book Rails 4 in Action: Revised Edition of Rails 3 in Action.

For this action, you’re working with a preexisting object rather than a new object, which you used in the new action. This preexisting object is found by the edit action in PurchasesController, as shown here.

Listing 1.14. The edit action of PurchasesController
def edit end

Oops: it’s not here! The code to find the @purchase object is identical to what you saw earlier in the show action: it’s set in before_action, which runs before the show, edit, update, and destroy actions.

Back in the view for a moment, at the bottom of it you can see two uses of link_to. The first creates a “Show” link, linking to the @purchase object, which is set up in the edit action of your controller. Clicking this link would take you to purchase_path(@purchase) or /purchases/:id. Rails will figure out where the link needs to go according to the class of the object given. Using this syntax, it will attempt to call the purchase_path method because the object has a class of Purchase, and it will pass the object along to that call, generating the URL.

With the first two parts of CRUD (creating and reading) done, you’re ready for the third part: updating. Updating is similar to creating and reading in that it has two actions for each part (creation has new and create; reading has index and show). The two actions for updating are edit and update.

Let’s begin by writing a feature and creating the edit action.

As with the form used for creating new projects, you want a form that allows users to edit the information of a project that already exists. You first put an “Edit Project” link on the show page that takes users to the edit action, where they can edit the project. Put the code from the following listing into spec/features/editing_projects_spec.rb.

When you run the spec again, you’re told that the edit view is missing:

1) Users can edit existing projects with valid attributes
   Failure/Error: click_link "Edit Project"
   ActionView::MissingTemplate:
     Missing template projects/edit, application/edit with
     {
      :locale => [:en],
      :formats => [:html],
      :variants => [],
      :handlers => [:erb, :builder, :raw, :ruby, :coffee, :jbuilder]
     }.

     Searched in:
     * ".../ticketee/app/views"

It looks like you need to create this template. The edit action’s form is similar to the form in the new action—if only there were a way to extract out just the form into its own template. Well, in Rails, there is! You can extract out the form from app/views/projects/new.html.erb into what’s called a partial. You saw partials briefly in chapter 1.

Rails 3 in Action

This is an excerpt from Manning's book Rails 3 in Action.

For this action, you’re working with a pre-existing object rather than a new object, which you used in new. This pre-existing object is found by the edit action in PurchasesController, shown in the next listing.

You want to ensure that all visits to the new, create, edit, update, and destroy actions are done by admins and are inaccessible to other users. Open spec/controllers/projects_controller_spec.rb, and add a let inside the describe so the top of the spec looks like the following listing.

Listing 8.9. Update tests for spec/controllers/tickets_controller_spec.rb
it "cannot edit a ticket without permission" do get :edit, { :project_id => project.id, :id => ticket.id } cannot_update_tickets! end it "cannot update a ticket without permission" do put :update, { :project_id => project.id, :id => ticket.id, :ticket => {} } cannot_update_tickets! end