@MAAAAAAM

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

投稿内容が保存されない

Q&A

Closed

解決したいこと

image.png

はじめましてプログラミング初心者です
Ruby on Railsで簡単な募集掲示板アプリを作成しているのですが
募集内容を投稿する機能の実装中にエラーが発生してしまいました。
ActiveHashを使用しパラメーターは送られているのですが保存ができず
困っています。ご教授いただけないでしょうか。

発生している問題・エラー

NoMethodError in PostsController#create

image.png

コントローラー posts_controller.rb

class PostsController < ApplicationController
  def index 
    @posts = Post.includes(:user).order("created_at DESC")
  end

  def new
    @post = Post.new
  end

  def create
    #binding.pry
    @post = Post.new(post_params)
    if @post.save
      redirect_to root_path
    else
      render :new
    end
  end

  def search
    @posts = Post.search(params[:keyword])
  end

  private
  def post_params
    params.require(:post).permit(:game, :content, :talk_id, :mokuteki_id, :level_id, :vc_id).merge(user_id: current_user.id)
  end

end

モデルpost.rb

class Post < ApplicationRecord
  belongs_to :user, optional: true
  has_one_attached :image

  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :talk
  belongs_to :mokuteki
  belongs_to :level
  belongs_to :vc

  validates :game, :content, presence: true 

  with_options presence: true, numericality: { other_than: 1, message: "can't be blank" } do
    validates :talk_id
    validates :mokuteki_id
    validates :level_id
    validates :vc_id
  end

  def self.search(search)
    if search != ""
      Post.where('game LIKE(?)', "%#{search}%")
    else
      Post.all
    end
  end
end

  モデルlevel.rb

class Level < ActiveHash::Base
  self.data = [
    { id: 1, name: '--' },
    { id: 2, name: '初心者' },
    { id: 3, name: '経験者' },
    { id: 4, name: '中級者' },
    { id: 4, name: '上級者' }
  ]
  include ActiveHash::Associations
  has_many :posts
end

routes.rb

Rails.application.routes.draw do
  devise_for :users
  root to: "posts#index"

  resources :posts do
    collection do
      get 'search'
    end
  end
end

ビュー search.html.erb

<%= render "tnp/header" %>
<div class = "search">
  <%= form_with(url: search_posts_path, local: true, method: :get, class: "seach-form") do |f| %>
    <%= f.text_field :keyword, placeholder: "投稿を検索する", class: "seach-input" %>
    <%= f.submit "検索", class: "seach-btn" %>
  <% end %>
</div>
<div class = "nav" >
  <%= link_to "投稿する", new_post_path, class: :nav__btn %>
</div>
<div class="contents">
  <% @posts.each do |post| %>
    <div class="content">
      <div class="content_name">
        <%= post.user.name%>
      </div>
      <div>
        <%= @post.talk.name %>
        <%= @post.mokuteki.name %>
        <%= @post.level.name %>
        <%= @post.vc.name %>
      </div>
      <div class="content_game">
        <%= post.game%>
      </div>
      <div class="content_text">
        <%= post.content%>
      </div>
    </div>
  <% end %>
</div>

自分で試したこと

Levelクラスのcurrent_scopeメソッドが見つからない?と解釈し
バリデーションやアソシエーションを何度も見直したのですが間違ってはいないのかなと思いビューファイルの記述方法も確認してみたがこれでいいと思っています。
他に思い当たるところがなく解決できませんでした。

補足

関係はないとは思いますが
ActiveHashのモデルを作成する前に'arity'という名前のコントローラー、モデル、テーブルを間違えて作ってしまい一応削除しています。
rails db:rollback

ファイル削除

rails d model arity

rails d controller arity
作成したことによって他に影響が出てしまっているのでしょうか、、、

0 likes

2Answer

Comments

  1. @MAAAAAAM

    Questioner

    遅くなってしまいすみません。
    追加で編集させていただきました。

Comments

  1. @MAAAAAAM

    Questioner

    調べていただきありがとうございます。
    試行錯誤しモデルpost.rbのvalidates :level_idをコメントアウトしてみたところ保存され表示できるようになりました。
    とりあえず進めなくてはいけないので一旦このやり方で進めようと思います。
    あとはなぜvalidates :level_idを外すと保存できるようになったのかの理解ができていないので時間ができた時にまた調べようと思います。
    最後までご丁寧にありがとうございます。
  2. @MAAAAAAM

    Questioner

    補足モデルを作り直すことでvalidatesをしても保存できるようになりました。

Your answer might help someone💌