0

我遵循 Railscast 关于创建可排序列表的一集,并成功创建了一个可在内部更新其模型的可排序列表——在我的应用程序中,项目有很多步骤(这些步骤嵌套在项目中),我创建了一个包含可排序步骤的表当我打开 project_steps 路径时可以访问。

我现在要做的是从 edit_project_steps 路径(步骤有很多图像)中更新外部模型(图像)。我不确定如何将 Railscast 中所做的事情扩展到更新外部模型;现在,当我尝试在 edit_project_steps 路径中对图像进行排序时,出现错误“ ActionController::RoutingError (No route matches [POST] "/projects/1/steps/2/edit")

有人能指出我正确的方向吗?

这是我到目前为止所拥有的:

路线.rb

  resources :projects do
    resources :steps do
        collection {post :sort}
      end
      match "steps/:id" => "steps#number", :as => :number
  end

  resources :images do
     collection {post :sort}
   end

图像.rb

class ImagesController < ApplicationController
   # Sorts images
  def sort
    render nothing: true
  end

end

步骤/edit.html.erb

<div class="imageGallery span8">
    <p style="margin: 5px 0px;"><b>Step Images</b> - Click and drag to rearrange</p>
      <div class = "wrapper">
        <div class="scrolls">
          <div class="imageDiv" id="stepImages" data-update-url="<%= sort_images_url %>">
            <div class="images">
              <%= render :partial => 'images/image', :collection => @step.images.all %>
            </div>
            <div class="clear"></div>
          </div>
        </div>
      </div>

<% #drag images %>
<script>
$(".imageDiv .images").sortable({
  cursor: "move",
  axis: 'x', 
  update: function(){
    $.post($(this).data('update-url'), $(this).sortable('serialize'));
        }
  }).disableSelection();
</script>

耙路线

            sort_project_steps POST       /projects/:project_id/steps/sort(.:format)     steps#sort
                 project_steps GET        /projects/:project_id/steps(.:format)          steps#index
                               POST       /projects/:project_id/steps(.:format)          steps#create
              new_project_step GET        /projects/:project_id/steps/new(.:format)      steps#new
             edit_project_step GET        /projects/:project_id/steps/:id/edit(.:format) steps#edit
                  project_step GET        /projects/:project_id/steps/:id(.:format)      steps#show
                               PUT        /projects/:project_id/steps/:id(.:format)      steps#update
                               DELETE     /projects/:project_id/steps/:id(.:format)      steps#destroy
                project_number            /projects/:project_id/steps/:id(.:format)      steps#number
                      projects GET        /projects(.:format)                            projects#index
                               POST       /projects(.:format)                            projects#create
                   new_project GET        /projects/new(.:format)                        projects#new
                  edit_project GET        /projects/:id/edit(.:format)                   projects#edit
                       project GET        /projects/:id(.:format)                        projects#show
                               PUT        /projects/:id(.:format)                        projects#update
                               DELETE     /projects/:id(.:format)                        projects#destroy
                   sort_images POST       /images/sort(.:format)                         images#sort
                        images GET        /images(.:format)                              images#index
                               POST       /images(.:format)                              images#create
                     new_image GET        /images/new(.:format)                          images#new
                    edit_image GET        /images/:id/edit(.:format)                     images#edit
                         image GET        /images/:id(.:format)                          images#show
                               PUT        /images/:id(.:format)                          images#update
                               DELETE     /images/:id(.:format)                          images#destroy
                          root            /                                              projects#index

这是我试图在页面上排序的图像:

我试图在 edit_project_step 路径中排序的图像

4

1 回答 1

0

原来是一系列问题......这是我为感兴趣的人提供的最终代码:

图像.rb

  def sort
    params[:image].each_with_index do |id, index|
      Image.update_all({position: index+1}, {id: id})
    end
    render nothing: true
  end

步骤/edit.html.erb

<div class="imageGallery span8">
    <p style="margin: 5px 0px;"><b>Step Images</b> - Click and drag to rearrange</p>
    <div class = "wrapper">
      <div class="scrolls">
        <div id="images" data-update-url="<%= sort_images_url %>">
          <% @step.images.order("position").each do |image| %>
            <%=content_tag_for(:div, image) do %>
              <%= render :partial => 'images/image', :locals => {:image=> image} %>
            <% end %>
          <% end %>
        </div>
      </div>
    </div>

<% #drag images %>
<script>
$("#images").sortable({
  cursor: "move",
  axis: 'x', 
  update: function(){
    $.post($(this).data('update-url'), $(this).sortable('serialize'));
  }
  }).disableSelection();
</script>
于 2013-02-09T07:14:59.857 回答