Rails路由范围 - 没有路由匹配[patch]

时间:2016-09-22 01:16:20

标签: ruby-on-rails routes

将管理范围添加到我的路由后,我开始收到此错误: 没有路线匹配[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

视图/项目/ 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"

1 个答案:

答案 0 :(得分:0)

您只在路线中定义了show动作,请更改此行:

resources :projects, only: [:show]  

要:

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

或者

resources :projects