表記の通り、railsで簡単な掲示板作っているのですが、新規投稿時にActiveModel::ForbiddenAttributesErrorが発生します。
関連するコードを記載しますので、ご教示いただければ幸いです。
なお、http://localhost:3000/topicsや
http://localhost:3000/topics/newそれぞれでは正しく表示されます。
コントローラー
class TopicsController < Ap![イメージ説明](35ca876ac464d026adf6615d5bb1e0e7.png)plicationController before_action :set_topic, only: [:show, :edit, :update, :destroy] # GET /topics # GET /topics.json def index @topics = Topic.all end # GET /topics/1 # GET /topics/1.json def show end # GET /topics/new def new @topic = Topic.new end # GET /topics/1/edit def edit end # POST /topics # POST /topics.json def create @topic = Topic.new(topic_params) respond_to do |format| if @topic.save format.html { redirect_to @topic, notice: 'Topic was successfully created.' } format.json { render :show, status: :created, location: @topic } else format.html { render :new } format.json { render json: @topic.errors, status: :unprocessable_entity } end end end # PATCH/PUT /topics/1 # PATCH/PUT /topics/1.json def update respond_to do |format| if @topic.update(topic_params) format.html { redirect_to @topic, notice: 'Topic was successfully updated.' } format.json { render :show, status: :ok, location: @topic } else format.html { render :edit } format.json { render json: @topic.errors, status: :unprocessable_entity } end end end # DELETE /topics/1 # DELETE /topics/1.json def destroy @topic.destroy respond_to do |format| format.html { redirect_to topics_url, notice: 'Topic was successfully destroyed.' } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_topic @topic = Topic.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def topic_params params.fetch(:topic, {}) end end
ビュー
<h1>New Topic</h1> <%= form_for(@topic) do |f| %> <%= f.text_field :title %> <%= f.text_field :description %> <%= f.submit %> <% end %> <%= link_to 'Back', topics_path %>
ルーティング
Rails.application.routes.draw do get '/' => 'home#top' resources :topics end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。