LoginSignup
6
9

More than 5 years have passed since last update.

Railsで簡単にパラメータを受け取る方法

Posted at

Controller 修正

Controller の元ページにパラメータを受け取るための記述をする

project.controller.rb
def index
    @projects = Project .order(params[:sort])
...
end

View 修正

以下のリンクを追加してパラメータを渡す。
(リンクでなくても良い)

index.html.erb
<%= link_to "Name", sort: "name" %>
<%= link_to "Price", sort: "price" %>
<%= link_to "Created", sort: "created_at" %>
…

ここまで終わったらブラウザで確認する。

$ bundle exec rails server

以上でroutes.rbをいじったり、特にメソッドを追加することなくパラメータを渡すことができるようになる。

参考

Rails3 のビューでテーブルの題名クリックするとソートするメモ

6
9
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
6
9