Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a "Edit" form partial in the "Show" page of another model in Rails

I want to display an "Edit" form in the "Show" view of a parent object.

My model looks like this: A Trip has many Days which has many Activities. Trip accepts nested attributes for Days. Days accepts nested Attributes for Activities.

When I am in the "Show" view for a Trip, how do I render an "Edit" form partial for an "Activity"?

I know I need to somehow specify to the Edit Form partial which Activity ID that I want to edit but I'm not sure how to pass along that information from the "Show" view of a "Trip".

    <% @trip.days.each do |day| %>
    <div id="daydiv_<%= day.id %>">
      <b><%= day.summary %></b>
        <%= content_tag_for :ol, day do %>
          <% day.activities.each do |activity| %>
                <li id="activity_<%= activity.id %>"><%= link_to activity.address, edit_activity_path(activity) %></li>
          <% end %>
      <% end %>
    </div>
    <% end %>

<div id="activity_form">
  <%= render :partial => "/activities/form", :activity => @activity  %>
</div> 

my /activities/form partial looks like this:

<%= form_for(@activity) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
like image 594
Hung Luu Avatar asked Nov 05 '12 20:11

Hung Luu


People also ask

How can you tell Rails to render without a layout *?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.

What is Local_assigns?

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not. Here you render a partial with some values, the headline and person will become accessible with predefined value.


1 Answers

This is what I ended up doing and it works.

In my show.html.erb for my "Trip".

show.html.erb

<div id="activity_form">
<h2>Activities</h2>
</div> 

link to "Edit' an Activity. Notice the :remote => true which tells the Rails controller that this will be an AJAX request so to render edit.js.erb

<%= link_to activity.location[0, 20], edit_day_activity_path(day, activity), :class=>"btn btn-info fixedwidthbtn", method: :get, :remote => true 

_form.html.erb This form partial is under the Activities View directory (../views/activities/_form.html.erb).

<%= form_for([@day, @activity], :remote => true) do |f| %>
<fieldset>
  <%= f.label :title, "Activity" %>
  <%= f.text_field :title, :rows => 1 %> 
</fieldset>
  <div class="actions">
    <%= f.submit %>
  </div>
</form>
   <%= link_to 'Delete', [@day, @activity], method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

edit.js.erb This is a file under the Activities view directory (../views/activities/edit.js.erb). Says to grab the DOM element with ID of "activity_form" and render the partial "form"

$("#activity_form").html("<%= escape_javascript(render(:partial => "form"))%>");

update.js.erb I included this javascript after hitting update on the Edit form to render an updated partial of the Activities List. So that I don't have to reload the page to see an update.

$("#activities_list").html("<%= escape_javascript( render(:partial => "/trips/activities") ) %>");

routes.rb This is how I nest my routes. Only doing it 1 level following best practices.

resources :trips do 
  resources :days 
end

resources :days do 
  resources :activities 
end
like image 82
Hung Luu Avatar answered Sep 21 '22 06:09

Hung Luu