Fernando Maymone
Fernando Maymone

Reputation: 355

Problem when saving a nested form in Rails

In my app I have a view called profiles. On this view the user can update your albums and the photos of those albums.

A User has many Albums. And a Album has many Photos.

Im creating a form inside my profile view to save a album. Like this:

#routes.rb
  get '/profile/edit/profile_albums', to: 'profiles#edit_profile_albums'


#/view/profile/edit/profile_albums.html.erb


<%= form_for album do |f| %>
    <div class="dialog dialog-centered" id=<%= album.id %> style="display: block;">
      <div class="dialog-container dialog-wide">
        <div class="dialog-content panel">
          <div class="form-group bottom-7">
            <label for="">Album Name</label>
            <input type="text" id="" value=<%= album.name %> placeholder="Add Album Name">
          </div>
          <div class="edit-photo-album bottom-5">
          <% album.photos.each do |photo| %>
           <span class="edit-photo">
              <img src="<%= asset_path ix_refile_image_url(photo, :media, fit: 'fill', bg: '0fff') %>" class="photo-preview">
                <a href="" title="Remove" class="remove-item">Remove</a>
           </span>
          <% end %>
                <span class="btn btn-default btn-file">
                        <i ></i> Upload Photos 
                        <%= f.attachment_field :photos_media, multiple: true, direct: true, presigned: true %>
                    </span>

          </div><!-- end photo album -->
          <p>
          <div class="actions">
            <%= f.submit 'Save Album', class: 'btn btn-lg btn-solid-red btn-margin-right' %>
          </div>
          <button type="button" class="btn btn-lg btn-outline-green dialog-close">Cancel</button>
          </p>
        </div><!-- end dialog content -->
      </div><!-- end dialog container -->
    </div>
<% end %>

When I pressing the button save:

 <%= f.submit 'Save Album', class: 'btn btn-lg btn-solid-red btn-margin-right' %>

I get this message:

Routing Error
No route matches [PATCH] "/profile/edit/albums"

Someone can help me?

Thanks

Upvotes: 0

Views: 60

Answers (2)

Hesham Youssef
Hesham Youssef

Reputation: 198

I think you need to specify the url in the form_for.

First using rake routes, check for the route where this form should be sent to, and then modify your form for to be something like:

<%= form_for album, url: edit_profile_albums_path do |f| %>

Just replace edit_profile_albums with the path you will get when you run rake routes

Upvotes: 3

Matthieu Libeer
Matthieu Libeer

Reputation: 2365

As the error suggests, you need to add the proper route for the form to work. In this case, it's probably resources :albums in routes.rb. I recommend going through this guide (easy to read): https://guides.rubyonrails.org/routing.html

Upvotes: 1

Related Questions