7.5 Update a destination (use of form_for)


#1

I'm having a lot of difficulties with this section 7: Update a destination of Associations I.
1st issue: I cannot pass part 5 where I have to create a form.

HERE ARE THE INSTRUCTIONS:
In app/views/destinations/edit.html.erb inside <div class="container">...</div>, 
use form_for to create a form with the fields of the @destination object.

MY SCRIPT BELOW:

<div class="header">
  <div class="container">
    <img src="http://s3.amazonaws.com/codecademy-content/courses/learn-rails/img/logo-1tm.svg" width="80">
    <h1>BokenjiKan</h1>
    
    <%= form_for(@destination) do |f| %>  
  <div class="field"> 
    <%= f.label :message %><br> 
    <%= f.text_area :content %> 
  </div> 
  <div class="actions"> 
    <%= f.submit "Create" %> 
  </div> 
<% end %>

  </div>
</div>

<div class="destination">
  <div class="container">
    <%= image_tag @destination.image %>
    

  </div>
</div>

2nd Issue: I cannot understand what was happening for the first four parts as well. I would appreciate it VERY MUCH if someone could explain to me what's happening at each script for each part. Thank you!

PART 1: First in the routes file (config/routes.rb), add these routes:

get '/destinations/:id/edit' => 'destinations#edit', as: :edit_destination 
patch '/destinations/:id' => 'destinations#update'

PART 2: Then in the Destinations controller below the show action, add an edit action:

def edit 
  @destination = Destination.find(params[:id]) 
end

PART 3: Below the edit action, add a private method named destination_params:

private 
  def destination_params 
    params.require(:destination).permit(:name, :description) 
  end

PART 4:
Between the edit action and the private method, add an update action:

def update 
  @destination = Destination.find(params[:id]) 
  if @destination.update_attributes(destination_params) 
    redirect_to(:action => 'show', :id => @destination.id) 
  else 
    render 'edit' 
  end 
end

#2

Update: I've figured out the correct script. However, I still have a hard time understanding the first four parts. Would appreciate explanations!

Not sure if I'm explaining it correctly but I changed the f.label and f.text_area to show the same symbols (:name and :description respectively), I also edited the actions class to submit as "Update" (maybe that affects as well). The rest of the code is correct.

  <%= f.label :Name %>  <br>
  <%= f.text_area :name %>
</div> 
<div class="field">
  <%= f.label :Description %> <br>
  <%= f.text_area :description %>
</div>
<div class="actions">
  <%= f.submit "Update" %>

#3

Hi @cherye,

I'll see if I can explain those four code segments for you:

  1. Going line-by-line:
    1. If there's a request for a page located at /destinations/[ID number]/edit, send it to the edit action in the Destinations controller, and name it edit_destination for the path and URL helpers (easy ways to link to that page in Rails).
    2. If there's a request to update a destination, send it to the update action under the Destinations controller.
  2. In the edit action, create a variable called @destination and set it equal to the destination requested by ID number.
  3. Create a private method (so it can't be accessed out of that file, if I remember how private methods work correctly) called destination_params, and any creation or change to an existing article isn't allowed without (or with any more than) the destination, name, and description fields for security reasons.
  4. Inside the update method, create a variable called @destination and set it equal to the destination requested by ID number. Then check if it has all of the necessary parameters (as specified in our private destination_params method), and if so then show the updated destination. Otherwise, continue to show the edit page so the person editing the destination can fix their mistake.

I hope this helps! Feel free to ask any more questions you may have :slight_smile:

Ruby code isn't hard to read. If you're having a hard time keeping up with this course or understanding the code, I'd suggest you take the Ruby course first, then come back to this.


Please edit your second post to show what you changed, rather than the final solution, in order to keep with the forum guidelines more fully. Thanks!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.