Letting the users edit cases

Creating and deleting content is not enough. Let's give our users the possibility to modify it themselves. In this section we will implement the edit functionality.

Adding an Edit button

Go to app/views/cases/index.html.erb and below the <%=link_to("Delete", case_path(c), method: :delete) %> add the line:

<%=link_to("Edit", edit_case_path(c)) %>

Having changed the view, it's time to do this in the controller.
Go to app/controllers/cases_controller.rb and add the lines:

def edit  
    @case = Case.find(params[:id])
end

What will the user see when they decide to edit the content? Let's make a view for that.
Go to app/views/cases and create a new file called edit.html.erb. Type the following lines into the newly created edit.html.erb file:

<h1> Edit Case</h1>
<%= form_for @case do |form| %>
<%= form.text_field :image_link %>
<%= form.submit %>
<% end %>

Add the update route in your cases controller:

def update  
    @case = Case.find(params[:id])
    if @case.update_attributes(case_params)
        redirect_to cases_path
    else
        render 'edit'
    end
end

Adding the logout link

Our users love us, they like creating, deleting and updating cases. While we'd like them to stay permanently on the website, they may want to be able to quit the app.

So let's give them a log out link.
This is easy – put the following code in your index.html.erb, below the <%=link_to("Create a new case", new_case_path) %> line:

 <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>

Devise knows how to handle the rest, so our work is done.

Check it out!

results matching ""

    No results matching ""