###前提・実現したいこと
ブログ機能を作っているのですが、投稿失敗した時にエラーを吐いてかつjavascriptが読み込まれるようにしたいと思っています。(javascriptはタグ機能を記事投稿画面で使っているので読み込むようにしたいです。)
###該当の箇所(コントローラー)
/articles/controller/articles_controller.rb def create @article = Article.new(article_params) respond_to do |format| if @article.save format.html { redirect_to portfolio_path(@article), notice: 'Article was successfully created.' } format.json { render :show, status: :created, location: @article } redirect_to portfolio_path(@article) else format.html { render :new } format.json { render json: @article.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @article.update(article_params) format.html { redirect_to portfolio_path(@article), notice: 'Article was successfully updated.' } format.json { render :show, status: :ok, location: @article } else format.html { render :new } format.json { render json: @article.errors, status: :unprocessable_entity } end end end
require 'length_with_wide_char_validator' class Article < ActiveRecord::Base validates :image, :video_id, presence: true validates :title, :length_with_wide_char => {:maximum => 70} validates :description, length: { maximum: 500 } end
上記のようにモデルにバリデーションを書いているのでエラーは正常に表示されます。しかし、投稿失敗した時にformat.html { render :new }で得るページでjsファイルが読み込まれません。読み込むためにどのような記述をすればいいのか教えていただきたいです。
###追記
railsでデフォルトで入っているturbolinksはoffにしてあります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/26 09:44
2016/09/26 12:26