質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1677閲覧

Scaffoldで作成したresourcesにcollection doを追加した際のエラー

mig2332

総合スコア33

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2016/04/16 12:29

編集2016/04/17 05:47

###前提・実現したいこと
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

追加でルーティングを追加することは可能です。
idを引数として取りたい場合は、member doを、それ以外はcollection doを使います。

resources :notes do member do get 'rule' end end

投稿2016/04/16 14:03

miyamiya

総合スコア691

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mig2332

2016/04/17 05:30

ありがとうございます!!collection do を使用してルーティングを追加しました。 するとエラーが Couldn't find Note with 'id'= def set_note @note = Note.find(params[:id]) end に変わりました。(id=の先にruleがなくなった) しかしまだ似たようなエラーがでます。 試しにmemberを使って http://localhost:3000/notes/:id/rule としてみるとエラーなくできました。 collectionを使いたいのですが、collectionだとエラーがでてしまいます。 (rake route) rule_notes_path   GET  /notes/rule(.:format)  notes#rule
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問