LoginSignup
0
0

More than 3 years have passed since last update.

【Rails】ルーティング resourcesメソッド

Last updated at Posted at 2020-09-16

resourcesを使って、indexアクションを設定に仕方。

resourcesを使って、indexアクションを設定し、tweetsコントローラーの
indexアクションを呼ぶパスを指定します。

config/routes.rb

Rails.application.routes.draw do
  resources :tweets, only: :index #追記
end

これで、tweetsというパスを指定すると、tweetsコントローラーのindexアクションの処理
を実行することができるようになりました。

しかし、まだtweetsコントローラーを作成していないためこのままだとエラーが生じてしまいます。

tweetsコントローラーを作成しよう

tweetsコントローラーをコマンドを使って作成します。

ターミナルで下記のコマンドを実行しましょう
作成するコントローラーはtweets_controllerとします。

ターミナル
# コントローラーを作成
$ rails g controller tweets

コントローラーを作成が完了です。

正しく作成できれば下記の様な出力結果が表示されます。

ターミナル

create  app/controllers/tweets_controller.rb
invoke  erb
create    app/views/tweets
invoke  test_unit
create    test/controllers/tweets_controller_test.rb
invoke  helper
create    app/helpers/tweets_helper.rb
invoke    test_unit
create      test/helpers/tweets_helper_test.rb
invoke  assets
invoke    coffee
create      app/assets/javascripts/tweets.js.coffee
invoke    scss
create      app/assets/stylesheets/tweets.css.scss
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0