###前提・実現したいこと
Scaffoldでnotesというresourcesをつくりました。
そのnotesの中にruleというルーティングを追加するために
resources :notes do
get 'rule'
end
をroutes.rbに追加しました。
しかし http://localhost:3000/notes/rule をしてみると下記のエラーがでます。
初心者です。
###発生している問題・エラーメッセージ
Couldn't find Note with 'id'=rule def set_note @note = Note.find(params[:id]) end
###該当のソースコードここに言語を入力
notes_controller.rb
class NotesController < ApplicationController before_action :set_note, only: [:show, :edit, :update, :destroy, :rule] # GET /notes # GET /notes.json def index @notes = Note.all end # GET /notes/1 # GET /notes/1.json def show end # GET /notes/new def new @note = Note.new end # GET /notes/1/edit def edit end def rule end # POST /notes # POST /notes.json def create @note = Note.new(note_params) if params[:back] render :new elsif @note.save redirect_to @note, notice:"作成完了" else render :new end end # PATCH/PUT /notes/1 # PATCH/PUT /notes/1.json def update respond_to do |format| if @note.update(note_params) format.html { redirect_to @note, notice: 'アップロードが完了しました' } format.json { render :show, status: :ok, location: @note } else format.html { render :edit } format.json { render json: @note.errors, status: :unprocessable_entity } end end end # DELETE /notes/1 # DELETE /notes/1.json def destroy @note.destroy respond_to do |format| format.html { redirect_to notes_url, notice: '掲載が削除されました' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_note @note = Note.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def note_params params.require(:note).permit(:title, :content, :price, :image_1, :image_2, :image_3, :category, :rule) end end
$rake routes
root GET / home#index note_rule GET /notes/:note_id/rule(.:format) notes#rule notes GET /notes(.:format) notes#index POST /notes(.:format) notes#create new_note GET /notes/new(.:format) notes#new edit_note GET /notes/:id/edit(.:format) notes#edit note GET /notes/:id(.:format) notes#show PATCH /notes/:id(.:format) notes#update PUT /notes/:id(.:format) notes#update DELETE /notes/:id(.:format) notes#destroy
###試してみたこと
collection doかmember doをルーティングに使ってみるという回答をいただきたcollection doを使用しました。
するとhttp://localhost:3000/notes/ruleでエラーが
ActiveRecord::RecordNotFound in NotesController#rule Couldn't find Note with 'id'= def set_note @note = Note.find(params[:id]) end
$rake routes
rule_notes GET /notes/rule(.:format) notes#rule
に変わりました。
:idを含むmember do ではエラーなくruleが表示されました。
collection doを使用したいのですが、エラーが表示されます。
###補足情報(言語/FW/ツール等のバージョンなど)
Ruby on Rails
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/17 05:30