Ask A Question

Notifications

You’re not receiving notifications from this thread.

Using Active Storage to upload images not working.

Shan Siddiqui asked in Rails

Hey guys,

I could really use your assistance with my current app. I decided to add the active storage feature and followed the guide on how to set it up. I did a practice project first to test the water then I added it to my existing project this is the error I keep getting "ActiveModel::UnknownAttributeError in CoursesController#create"Let me show you my code

Let me show you my code:

This is the model:

class Course < ApplicationRecord
def star_number
self.star.blank? ? 1 : self.star
end
def cover
has_one_attached :file
end
end

Here is the Controller: The error keeps pointing to ' @course = Course.new(course_params) '

class CoursesController < ApplicationController
before_action :set_course, only: %i[ show edit update destroy ]
# GET /courses or /courses.json
def index
u/courses = Course.all
end
# GET /courses/1 or /courses/1.json
def show
end
# GET /courses/new
def new
u/course = Course.new
end
# GET /courses/1/edit
def edit
end
# POST /courses or /courses.json
def create
u/course = Course.new(course_params) This is where the Error is coming from?
respond_to do |format|
if u/course.save
format.html { redirect_to course_url(@course), notice: "Course was successfully created." }
format.json { render :show, status: :created, location: u/course }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: u/course.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /courses/1 or /courses/1.json
def update
respond_to do |format|
if u/course.update(course_params)
format.html { redirect_to course_url(@course), notice: "Course was successfully updated." }
format.json { render :show, status: :ok, location: u/course }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: u/course.errors, status: :unprocessable_entity }
end
end
end
# DELETE /courses/1 or /courses/1.json
def destroy
u/course.destroy
respond_to do |format|
format.html { redirect_to courses_url, notice: "Course was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
u/course = Course.find(params[:id])
end
# Only allow a list of trusted parameters through.
def course_params
params.require(:course).permit(:video, :title, :star, :description, :public, :file)
end
end
_attachment.html.erb:

<%if post.file.attached%>
<div class="row">
<%if post.file.image?%>
<div class="message-image-container">
<%= image_tag(post.file, class:"message-image") %>
</div>
<%end%>
</div>
<%end%>

Lastly the course file

<div id="<%= dom_id course %>">
<p>
<strong>Image:</strong>
<%= course.image %>
<%= render 'courses/attachment', course: course %>
</p>
<p>
<strong>Video:</strong>
<%= course.video %>
</p>
<p>
<strong>Title:</strong>
<%= course.title %>
</p>
<p>
<strong>Star:</strong>
<%= course.star %>
</p>
<p>
<strong>Description:</strong>
<%= course.description %>
</p>
</div>

It is most likely something with the controller, but the error is not helping me out. I am having a hard time figuring it out, can you guys assist?

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.