lundi 19 mars 2018

ActiveModel::ForbiddenAttributesError when creating a reply to a forum post

I am using rails 5.0.0. I get the following error when I try to create a reply to a forum post.

Started POST "/discussions/7/replies" for 108.252.220.249 at 2018-03-19 15:54:44 +0000
Cannot render console from 108.252.220.249! Allowed networks: 127.0.0.1,::1, 127.0.0.0/127.255.255.255
Processing by RepliesController#create as JS
Parameters: {"utf8"=>"✓", "reply"=>{"reply"=>""}, "commit"=>"Submit Reply", "discussion_id"=>"7"}
User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
Discussion Load (0.2ms)  SELECT  "discussions".* FROM "discussions" WHERE "discussions"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
(0.1ms)  begin transaction
(0.1ms)  rollback transaction
Completed 401 Unauthorized in 10ms (ActiveRecord: 1.0ms)



ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):

This is my part of my replies controller file. The error makes it seem like there something is wrong with the create action so that is what I have included for this post. Also, my schema, there is a 'replies' table with a column named 'reply' which is the actual text the user wants to submit as their response. Sorry for the confusing naming scheme.

I feel like the create action is correct, and that there is something wrong elsewhere. Like a strong parameters type of problem? Are there any suggestions as to where else in my files could cause an error like this to arise?

class RepliesController < ApplicationController
before_action :authenticate_user!
before_action :set_reply, only: [:edit, :update, :show, :destroy]
before_action :set_discussion, only: [:create, :edit, :show, :update, :destroy]

def create
    #create a reply within the discussion and save userid to the reply
    @reply = @discussion.replies.create(params[:reply]).permit(:reply, :discussion_id)
    @reply.user_id = current_user.id

    respond_to do |format|
        if @reply.save
            format.html {redirect_to discussion_path(@discussion)}
            format.js #render create.js.erb

        else
            format.html{redirect_to discussion_path(@discussion), notice: 'Reply did not save. Try again'}
            format.js
        end
    end
end

...

private
def set_discussion
    @discussion = Discussion.find(params[:discussion_id])
end

def set_reply
    @reply = Reply.find(params[:id])
end

def reply_params
    params.require(:reply).permit(:reply)
end

end




Aucun commentaire:

Enregistrer un commentaire