2016-02-09 56 views
0

我遇到問題「沒有路由匹配{:action =>」show「,:controller =>」picks「,:id => nil} [:id]「作用域資源 - 缺少必需的鍵:[:id]

我想要example.com/staff/picks,所以我在路由中做了範圍。直到我創建發佈新帖子的表單爲止。由於缺少身份證明,現在無法顯示帖子。

routes.db

resources :places 
    resources :events 
    root "search#index" 
    post 'find' => 'search#find', as: :find 


    get '/staff', to: redirect('staff/picks') 

    scope 'staff' do 
    resources :picks 
    resources :discover 
    end 

picks_controller.rb

class PicksController < ApplicationController 
before_action :staff_validate 
    def index 
    @pickplaces = PickPlaces.all.order('created_at DESC') 
    end 

    def new 
    @pickplaces = PickPlaces.new 
    end 

    def create 
    @pickplaces = PickPlaces.new(pickplaces_params) 
    if @pickplaces.save 
     redirect_to pick_path 
    else 
     render 'new' 
    end 
    end 

    def show 
    @pickplaces = PickPlaces.find(params[:id]) 
    end 

    def edit 
    @pickplaces = PickPlaces.find(params[:id]) 
    end 

    def update 
    @pickplaces = PickPlaces.find(params[:id]) 
    if @pickplaces.update(params[:pickplaces].permit(:title, :about, :location, :kind)) 
     redirect_to @pickplaces 
    else 
     render 'edit' 
    end 
    end 

    private 

    def pickplaces_params 
    params.require(:pickplaces).permit(:title, :about, :location, :kind) 
    end 
end 

staff_controller.rb

class StaffController < ApplicationController 
before_action :staff_validate 
    def index 

    end 
end 

new.html.slim - 查看/精選

h1 Add some place that people will love 
= form_for :pickplaces, url: picks_path do |f| 

    p 
    = f.label "Title" 
    = f.text_field :title, placeholder: 'Golden Gate' 

    p 
    = f.label "Location" 
    = f.text_field :location, placeholder: 'California, US' 

    p 
    | Select place type 
    = f.radio_button(:kind, "city", :checked => true) 
    = f.label(:placetype_city, "City") 
    = f.radio_button(:kind, "bridge") 
    = f.label(:placetype_bridge, "Bridge") 
    = f.radio_button(:kind, "other") 
    = f.label(:placetype_other, "Other") 

    p 
    = f.label "About" 
    = f.text_area :about, placeholder: "World's most amazing place" 

    = f.submit "Go Public!" 

index.html.slim - 查看/精選

h1 Staff Picks 

- @pickplaces.each do |pickplaces| 

    h2 
    = link_to pickplaces.title, pick_path(@picks) 

這是有點髒

回答

0

在索引視圖你使用未在任何地方設置的實例變量@picks,導致路由錯誤。

相反,你應該使用pickplaces變量從集合:

- @pickplaces.each do |pickplaces| 
    h2 
    = link_to pickplaces.title, pick_path(pickplaces) 
+0

謝謝!而已!以及如何在控制器重定向?當我發佈新帖子時,應該將用戶重定向到剛創建的帖子 //編輯:已解決! redirect_to pick_path(@pickplaces)我忘了@。再次感謝你! –

相關問題