How to visualize the Edit form on the Show page of another model in Rails

I want to display the Edit form in the Show view of the parent.

My model looks like this: There are many days in the Trip, in which there are many activities. Trip accepts nested attributes for days. Days accept nested attributes for actions.

When I am in the Show view for a trip, how do I make a partial Change form for an Action?

I know that I need to somehow indicate Partially Edit Form, which activity identifier I want to change, but I'm not sure how to transfer this information from the Show view to 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 partially 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 %> 
+7
source share
1 answer

This is what I finished and it works.

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

show.html.erb

 <div id="activity_form"> <h2>Activities</h2> </div> 
Note: remote => true, which tells the Rails controller that this will be an AJAX request, so edit.js.erb is for rendering
 <%= 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 is partially located in the "Type of activity" 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 in the Actions submission directory (../views/activities/edit.js.erb). Says to grab the DOM element with id "activity_form" and make a partial "form"

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

update.js.erb I enabled this javascript after clicking on the update in the "Edit" form to display the updated part of the list of operations. So I do not need to reload the page to see the update.

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

routes.rb This is how I set up routes. Only level 1 is in line with best practices.

 resources :trips do resources :days end resources :days do resources :activities end 
+5
source

All Articles