【问题标题】:Problem when saving a nested form in Rails在 Rails 中保存嵌套表单时出现问题
【发布时间】:2018-09-06 19:05:08
【问题描述】:

在我的应用程序中,我有一个名为配置文件的视图。在此视图中,用户可以更新您的相册和这些相册的照片。

一个用户有很多相册。一个相册有很多照片。

我正在我的个人资料视图中创建一个表单来保存相册。像这样:

#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 %>

当我按下按钮保存时:

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

我收到这条消息:

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

有人可以帮助我吗?

谢谢

【问题讨论】:

  • 为什么需要这样的 URL:'/profile/edit/profile_albums',而不是 '/profile/:id/edit/profile_albums'

标签: ruby-on-rails ruby


【解决方案1】:

我认为您需要在form_for 中指定url

首先使用rake routes,检查该表单应该发送到的路由,然后将您的form for修改为:

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

只需将edit_profile_albums 替换为运行rake routes 时将获得的路径

【讨论】:

    【解决方案2】:

    正如错误提示的那样,您需要添加正确的路径才能使表单正常工作。在这种情况下,它可能是resources :albums in routes.rb。我建议阅读本指南(易于阅读):https://guides.rubyonrails.org/routing.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多