【问题标题】:Rails routes scope - No routes matches [patch]Rails 路线范围 - 没有路线匹配 [补丁]
【发布时间】:2016-09-22 01:16:19
【问题描述】:

在将管理范围添加到我的路由后,我开始收到此错误: 没有路线匹配 [PATCH] "/projects/5"
提交更新或新时会发生这种情况
routes.rb

  resources :projects, only: [:show]  
  scope '/admin' do
    resources :projects, only: [:index, :destroy, :edit, :update, :new]
    resources :users
  end
  root 'welcome#index'
  get 'about' => 'welcome#about'

projects_controller.rb

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, only: [:index, :edit, :update, :destroy]

  # GET /projects
  # GET /projects.json
  def index
    user_signed_in?
    @projects = Project.all
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
    @project = Project.find(params[:id])
    @attachments = @project.attachments.all    
  end

  # GET /projects/new
  def new
    @project = Project.new
  end

  # GET /projects/1/edit
  def edit
    @project = Project.find(params[:id])
    @attachments = @project.attachments.all
  end

  # POST /projects
  # POST /projects.json
  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save

          if params[:images]
            # The magic is here ;)
            params[:images].each { |image|
              @project.attachments.create(image: image)
            }
          end        

        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /projects/1
  # PATCH/PUT /projects/1.json
  def update
    respond_to do |format|
      if @project.update(project_params)

          if params[:images]
            # The magic is here ;)
            params[:images].each { |image|
              @project.attachments.create(image: image)
            }
          end         


        format.html { redirect_to @project, notice: 'Project was successfully updated.' }
        format.json { render :show, status: :ok, location: @project }
      else
        format.html { render :edit }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project.destroy
    respond_to do |format|
      format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_project
      @project = Project.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_params
      params.require(:project).permit(:title, :description, :active, :position, category_ids:[])
    end
end

views/projects/edit.html.erb

<%= form_for @project, :html => { :multipart => true, :class => 'form-horizontal' } do |f| %>
                  <% if @project.errors.any? %>
                    <div id="error_explanation">
                      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

                      <ul>
                      <% @project.errors.full_messages.each do |message| %>
                        <li><%= message %></li>
                      <% end %>
                      </ul>
                    </div>
                  <% end %>

                  <div class="form-group">
                    <h4><%= f.label :title %></h4>
                    <%= f.text_field :title, class:"form-control" %>
                  </div>

                  <div class="form-group">
                    <h4><%= f.label :description %></h4>
                    <%= f.text_area :description, class:"form-control" %>
                  </div>

                  <div class="form-group">
                    <h4><%= f.label :active %> <%= f.check_box :active, class:"" %> </h4>
                  </div>  
                  <div class="form-group">
                    <h4><%= f.label :position %></h4>
                    <%= f.text_area :position, class:"form-control" %>
                  </div>
                  <div class="form-group">
                    <h4><%= f.label "Categories" %></h4><br>
                    <%= f.collection_check_boxes :category_ids, Category.all, :id, :name do |b| %>
                      <div class="collection-check-box">
                        <%= b.check_box %>
                        <%= b.label %>
                      </div>     
                    <% end %>
                  </div>  
                  <div class="form-group">
                    <h4><strong><p>Project Images</p></strong></h4>
                   <%= file_field_tag "images[]", type: :file, multiple: true, class: "form-control" %>
                  </div>   


                  <div class="actions" style="margin-left:-15px">

                    <%= f.submit class:"btn btn-primary" %>
                  </div>
                  <br></br>
                <% end %>

我在以下帖子中尝试了一些解决方案但没有成功:
Updating record rails 4 No route matches [PATCH] "/admin/usersupdate"
how to fix "No route matches [PATCH]"
Routing Error No route matches [PATCH] "/locations"

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    您只在路线中定义了显示操作,更改此行:

    resources :projects, only: [:show]  
    

    收件人:

    resources :projects, only: [:show, :update]
    

    或者

    resources :projects  
    

    【讨论】:

    • 这行得通,但是在管理员范围内更新和创建不是更好吗?
    • 视情况而定,如果您想在管理范围内更新或创建项目,则添加它。
    • 但是在收到错误时我在管理范围内进行了更新,然后在管理范围之外我没有收到任何错误。有意义吗?
    猜你喜欢
    • 2012-10-11
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    相关资源
    最近更新 更多