New to ruby. Using baserails. Experencing problems - ruby-on-rails

I have been recently trying to learn ruby by using ruby on rails with baserails. Everything was fine until I got in the spot in the tutorial where there wasn't much guidance on how to install s3 using paperclip the tutorial was for dropbox. I am so lost and have been getting errors for 2 days. I have researched this issue alot, but with my limited knowledge I am having a hard time understanding it. I know my issue is a paperclip/s3 issue. I have all my gems up to to date. Could someone please work with me and help guide me in fixing my issue. Here is my listings_controller.fb file. Line 66 gives me this error.
uninitialized constant ListingsController::Listing.
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
# GET /listings
# GET /listings.json
def index
#listings = Listing.all
end
# GET /listings/1
# GET /listings/1.json
def show
end
# GET /listings/new
def new
#listing = Listing.new
end
# GET /listings/1/edit
def edit
end
# POST /listings
# POST /listings.json
def create
#listing = Listing.new(listing_params)
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render :show, status: :created, location: #listing }
else
format.html { render :new }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /listings/1
# PATCH/PUT /listings/1.json
def update
respond_to do |format|
if #listing.update(listing_params)
format.html { redirect_to #listing, notice: 'Listing was successfully updated.' }
format.json { render :show, status: :ok, location: #listing }
else
format.html { render :edit }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
#listing = Listing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
end
______________Terminal_______________-
Started GET "/listings/1" for 127.0.0.1 at 2017-02-09 21:36:39 +0900
Processing by ListingsController#show as HTML
Parameters: {"id"=>"1"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)
NameError (uninitialized constant ListingsController::Listing):
app/controllers/listings_controller.rb:67:in `set_listing' Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
(5.2ms) Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms) Rendering
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
(1.3ms) Rendered
/Users/chrisdionne/.rvm/gems/ruby-2.3.3/gems/actionpack-5.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb
within rescues/layout (161.7ms)

Related

NoMethodError Rails 3.2

I've been searching all over and can't find the answer to this anywhere.
I'm taking the intro to RoR course on udemy, and I've been able to solve all the problems I've had in the first 80% of the course, but now I'm at a roadblock and can't find this. We're building an app like Etsy, and I'm at the point where I need to restrict users from editing/deleting listings that don't belong to them.
I'm running Ruby 1.9.3 on Rails 3.2.21
I tried following the instructions for adding the check user filter, but when I checked back on local host, I received this error:
NoMethodError in ListingsController#edit
undefined method `user' for nil:NilClass
app/controllers/listings_controller.rb:98:in `check_user'
Parameters:
{"id"=>"8"}
My code matches the instructor's code exactly, but I think this error is because I'm using Rails 3, and he's using 4.
Here's my listings_controller.rb
class ListingsController < ApplicationController
# GET /listings
# GET /listings.json
before_filter :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_filter :check_user, only: [:edit, :update, :destroy]
def index
#listings = Listing.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #listings }
end
end
# GET /listings/1
# GET /listings/1.json
def show
#listing = Listing.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #listing }
end
end
# GET /listings/new
# GET /listings/new.json
def new
#listing = Listing.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #listing }
end
end
# GET /listings/1/edit
def edit
#listing = Listing.find(params[:id])
end
# POST /listings
# POST /listings.json
def create
#listing = Listing.new(params[:listing])
#listing.user_id = current_user.id
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render json: #listing, status: :created, location: #listing }
else
format.html { render action: "new" }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# PUT /listings/1
# PUT /listings/1.json
def update
#listing = Listing.find(params[:id])
respond_to do |format|
if #listing.update_attributes(params[:listing])
format.html { redirect_to #listing, notice: 'Listing was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
#listing = Listing.find(params[:id])
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url }
format.json { head :no_content }
end
end
private
def set_listing
#listing = Listing.find(params[:id])
end
def listing_params
params.require(:listing).permit(:name, :description, :price, :image)
end
def check_user
if current_user != #listing.user
redirect_to root_url, alert: "Sorry, this listing belongs to someone else."
end
end
end
The code that we had to add for this is the second before_filter and the def check_user
If any other information is needed to help answer this, please let me know.
It's not a Rails 3 vs 4 issue, your code never calls set_listing and so #listing is never being set. You should probably have a:
before_filter :set_listing, only: [:show, :edit, :update, :destroy]
at the top of your file, before the before_filter :check_user, ...

undefined method `update' for nil:NilClass post scaffold

I am trying to update a post, and ever since I added the Redcarpet gem, I get an error when I try to update a post.
Here is the error: undefined method 'update' for nil:NilClass
Here is my posts controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find_by_urlid(params[:urlid])
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edite
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.urlid = SecureRandom.urlsafe_base64
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Your Post was successfully created!' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Your Post was successfully updated!' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'You have successfully deleted the Post!' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find_by_urlid(params[:urlid])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:urlid, :author, :title, :body, :likes, :dislikes, :tags)
end
end
I've look at other posts, and their answers don't work. Any ideas?
Edit:
Server Log:
Started PATCH "/posts/sfqm5y99cbomqh4nmuxq5w" for 127.0.0.1 at 2014-10-31 13:05:07 -0700
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FRRgB2pI6GAQa6YL8KLZrUShshjjGd7+IXrLv1hTi5E=", "post"=>{"author"=>"Un3qual", "title"=>"First post", "body"=>" on new *dev* system **:D**", "likes"=>"9999", "dislikes"=>"0", "tags"=>""}, "commit"=>"Update Post", "urlid"=>"sfqm5y99cbomqh4nmuxq5w"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."urlid" = 'sfqm5y99cbomqh4nmuxq5w' LIMIT 1
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `update' for nil:NilClass):
app/controllers/posts_controller.rb:46:in `block in update'
app/controllers/posts_controller.rb:45:in `update'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.9ms)
Started PATCH "/posts/sfqm5y99cbomqh4nmuxq5w" for 127.0.0.1 at 2014-10-31 15:42:18 -0700
Processing by PostsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FRRgB2pI6GAQa6YL8KLZrUShshjjGd7+IXrLv1hTi5E=", "post"=>{"author"=>"Un3qual", "title"=>"First post", "body"=>" on new *dev* system **:D**", "likes"=>"9999", "dislikes"=>"0", "tags"=>""}, "commit"=>"Update Post", "urlid"=>"sfqm5y99cbomqh4nmuxq5w"}
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."urlid" = 'sfqm5y99cbomqh4nmuxq5w' LIMIT 1
Completed 500 Internal Server Error in 4ms
NoMethodError (undefined method `update' for nil:NilClass):
app/controllers/posts_controller.rb:46:in `block in update'
app/controllers/posts_controller.rb:45:in `update'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.2ms)
"Post.find_by_urlid(params[:urlid])" code has deprecated "find_by_" method. Rails 4 has deprecated "find_by_" methods. [Link]: http://edgeguides.rubyonrails.org/4_0_release_notes.html
Try Using: Post.find_by(urlid: params[:urlid]).
There is a typo error in "edite" method.
Based on the comments to your question, this is happening because there is no record with this urlid.
You should change your callback (before_action) filter like this.
def set_post
#post = Post.find_by(urlid: params[:urlid])
redirect_to posts_path, flash: {error: "The post does not exists"} if #post.nil?
end
This will redirect to the index of the posts, with an error message (you should have a div for errors flashing in your layout or your templates for it to appear).
If the #post exists, then it will relay to the action normally.
If you're not using any preloader which instantiate #post variable then it's the problem. For update action you should have:
def update
#post = Post.find params[:id]
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Your Post was successfully updated!' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end

rails generate scaffold command on Windows causes ArgumentError

I used "rails generate scaffold project" to create an new web application. I already did this in the past on Linux and Mac OSX running other versions of rails and ruby and all worked fine, but this time I'm working on Windows 7. Here is my environment
C:\Users\user1\Company>ruby -v
ruby 2.0.0p451 (2014-02-24) [x64-mingw32]
C:\Users\user1\Company>rails -v
DL is deprecated, please use Fiddle
Rails 4.1.0
C:\Users\user1\Company>
after I ran the scaffold command, I ran rake db:migrate and I was able to create my first project successfully. Then I can edit the project, but when I click update, I get the following error message
ArgumentError (When assigning attributes, you must pass a hash as an argument.):
Full server log message
Started PATCH "/projects/1" for 127.0.0.1 at 2014-04-29 05:16:33 -0700
Processing by projectsController#update as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"gST6BUQNwOZQDYVj60DXLuFANv1JsM02YAIM+xYwt/M=", "commit"=>"Update project", "id"=>"1"}
project Load (0.0ms) SELECT "projects".* FROM "projects" WHERE "projects"."id"= ? LIMIT 1 [["id", 1]]
(1.0ms) begin transaction
(0.0ms) rollback transaction
Completed 500 Internal Server Error in 10ms
ArgumentError (When assigning attributes, you must pass a hash as an argument.):
app/controllers/projects_controller.rb:44:in `block in update'
app/controllers/projects_controller.rb:43:in `update'
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
Rendered C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (104.0ms)
Here is my "update" method (as was created automatically by the scaffold command)
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if #project.update(project_params)
format.html { redirect_to #project, notice: 'Update Successful!' }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :edit }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
I tried removing the "PATCH/" keyword, but no luck. I replaced the whole method with the following (this worked for my other application, but not this time on Windows)
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
#project = Project.find(params[:id])
if #project.update_attributes(params[:project])
flash[:notice] = "Update Successful!"
end
respond_with(#project)
end
but this did not make any difference.
I also tried (I found this by browsing SO)
# PUT /projects/1
# PUT /projects/1.json
def update
#project = Project.find(params[:id])
respond_to do |format|
if #project.update_attributes(params[:project])
format.html { redirect_to #project, notice: 'Update Successful!' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
but no success either
Here are the other working methods (all as automatically generated by "rails generate scaffold" command, and they all work fine)
# GET /projects
# GET /projects.json
def index
#projects = project.all
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
#project = project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
#project = project.new(project_params)
respond_to do |format|
if #project.save
format.html { redirect_to #project, notice: 'Creation Successful!' }
format.json { render :show, status: :created, location: #project }
else
format.html { render :new }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
Why is "rails generate scaffold" command not working on Windows and working fine on Linux and Mac OSX?
Update 1
Here are the other methods that were automatically created by "rails generate scaffold" command
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
<other methods listed above : index, show, new, edit, create, update, and destroy>
private
# Use callbacks to share common setup or constraints between actions.
def set_project
#project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params[:project]
end
end
Working Code after making the changes suggested by Kirti Thorat
This is what worked for me
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
#project = Project.find(params[:id])
respond_to do |format|
if #project.update(project_params)
format.html { redirect_to #project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: #project }
else
format.html { render :edit }
format.json { render json: #project.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
#project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:UnitMgtAddress)
end
You just created a scaffold without any other fields. So your projects table simply has 3 default fields id, created_at and updated_at. Because of which in your update action when you do:
if #project.update(project_params)
Or
if #project.update_attributes(params[:project])
you get error as update and update_attributes require a Hash as an argument and params[:project] is nil. Look at your params hash from server log
{"utf8"=>"√", "authenticity_token"=>"gST6BUQNwOZQDYVj60DXLuFANv1JsM02YAIM+xYwt/M=", "commit"=>"Update project", "id"=>"1"}
It doesn't have a key project.
Possible Solutions
Without adding new fields
If you are not planning to add any new fields in your projects table then there is no point in having an update action as what field would you update on?
With adding new fields
You can add new fields to your projects table like name, duration, etc as per your requirement (By creating a migration to add new fields).
After this you would just need to update the project_params method as below:
def project_params
params.require(:project).permit(:name, :duration)
end

Unable to sign_up using Devise and CanCan in Rails

I been working on my application in which I have a number of roles and also some users.
However suddenly I can't sign-up anymore. Signing in works fine, but signing up always redirects me back to the signing page and produces the same error:
The console always shows the following when I try to sign up:
Started POST "/registers/user/registration" for 127.0.0.1 at 2013-04-18 00:48:22 +0100
Processing by RegistersController#registration as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ofucDxxzZFbTVLfVaA3QxixOHL2V9hzxfSFU/99Pd60=", "user"=>{"name"=>"Omar Arroum", "email"=>"omar#gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up", "id"=>"user"}
Completed 401 Unauthorized in 1ms
Started GET "/users/sign_in" for 127.0.0.1 at 2013-04-18 00:48:22 +0100
Processing by Devise::SessionsController#new as HTML
Rendered devise/shared/_links.html.erb (0.4ms)
Rendered devise/sessions/new.html.erb within layouts/application (9.8ms)
Rendered layouts/_navigation.html.erb (1.3ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 63ms (Views: 61.6ms | ActiveRecord: 0.0ms)
I haven't made any changes to the `user_controller.rb, so not sure what is causing this issue
EDIT: Added the RegistersController
class RegistersController < ApplicationController
#before_filter :authenticate_user!
load_and_authorize_resource
# GET /registers
# GET /registers.json
def index
#registers = Register.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #registers }
end
end
# GET /registers/1
# GET /registers/1.json
def show
#register = Register.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #register }
end
end
# GET /registers/new
# GET /registers/new.json
def new
#register = Register.new(:event_id => params[:event_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: #register }
end
end
# GET /registers/1/edit
def edit
#register = Register.find(params[:id])
end
# POST /registers
# POST /registers.json
def create
#register = Register.new(params[:register])
respond_to do |format|
if #register.save
format.html { redirect_to #register, notice: 'Register was successfully created.' }
format.json { render json: #register, status: :created, location: #register }
else
format.html { render action: "new" }
format.json { render json: #register.errors, status: :unprocessable_entity }
end
end
end
# PUT /registers/1
# PUT /registers/1.json
def update
#register = Register.find(params[:id])
StudentRegister.find_all_by_register_id(#register).each do |streg|
streg.update_attributes(:present => false, :late => false)
#sets the users status as being absent, mainly for the purpose of when copying a new register,
#as the presence and lateness is otherwise maintained
end
respond_to do |format|
if #register.update_attributes(params[:register])
format.html { redirect_to #register, notice: 'Register was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #register.errors, status: :unprocessable_entity }
end
end
end
# DELETE /registers/1
# DELETE /registers/1.json
def destroy
#register = Register.find(params[:id])
#register.students.delete_all_on_destroy
#register.destroy
respond_to do |format|
format.html { redirect_to registers_url }
format.json { head :no_content }
end
end
def registration
#studentregister = StudentRegister.find_all_by_register_id(params[:id])
end
def copy
#register = Register.find(params[:id]).amoeba_dup #Creates a duplicate of the existing register with all Students in it
#register.date = Date.today #Sets the registers date to today's date
#register.save
respond_to do |format|
format.html { redirect_to edit_register_path(#register) } #Redirects to the edit page so that they can make any updates
format.json { head :no_content }
end
end
end
You are doing before_filter :authenticate_user! in your RegistersController (most likely). That is what produces the 401 response and that flash message with Devise. CanCan should have nothing to do with it. Post your RegistersController.
Ok figured out what the issue was.
Due to the fact that I had a file called Registration.html.erb and an action name registration my route looked like this:
match 'registers/:id/registration' => 'registers#registration', :as => 'registration' #Route for taking the register
Problem is that Devise uses this action name internally, so my code was being routed wrongly.
To solve this, I changed everything named registration to take_register, like so:
match 'registers/:id/take_register' => 'registers#take_register', :as => 'take_register' #Route for taking the register
That did the trick for me, and now all works

Heroku rails deployement issues

currently i try to upload my rails app to heroku, but i am having this issues on my CLI:
2013-03-16T14:21:28+00:00 app[web.1]: Processing by PostsController#index as HTML
2013-03-16T14:21:28+00:00 app[web.1]: Completed 500 Internal Server Error in 1ms
2013-03-16T14:21:28+00:00 app[web.1]:
2013-03-16T14:21:28+00:00 app[web.1]: NoMethodError (undefined method `accept' for nil:NilClass):
2013-03-16T14:21:28+00:00 app[web.1]: app/controllers/posts_controller.rb:5:in `index'
2013-03-16T14:21:28+00:00 app[web.1]:
2013-03-16T14:21:28+00:00 app[web.1]:
I am not very sure about what's going on here, this is my code for routes.rb and PostsController
routes.rb
Apps2::Application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
resources :posts
root :to => 'posts#index'
end
PostController
class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
#posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
# GET /posts/1
# GET /posts/1.json
def show
#post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #post }
end
end
# GET /posts/new
# GET /posts/new.json
def new
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #post }
end
end
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(params[:post])
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render json: #post, status: :created, location: #post }
else
format.html { render action: "new" }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.json
def update
#post = Post.find(params[:id])
respond_to do |format|
if #post.update_attributes(params[:post])
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
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
Thanks.
The problem seems to be related to your database gem. Heroku uses PostgreSQL (pg) as it's database engine, not sqlite. Make sure you're using pg in production.
See: Undefined method 'accept' for nil:NilClass after upgrading to Rails 3
In my Gemfile I do:
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end

Resources