前提・実現したいこと
Ruby on rails(ver5.2.3)で勉強の為に簡単なブログのようなものを作っています。
Form_withで投稿機能を実装しましたが、モデルにデータが飛ばないこと、画面が遷移しないことで困っています。
発生している問題・エラーメッセージ
![
Create Articleを押すとArticleモデルにデータが保存され、ホーム画面に遷移する様にしたい。
該当のソースコード
rails
1new.html.haml 2 3.container 4 = form_with model: Article.new, local: true do |f| 5 %form.col.s12 6 .row 7 .input-field.col.s6 8 =f.text_field :title, class: "validate", id:"article_title" 9 =f.label :title 10 %form.col.s12 11 .row 12 .input-field.col.s12 13 =f.text_area :text, class: "materialize-textarea", id:"textarea1" 14 =f.label :text 15 =f.label :image 16 =f.file_field :image 17 .row 18 .row 19 =f.submit
rails
1 2articles_controller 3 4class ArticlesController < ApplicationController 5 6 def index 7 end 8 9 def new 10 @article = Article.new 11 end 12 def create 13 Article.create(article_params) 14 redirect_to article_url, notice: "投稿しました" 15 end 16 17 private 18 def article_params 19 params.require(:article).parmit(:title,:text,:image) 20 end 21end
rails
1Rails.application.routes.draw do 2 devise_for :users 3 resources :articles 4 root 'articles#index' 5end 6
試したこと
form_forで記載したところ画面の遷移はされているようでしたがモデルには保存されていませんでした。
<%= form_for(@article) do |f| %> <div> <div> <%= f.label :title %> </div> <%= f.text_field :title,id: :title%> </div> <div> <div> <%= f.label :image %> </div> <%= f.file_field :image, id: :image %> </div> <p><%= f.check_box :remove_image %>画像を削除する</p> <div> <div> <%= f.label :text %> </div> <%= f.text_area :text,id: :text,rows: 20,cols: 75 %> </div> <div><%= f.submit "投稿" %></div> <% end %> </div>
補足情報(FW/ツールのバージョンなど)
Rails 5.2.3
materializeCSSを使っています。
導入しているgemは
'pry-rails'
'compass-rails'
'sprockets'
'devise'
'mini_magick'
'haml-rails'
です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。