2016-09-22 57 views
0

,我开始收到此错误将管理范围,我的路线后: 没有路由匹配[PATCH]“/项目/ 5”
这是提交更新或新的时发生的事情
的routes.rbRails的路线范围 - 无路线[补丁]匹配

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

我试过在follwoing职位数解决方案,而sucsess:
Updating record rails 4 No route matches [PATCH] "/admin/usersupdate"
how to fix "No route matches [PATCH]"
Routing Error No route matches [PATCH] "/locations"

回答

0

你只定义在您的路线中显示动作,请更改以下行:

resources :projects, only: [:show] 

要:

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

或者

resources :projects 
+0

这个工作,但它不会是最好有更新,并在管理范围内创建? – Asan

+0

它依赖于,如果你想在管理范围内更新或创建项目,然后添加它。 –

+0

但是当我得到错误时,我在管理范围中进行了更新,然后在管理范围之外我没有发生错误。是否有意义? – Asan