I have programmed an android app, which can take profile pictures from a user. Now I want to upload these profile picture to my Ruby on Rail server. However the upload doesn't work. I receive the Error message:
app/controllers/items_controller.rb:55:in `item_params'
app/controllers/items_controller.rb:21:in `create'
Started POST "/items" for 192.168.3.7 at 2016-09-11 01:12:21 +0900
Processing by ItemsController#create as HTML
Parameters: {"image"=>#<ActionDispatch::Http::UploadedFile:0x5737110 #tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-2
8624-1vjbftr.jpg>, #original_filename="IMG_20160911_010525.jpg", #content_type="application/octet-stream", #headers="Content-Disposition: form-data; na
me=\"image\"; filename=\"IMG_20160911_010525.jpg\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n">}
Completed 400 Bad Request in 0ms (ActiveRecord: 0.0ms)
ActionController::ParameterMissing (param is missing or the value is empty: item):
app/controllers/items_controller.rb:55:in `item_params'
app/controllers/items_controller.rb:21:in `create'
Why does this not work? How should my item_params be defined? Here is my items_controller.rb
class ItemsController < ApplicationController
before_action :set_item, only: [:show, :edit, :update, :destroy]
# GET /items
# GET /items.json
def index
#items = Item.all
end
# GET /items/1
# GET /items/1.json
def show
send_data(item.file_contents,
type: #item.content_type,
filename: #item.filename)
end
# POST /items
# POST /items.json
def create
#item = Item.new(item_params)
if #item.save
render :show, status: :created, location: #item
else
render json: #item.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
if #item.update(item_params)
render :show, status: :ok, location: #item
else
render json: #item.errors, status: :unprocessable_entity
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
#item.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
#item = Item.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
#######################params.require(:item).permit(:name, :description, :picture)
params.permit(:picture)
end
end
UPDATE: I renamed my image from android app and called it "item". Now the parameter error disappears. However a new error arises:
app/controllers/items_controller.rb:24:in `create'
Started POST "/items" for 192.168.3.7 at 2016-09-11 10:25:26 +0900
Processing by ItemsController#create as HTML
Parameters: {"item"=>#<ActionDispatch::Http::UploadedFile:0x53b1d30 #tempfile=#<Tempfile:C:/Users/Clemens/AppData/Local/Temp/RackMultipart20160911-3144-anlpp6.jpg>, #original_filename="IMG_20160911_100920.jpg", #co
ntent_type="application/octet-stream", #headers="Content-Disposition: form-data; name=\"item\"; filename=\"IMG_20160911_100920.jpg\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n"
>}
Unpermitted parameter: item
(0.0ms) BEGIN
SQL (31.2ms) INSERT INTO `items` (`created_at`, `updated_at`) VALUES ('2016-09-11 01:25:26', '2016-09-11 01:25:26')
(0.0ms) COMMIT
Completed 500 Internal Server Error in 62ms (ActiveRecord: 31.2ms)
ActionView::MissingTemplate (Missing template items/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "C:/Benutzer/Clemens/RubymineProjects/rails-api-fileupload-tutorial-carrierwave-single/app/views"
):
app/controllers/items_controller.rb:24:in `create'
Any idea why I get this error? I put in views following line both in views\application\show.json.jbuilder and in views\items\show.json.jbuilder:
json.extract! #item, :locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]
However I still get the same error.
What is happening is that this line:
params.require(:item).permit(:name, :description, :picture)
will raise an exception if you do not have an item in your params. When you are sending to your create action, apparently you do not have an item. I am also assuming that when you saw this error you didn't have that line commented.
Related
At the /tags page I have a link with remote: true. It should be a link to an ajax request. But there are two requests, as JS and as HTML.
<%= link_to 'New', new_tag_path, class: "btn btn-outline btn-primary", remote: true %>
INFO -- : Started GET "/tags/new" for 192.168.18.236 at 2018-06-13 11:44:18 -0300
INFO -- : Processing by TagsController#new as JS
INFO -- : Rendered tags/_form.html.erb (41.0ms)
INFO -- : Rendered tags/_modal.html.erb (41.5ms)
INFO -- : Rendered tags/new.js.erb (49.2ms)
INFO -- : Completed 200 OK in 63ms (Views: 50.3ms | ActiveRecord: 2.2ms)
INFO -- : Started GET "/tags/new" for 192.168.18.236 at 2018-06-13 11:44:18 -0300
INFO -- : Processing by TagsController#new as HTML
INFO -- : Completed 500 Internal Server Error in 14ms (ActiveRecord: 1.9ms)
FATAL -- :
ActionView::MissingTemplate (Missing template tags/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
If I provide a new.html.erb, this MissingTemplate error is over, but the page is redirected to new.html.
What could be wrong with that request or that link?
Edit The controller code
class TagsController < ApplicationController
before_action :set_tag, only: [:show, :edit, :update, :destroy, :fix_correct_name]
before_action :set_tags, only: [:new, :edit]
# GET /tags/new
def new
#tag = Tag.new
end
# GET /tags/1/edit
def edit
end
# POST /tags
def create
#tag = Tag.new(tag_params)
respond_to do |format|
if #tag.save
format.html { redirect_to action: "index", notice: 'Tag adicionada.' }
else
format.html { render :new }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tag
#tag = Tag.find(params[:id])
end
def set_tags
#tags = Tag.all.pluck(:name, :id)
end
# Never trust parameters from the scary internet, only allow the white list through.
def tag_params
params.fetch(:tag, {}).permit(:name, :parent_id, :tag_name, :parent_name, :no_parent)
end
end
In your new action, you’ll need a response_to to handle the ajax call
def new
#tag = Tag.new
respond_to { |format| format.js }
end
Also, you’ll need a new.js.erb file and a _new.html.erb partial to handle the response and update the view.
Inside your new.js.erb, you will have to render the view with something like this
$(“div-id-name”).html(“<%= j render partial: “new” %>”)
And your new partial will simply hold your form (or whatever is it you wanna render)
I made it work now, only replacing require jquery_ujs by require rails-ujs to application.js and adding rails-ujs gem.
I added a route to the default restful routes in my app,so in my controller I have an action called status, it's verb is Patch, but I get the above error each time I try to update an attribute via the status action, Note: the default update action in the Restful route has no issues.
Routes.rb
Rails.application.routes.draw do
devise_for :users
resources :projects do
resources :gigs, shallow: true do
patch :status
end
end
end
Gigs Controller
class GigsController < ApplicationController
before_action :set_gig, only: [:show, :edit, :update, :destroy,:status]
def status
respond_to do |format|
if #gig.update(:done, true)
format.html { redirect_to #gig, notice: 'Gig was successfully updated.' }
format.json { render :show, status: :ok, location: #gig }
else
format.html { render :edit }
format.json { render json: #gig.errors, status: :unprocessable_entity }
end
end
end
private
def set_gig
#gig = Gig.find(params[:id])
end
def gig_params
params.require(:gig).permit(:name, :description, :done,:timeline)
end
end
and here is the link I added to the index.html.erb to execute the update
<td><%= link_to 'Mark as Done', gig_status_path(gig), method: :patch %></td>
Here is the log
Started PATCH "/gigs/2/status" for 127.0.0.1 at 2018-04-07 12:26:20 +0100
Processing by GigsController#status as HTML
Parameters: {"authenticity_token"=>"iNYFcsiwtNnCAC9goTGtfrqHFnBKufpgQ+61/pwZLVV6Nw82MBTART5ozVQUVsk74UFiWDknWrtoqYLN9D/2YQ==", "gig_id"=>"2"}
[1m[36mGig Load (0.0ms)[0m [1m[34mSELECT "gigs".* FROM "gigs" WHERE "gigs"."id" = $1 LIMIT $2[0m [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 0ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound - Couldn't find Gig with 'id'=:
app/controllers/gigs_controller.rb:93:in `set_gig'
Started POST "/__better_errors/df8ad027b952f89c/variables" for 127.0.0.1 at 2018-04-07 12:26:20 +0100
You can check URI Pattern by using command rake routes for status action.
Prefix Verb URI Pattern Controller#Action
gig_status PATCH /gigs/:gig_id/status(.:format) gigs#status
project_gigs GET /projects/:project_id/gigs(.:format) gigs#index
POST /projects/:project_id/gigs(.:format) gigs#create
new_project_gig GET /projects/:project_id/gigs/new(.:format) gigs#new
edit_gig GET /gigs/:id/edit(.:format) gigs#edit
gig GET /gigs/:id(.:format) gigs#show
PATCH /gigs/:id(.:format) gigs#update
PUT /gigs/:id(.:format) gigs#update
so you need to use gig_id in case of status action
Modify method set_gig as follows:
def set_gig
#gig = Gig.find(params[:gig_id])
end
I'm having trouble getting Best In Place to update. This is the first time I've used this gem or made a Rails app so I'm sure I'm just missing something simple even though I've scoured the documentation and searched through other questions here.
This is the error I get in the console:
Started PUT "/tasks/1" for 127.0.0.1 at 2016-04-22 11:52:10 -0600
Processing by TasksController#update as JSON
Parameters: {"authenticity_token"=>"cAwRbx8JeYDMG1LrR7nl0VQfwW/x00RUd8rsTP8Iwc
0=", "tasks"=>{"title"=>"Task 1sadsads"}, "id"=>"1"}
Task Load (0.0ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? ORD
ER BY priority ASC LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? ORDER B
Y priority ASC LIMIT 1 [["id", "1"]]
Completed 400 Bad Request in 3ms
ActionController::ParameterMissing (param is missing or the value is empty: task
):
app/controllers/tasks_controller.rb:99:in `task_params'
app/controllers/tasks_controller.rb:62:in `update'
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb
(0.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.1.8
/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (36.0ms)
My controller:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
#tasks = Task.all
respond_to do |type|
type.html
type.json {render :json => #task}
end
end
# GET /tasks/1
# GET /tasks/1.json
def show
#task = Task.find params[:id]
respond_to do |format|
format.html
format.json {render :json => #task}
end
end
# GET /tasks/new
def new
#task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
#task = Task.new(task_params)
respond_to do |format|
if #task.save
format.html { redirect_to #task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: #task }
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def sort
params[:order].each do |key,value|
Task.find(value[:id]).update_attribute(:priority,value[:position])
end
render :nothing => true
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
#task = Task.find params[:id]
if #task.update(task_params)
respond_to do |format|
format.html { redirect_to( #task )}
format.json { render :json => #task }
end
else
respond_to do |format|
format.html { render :action => :edit } # edit.html.erb
format.json { render :nothing => true }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
#task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:title, :description, :priority)
end
end
My view partial:
<div class="panel panel-default" data-id="<%= task.id %>">
<div class="panel-heading">
<h3 class="panel-title"><span class="rest-in-place" data-url="/tasks/<%= task.id %>" data-object="tasks" data-attribute="title" data-placeholder="Enter a title">
<%= task.title %>
</span></h3>
</div>
<div class="panel-body">
<%= truncate task.description, length: 50 %>
</div>
</div>
I really appreciate any help I can get. I'm 99% sure it's something I configured wrong in the controller, I just can't for the life of me figure out what it is.
EDIT: The background for this is that I have a bunch of 'Tasks' and I want to list them all on the index and be able to update any task on the index just by clicking on the title.
ActionController::ParameterMissing (param is missing or the value is empty: task
):
app/controllers/tasks_controller.rb:99:in `task_params'
app/controllers/tasks_controller.rb:62:in `update'
This is telling you that there is no parameter with the name of :task. You can see your parameters above this message.
Parameters: {"authenticity_token"=>"cAwRbx8JeYDMG1LrR7nl0VQfwW/x00RUd8rsTP8Iwc
0=", "tasks"=>{"title"=>"Task 1sadsads"}, "id"=>"1"}
You have tasks in your params here. This information is coming from your view.
data-object="tasks"
Try changing this piece in your view to task and it should send the request correctly.
Background
I'm making a Rails application which has two namespaces, which are admin (for administrator purpose) and api for mobile & web client.
A weird thing happened yesterday. I made a new table facilities which consists of id, name, created_at, updated_at in my PostgreSQL database.
Problem
I tried to get all facilities using admin namespace http://localhost:3000/admin/facilities and it works well (return the HTML and the list of facilities).
Started GET "/admin/facilities" for ::1 at 2015-05-10 16:12:47 +0800
Processing by Admin::FacilitiesController#index as HTML
Facility Load (0.4ms) SELECT "facilities".* FROM "facilities"
Rendered admin/facilities/index.html.erb within layouts/application (2.5ms)
Completed 200 OK in 91ms (Views: 90.4ms | ActiveRecord: 0.4ms)
But when I call http://localhost:3000/api/v1/facilities from browser, which supposed to return JSON. I got an error
NoMethodError in Api::V1::FacilitiesController#index
undefined method `new' for nil:NilClass
Extracted source (around line #8):
6 def index
7 #facilities = Facility.all
8 render json: #facilities
9 end
10
11
The error from console
Started GET "/api/v1/facilities" for ::1 at 2015-05-10 16:38:26 +0800
Processing by Api::V1::FacilitiesController#index as HTML
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL ORDER BY "users"."id" ASC LIMIT 1
Facility Load (0.5ms) SELECT "facilities".* FROM "facilities"
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.1ms)
NoMethodError (undefined method `new' for nil:NilClass):
app/controllers/api/v1/facilities_controller.rb:8:in `index'
Rendered /Users/abrahamks/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.1ms)
Rendered /Users/abrahamks/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.7ms)
. . . .
. . . .
and so on
I check from rails c and call Facility.all and looks like the command line return correct values, but I don't understand why it can't render / return json.
Loading development environment (Rails 4.2.1)
2.2.1 :001 > Facility.all
Facility Load (0.5ms) SELECT "facilities".* FROM "facilities"
=> #<ActiveRecord::Relation [#<Facility id: 1, name: "Kitchen", created_at: "2015-05-09 10:54:00", updated_at: "2015-05-09 16:08:48">, #<Facility id: 2, name: "Washer", created_at: "2015-05-09 11:20:40", updated_at: "2015-05-09 16:09:32">, #<Facility id: 3, name: "Swimming Pool", created_at: "2015-05-09 11:22:19", updated_at: "2015-05-09 16:09:41">, #<Facility id: 4, name: "Internet", created_at: "2015-05-09 11:24:02", updated_at: "2015-05-09 16:12:31">, #<Facility id: 5, name: "Dryer", created_at: "2015-05-09 15:55:36", updated_at: "2015-05-09 16:12:54">]>
What's wrong with my code? Is there any configuration that I missed?
Thank you.
More Details if Needed
# GET /facilities/1
facilities has many-to-many relationship with properties, I generate a join table
class CreateJoinTablePropertiesFacilities < ActiveRecord::Migration
def change
create_join_table :properties, :facilities do |t|
t.index [:property_id, :facility_id]
t.index [:facility_id, :property_id]
end
end
end
Here is my facility.rb
class Facility < ActiveRecord::Base
has_and_belongs_to_many :property, join_table: :facilites_properties
end
Here is my routes.rb
Rails.application.routes.draw do
namespace :admin do
resources :facilities
end
namespace :api do
namespace :v1 do
resources :properties
resources :facilities, only: [:index, :show]
resources :users do
member do
post 'list', :to => "users#list_property"
end
end
Here is my app/controllers/api/v1/facilities_controller
class Api::V1::FacilitiesController < ApplicationController
before_action :set_facility, only: [:show, :edit, :update, :destroy]
# before_action :authenticate
# GET /facilities
# GET /facilities.json
def index
#facilities = Facility.all
render json: #facilities
end
# GET /facilities/1
# GET /facilities/1.json
def show
render json: #facility
end
private
# Use callbacks to share common setup or constraints between actions.
def set_facility
#facility = Facility.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def facility_params
params.require(:facility).permit(:name)
end
end
And this is app/controllers/admin/facilities_controller
class Admin::FacilitiesController < ApplicationController
before_action :set_facility, only: [:show, :edit, :update, :destroy]
# before_action :authenticate
# GET /facilities
# GET /facilities.json
def index
#facilities = Facility.all
end
# GET /facilities/1
# GET /facilities/1.json
def show
end
# GET /facilities/new
def new
#facility = Facility.new
end
# GET /facilities/1/edit
def edit
end
# POST /facilities
# POST /facilities.json
def create
#facility = Facility.new(facility_params)
respond_to do |format|
if #facility.save
format.html { redirect_to admin_facilities_path, notice: 'Facility was successfully created.' }
format.json { render :show, status: :created, location: #facility }
else
format.html { render :new }
format.json { render json: #facility.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /facilities/1
# PATCH/PUT /facilities/1.json
def update
respond_to do |format|
if #facility.update(facility_params)
format.html { redirect_to admin_facilities_path, notice: 'Facility was successfully updated.' }
format.json { render :show, status: :ok, location: #facility }
else
format.html { render :edit }
format.json { render json: #facility.errors, status: :unprocessable_entity }
end
end
end
# DELETE /facilities/1
# DELETE /facilities/1.json
def destroy
#facility.destroy
respond_to do |format|
format.html { redirect_to facilities_url, notice: 'Facility was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_facility
#facility = Facility.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def facility_params
params.require(:facility).permit(:name)
end
end
The stack trace is showing the request was processed as HTML.
Processing by Admin::FacilitiesController#index as HTML
As you are trying to render JSON, you can either specify the controller to respond to json
module Api
module V1
class FacilitiesController < ApplicationController
respond_to :json
end
end
end
Or you can specify json as a default response within your routes.rb file
namespace :api, defaults: { format: 'json' } do
# your api json routes...
end
Hope either one of these help.
I just realised I forgot to generate the serializer. Since I use gem Active Model Serializers, this is what I did to generate the serializer.
rails g serializer facility
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