LoginSignup
0
0

More than 1 year has passed since last update.

enumの値によって遷移先を変えたい

Posted at

はじめに

現在プログラミング学習を始めて3ヶ月目の初学者です。
学んだことをqiitaに投稿という形でアウトプットするため、また備忘録として記事を作成しました。

考えた方法

if文で条件分岐

if文

パッと思いつくほどif文を使いこなせていないので、とにかく調べる
https://kurose.me/rails-enum-login/
こちらの記事を参考にして
作ったif文がこちら

def create
@task = Task.new(task_params)

respond_to do |format|
  if @task.save
    format.html {
      if @task.category(0) == "時間が決まったタスク"
        redirect_to time_path
      elsif @task.category(1) == "よく使うタスク"
        redirect_to every_path
      elsif @task.category(2) == "たまたま行ったタスク"
        redirect_to by_chance_path
      elsif @task.category(3) == "ToDo"
        redirect_to todo_path
      else
        render :root
      end
    }
    format.json { render :time, status: :created, location: @task }
  else
    format.html { render :new, status: :unprocessable_entity }
    format.json { render json: @task.errors, status: :unprocessable_entity }
  end
end

end

※controllerはscaffoldコマンドで作成

わかっていたがエラー
ArgumentError in TasksController#create
wrong number of arguments (given 1, expected 0)
引数の値が0を期待しているが、1つ送られてきてる 

記事をよく確認したら引数入れてなかった!
なのでcategoryの引数を消したら無事遷移できました!

最後に

qiitaへの投稿に慣れていないので、分かりづらい上に情報が少ないですが、これから欠かさず投稿していこうと思います。

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