Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dena G
441 PointsUndefined method 'constant' error
Hi all,
I'm getting a NoMethodError in PostsController#create for undefined method `content' for line: if @post.save
It was working before now I can't post anything. Any help will be greatly appreciated. Thanks in advance.
Posts/Controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 25)
end
def show
end
def new
@post = current_user.posts.build
end
def edit
end
def create
@post = current_user.posts.build(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
def correct_user
@post = current_user.posts.find_by(id: params[:id])
redirect_to posts_path, notice: "Not authorized to edit this post" if @post.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:description, :image)
end
end
Posts/Index
<div class="page-header">
<center><strong><h1> Explore Page </h1></strong></center>
</div>
<% if user_signed_in? %>
<center><%= link_to 'New Post', new_post_path, class: "btn btn-danger btn-lg active" %></center>
<% end %>
<div id="posts" class="transitions-enabled">
<% @posts.each do |post| %>
<div class="box panel panel-default">
<%= link_to image_tag(post.image.url(:medium)), post %>
<div class="panel-body">
<%= post.description %><br/>
<strong><%= post.user.name if post.user %></strong>
<% if post.user == current_user %>
<div class="actions">
<%= link_to edit_post_path(post) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<%= link_to post, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-trash"></span>
Delete
<% end %>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
<div class="center">
<%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
</div>
Post Model
class Post < ActiveRecord::Base
belongs_to :user
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png"]
validates :description, presence: true
validates :image, presence: true
# Returns microposts from the users being followed by the given user.
def self.from_users_followed_by(user)
followed_user_ids = user.followed_user_ids
where("user_id IN (:followed_user_ids) OR user_id = :user_id",
followed_user_ids: followed_user_ids, user_id: user)
end
end
Dena G
441 PointsHi Maciej,
I just realized what was wrong when you asked for the model. Thank you for the response. I got it to work now.
Maciej Czuchnowski
36,441 Points:)
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsCan you show the model and schema?