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.
Related
my rails version is 5.0.5, i am currently developing an online store. i am following the steps on Agile web development (rails 5). i have followed the steps accordingly, i can create new products, edit products,and delete products, i am stuck at the stage where i am to add a product to cart(the "Add to cart" button shows) when i click "Add to cart" it gives me an error{cant find product with id}..........this is the error guys.
ruby 2.4.1p111 (2017-03-22 revision 58053) [i386-mingw32]
C:\Users\COMPUTER>cd desktop
C:\Users\COMPUTER\Desktop>cd trial
C:\Users\COMPUTER\Desktop\trial>cd depot
C:\Users\COMPUTER\Desktop\trial\depot>rails s
=> Booting Puma
=> Rails 5.0.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.1-p111), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started GET "/" for 127.0.0.1 at 2017-10-22 17:44:40 +0100
ActiveRecord::SchemaMigration Load (0.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by StoreController#index as HTML
Rendering store/index.html.erb within layouts/application
Product Load (4.0ms) SELECT "products".* FROM "products" ORDER BY "products"."title" ASC
Rendered store/index.html.erb within layouts/application (620.0ms)
Completed 200 OK in 1888ms (Views: 1385.1ms | ActiveRecord: 20.0ms)
Started POST "/line_items" for 127.0.0.1 at 2017-10-22 17:46:14 +0100
Processing by LineItemsController#create as HTML
Parameters: {"authenticity_token"=>"tysgjA3w3dfMA1ACBRWeigDDnDM9WDiwBDKotUwmXVVxVy0+msnR4gmkB3QV8qU8dKdLdnb5yEOS5FmdUs7LyQ=="}
Cart Load (4.0ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
(4.0ms) begin transaction
SQL (48.0ms) INSERT INTO "carts" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2017-10-22 16:46:15.531901"], ["updated_at", "2017-10-22 16:46:15.531901"]]
(108.0ms) commit transaction
Product Load (4.0ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 644ms (ActiveRecord: 180.0ms)
ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=):
app/controllers/line_items_controller.rb:29:in `create'
Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (52.0ms)
Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (24.0ms)
Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (16.0ms)
Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (8928.0ms)
also below is the store(the default homepage that shows the products) view file.....
<p id="notice"><%= notice %></p>
<h1>Your Pragmatic Catalog</h1>
<% cache #products do %>
<% #products.each do |product| %>
<% cache #product do %>
<div class="entry">
<h3><%= product.title %></h3>
<%= image_tag(product.image_url) %>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency (product.price) %></span>
<%= button_to 'Add to Cart' , line_items_path %>
</div>
</div>
<% end %>
<% end %>
<% end %>
als below is the routes......
Rails.application.routes.draw do
resources :line_items
resources :carts
root 'store#index', as: 'store_index'
resources :products
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
and below is the products model ......
class Product < ApplicationRecord
validates :title, :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
#...
private
# ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
unless line_items.empty?
errors.add(:base, 'Line Items present')
throw :abort
end
end
end
below is the carts model....
class Cart < ApplicationRecord
has_many :line_items, dependent: :destroy
end
def add_product (lineitem, product_id)
current_item = line_items.find_by(product_id: product.id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product.id)
end
current_item
end
line items model....
class LineItem < ApplicationRecord
belongs_to :product
belongs_to :cart
end
application_record.rb........
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
carts controller....
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :edit, :update, :destroy]
# GET /carts
# GET /carts.json
def index
#carts = Cart.all
end
# GET /carts/1
# GET /carts/1.json
def show
end
# GET /carts/new
def new
#cart = Cart.new
end
# GET /carts/1/edit
def edit
end
# POST /carts
# POST /carts.json
def create
#cart = Cart.new(cart_params)
respond_to do |format|
if #cart.save
format.html { redirect_to #cart, notice: 'Cart was successfully created.' }
format.json { render :show, status: :created, location: #cart }
else
format.html { render :new }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /carts/1
# PATCH/PUT /carts/1.json
def update
respond_to do |format|
if #cart.update(cart_params)
format.html { redirect_to #cart, notice: 'Cart was successfully updated.' }
format.json { render :show, status: :ok, location: #cart }
else
format.html { render :edit }
format.json { render json: #cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
#cart.destroy
respond_to do |format|
format.html { redirect_to carts_url, notice: 'Cart was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cart
#cart = Cart.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cart_params
params.fetch(:cart, {})
end
end
application controller.....
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
line items controller....
class LineItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_line_item, only: [:show, :edit, :update, :destroy]
# GET /line_items
# GET /line_items.json
def index
#line_items = LineItem.all
end
# GET /line_items/1
# GET /line_items/1.json
def show
end
# GET /line_items/new
def new
#line_item = LineItem.new
end
# GET /line_items/1/edit
def edit
end
# POST /line_items
# POST /line_items.json
def create
product = Product.find (params[:product_id])
#line_item = #cart.line_items.build(product: product)
respond_to do |format|
if #line_item.save
format.html { redirect_to #line_item.cart, notice: 'Line item was successfully created.' }
format.json { render :show, status: :created, location: #line_item }
else
format.html { render :new }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /line_items/1
# PATCH/PUT /line_items/1.json
def update
respond_to do |format|
if #line_item.update(line_item_params)
format.html { redirect_to #line_item, notice: 'Line item was successfully updated.' }
format.json { render :show, status: :ok, location: #line_item }
else
format.html { render :edit }
format.json { render json: #line_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
#line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line_item
#line_item = LineItem.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_item_params
params.require(:line_item).permit(:product_id, :cart_id)
end
end
products controller...
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
#products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
#product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :description, :image_url, :price)
end
end
store controller.....
class StoreController < ApplicationController
def index
#products = Product.order(:title)
end
end
Your error is ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=):. This means that the controller doesn't know which product you want to add to the cart. This would be done by adding a product_id to the params of your POST.
In your view:
<%= button_to 'Add to Cart' , line_items_path %>
The line_items_path probably isn't correct. You probably want line_item_path(product_id: product.id). This will add the product's ID to the request and make it available when the controller tries to find the record here:
product = Product.find (params[:product_id])
In the view you have to send the correct product_id as suggested by Daniel Westendorf
<%= button_to 'Add to Cart' , line_items_path(product_id: product.id) %>
So, in this way when you create a new LineItem in new method of LinesItemController, you have it params
def new
#line_item = LineItem.new(params[:product_id])
end
For using the debug mode you can use byebug and you can use this step_by_step installation guide.
pry is another alternative.
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)
I don't understand why I'm geting these Unpermitted parameter: format messages, I'm doing a JSON request: POST "/questions/add_options.json" with these parameters Parameters: {"id_question"=>551, "options"=>[{"position"=>10, "label"=>"opc 10", "value"=>"opc 10", "go_page"=>nil}], "question"=>{}} and this is what I get in the terminal...
Started POST "/questions/add_options.json" for 127.0.0.1 at 2016-08-16 23:12:27 -0300
Processing by QuestionsController#add_options as JSON
Parameters: {"id_question"=>551, "options"=>[{"position"=>10, "label"=>"opc 10", "value"=>"opc 10", "go_page"=>nil}], "question"=>{}}
User Load (0.4ms) SELECT "login_aexa".* FROM "login_aexa" WHERE "login_aexa"."usuaex_id" = $1 ORDER BY "login_aexa"."usuaex_id" ASC LIMIT 1 [["usuaex_id", 1]]
Unpermitted parameter: format
Question Load (0.4ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT 1 [["id", 551]]
Unpermitted parameter: format
(0.2ms) BEGIN
(0.4ms) SELECT COUNT(*) FROM "options" WHERE "options"."question_id" = $1 [["question_id", 551]]
In the Rails controller I use params permit to reject parameters that are not allowed, like this:
def question_add_options_params
params.permit(:id_question, options: [:position, :label, :value, :go_page], question: {})
end
In my opinion the format should be fine, anyone know why I'm getting those Unpermitted parameter: format messages?
EDIT:
Here's the code of the controller
class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /questions
# GET /questions.json
def index
#questions = Question.all
end
# GET /questions/1
# GET /questions/1.json
def show
end
# GET /questions/new
def new
#question = Question.new
end
# GET /questions/1/edit
def edit
end
# POST /questions
# POST /questions.json
def create
#question = Question.new(question_params)
respond_to do |format|
if #question.save
format.html { redirect_to #question, notice: 'Question was successfully created.' }
format.json { render :show, status: :created, location: #question }
else
format.html { render :new }
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /questions/1
# PATCH/PUT /questions/1.json
def update
respond_to do |format|
if #question.update(question_params)
format.html { redirect_to #question, notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: #question }
else
format.html { render :edit }
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
def add_options
#question = Question.find(question_add_options_params[:id_question])
question_add_options_params[:options].each do|q_aop|
#question.options.create(q_aop)
end
#options = #question.options
end
# DELETE /questions/1
# DELETE /questions/1.json
def destroy
#question.destroy
respond_to do |format|
format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_question
#question = Question.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_params
params[:question]
end
def question_add_options_params
params.permit(:id_question, options: [:position, :label, :value, :go_page])
end
end
params.permit(:id_question, options: [:position, :label, :value, :go_page], question: {})
This line is telling Rails that the only params that are permitted are in the list above.
If you actually look at a real params-hash, it doesn't just contain the params passed in by the form, it also contains things like: :controller => :questions, :action => :create, :format => :json etc... which Rails always inserts based on the URL
Normally we namespace the form by using eg form_for #question which means the params come in like this:
{:controller => :questions, :action => :create,
:format => :json,
:question => {"id_question"=>551, "options"=>[{"position"=>10, "label"=>"opc 10", "value"=>"opc 10", "go_page"=>nil}]}
}
then you can do this in your controller:
params.require(:question).permit(:id_question, options: [:position, :label, :value, :go_page])
which doesn't literally tell rails that you aren't allowed to have the controller/action/format params that are always passed in by rails...
Obviously you'll need to modify the names of these to suit your needs, but this is what you need to do to stop the error.
I was having a similar issue with my JSON and thought this question could use a few more examples.
TL;DR
To avoid the Unpermitted parameter: format with JSON requests be sure to nest your request object for POST and PATCH requests (ex. { question: {name: '', prompt: ''} } and access with params.require(:question).permit(:name, :prompt, ..) in the controller. For GET and DELETE requests, use only params.require(:id) in the controller.
To build on Taryn's reply above, I needed to create a nested object in my front end for POST requests, and fix how I was using require and permit on the back end. Here are examples:
Example: POST /questions
Rails expects a post request to be of the form, { question: { name: '', prompt: '', ..} }. In the front end:
// bad
$http.post('/questions.json', { name: 'Question 1', prompt: 'Bears eat beets?' })
// good
$http.post('/questions.json', { question: { name: 'Question 1', prompt: '...' } })
Backend:
# app/controllers/questions_controller.rb
def question_params
# bad - logs 'Unpermitted parameter: format'
params.permit(:name, :prompt)
# good
params.require(:question).permit(:name, :prompt)
end
Example: GET /questions/:id
My mistake was in the backend again. For example:
# app/controllers/questions_controller.rb
def show
# bad - logs 'Unpermitted parameter: format'
question = Question.where(params.permit(:id)).first
# good
question = Question.find(params.require(:id))
render json: question
end
<%= link_to "Questinons", questions_path(format: "json") %>
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
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