I am trying to create new object and to store it into the Database but I cannot understand why I am getting the following error: Here you can see the mistake:
Started POST "/cruises" for 127.0.0.1 at 2017-09-26 16:34:59 +0100
Processing by CruisesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OkFmCPhl0pjFezWCsxp+c16wr3j9nbsdLIdgj+PsCMgSDmTzBoEJQ69tB8IA3uNayLRO+LMTi/73YqYCMImtCA==", "cruise"=>{"name"=>"NewAsasa", "ship_id"=>""}, "commit"=>"Create Cruise"}
(0.1ms) begin transaction
(0.1ms) rollback transaction
Rendering cruises/new.html.erb within layouts/application
Rendered cruises/_form.html.erb (3.6ms)
Rendered cruises/new.html.erb within layouts/application (4.4ms)
Completed 500 Internal Server Error in 12ms (ActiveRecord: 0.1ms)
ActionView::Template::Error (undefined method `map' for nil:NilClass
Did you mean? tap):
18:
19: <div class="field">
20: <em><%= f.label :ship_id %></em>
21: <%= f.collection_radio_buttons( :ship_id, #ships, :id, :name ) %>
22: </div>
23:
24: <div class="actions">
CruiseController - ""here you can see more about the controller below:I just need to write more text because it gives me error when I try to put this code, so do not read this explaination here it is for""
class CruisesController < ApplicationController
before_action :set_cruise, only: [:show, :edit, :update, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :redirect_if_not_found
# GET /cruises
# GET /cruises.json
def index
#cruises = Cruise.all
end
# GET /cruises/1
# GET /cruises/1.json
def show
end
# GET /cruises/new
def new
#cruise = Cruise.new
#ships = Ship.all
end
# GET /cruises/1/edit
def edit
#ships = Ship.all
end
# POST /cruises
# POST /cruises.json
def create
#cruise = Cruise.new(cruise_params)
respond_to do |format|
if #cruise.save
format.html { redirect_to #cruise, notice: 'Cruise was successfully created.' }
format.json { render :show, status: :created, location: #cruise }
else
format.html { render :new }
format.json { render json: #cruise.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /cruises/1
# PATCH/PUT /cruises/1.json
def update
respond_to do |format|
if #cruise.update(cruise_params)
format.html { redirect_to #cruise, notice: 'Cruise was successfully updated.' }
format.json { render :show, status: :ok, location: #cruise }
else
format.html { render :edit }
format.json { render json: #cruise.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cruises/1
# DELETE /cruises/1.json
def destroy
#cruise.destroy
respond_to do |format|
format.html { redirect_to cruises_url, notice: 'Cruise was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cruise
#cruise = Cruise.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def cruise_params
params.require(:cruise).permit(:name, :ship_id)
end
end
Whatever you are iterating over in your controller is nil. Based on the code you have shown I am assuming it is #ships. I bet if you put puts #ships in your controller it will log nil.
Your save fails in the create action, (rollback transaction) so the if condition of the create action trigger the render :new.
However, it is a render, not a redirect, that mean the new action is not executed, and both #cruise and #ship does not exist (undefined).
Finally during the rendering of the new view, an error is triggered because #ships is undefined.
EDIT : I made an error, #ships is not nil it just does not exist (undefined).
Related
I'm quite new to rails and I'm using grape to build an API rails app.
I've been getting this error message after I click on the 'Create Event' button :-
NameError at /events
undefined local variable or method `event_params' for #<EventsController:0x00000008f76400>
The error points to the controller file for events,
class EventsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user_from_token!
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
#events = Event.all
if user_signed_in?
if current_user.is_admin?
#events = Event.all
respond_to do |format|
format.html
format.json { render json: #events }
end
else
#events = current_user.events
respond_to do |format|
format.html
format.json { render json: #events }
end
end
else
render json: {}, status: :unauthorized
end
end
def new
#event = Event.new
end
def edit
end
def create
#event = current_user.events.new(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end
end
And here's the event_params method,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
The log file shows,
Started POST "/events" for 127.0.0.1 at 2015-08-10 14:38:16 +0800
Processing by EventsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yRVn4uuxpMVejs/gsG004PIi2JRaWnovGBm7TweGgkf2XpTLzjOZNGanCRVVKSPAV6JzNRWmDzRRbq/dC2KOKQ==", "event"=>{"event_name"=>"ddwq", "event_description"=>"rqreqreqr", "event_date(1i)"=>"2015", "event_date(2i)"=>"8", "event_date(3i)"=>"10", "event_time(1i)"=>"2015", "event_time(2i)"=>"8", "event_time(3i)"=>"10", "event_time(4i)"=>"06", "event_time(5i)"=>"38"}, "commit"=>"Create Event"}
[1m[36mUser Load (1.6ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["id", 1]]
Completed 500 Internal Server Error in 34ms
NameError - undefined local variable or method `event_params' for #<EventsController:0x000000082a6540>:
app/controllers/events_controller.rb:37:in `create'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
...
...
bin/rails:4:in `<main>'
Started POST "/__better_errors/beb9cf3b65f0e36a/variables" for 127.0.0.1 at 2015-08-10 14:38:16 +0800
def create
#event = current_user.events.new(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
respond_to do |format|
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end ##You have missed the end here
And here's the event_params method,
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
Try build instead of new
replace
#event = current_user.events.new(event_params)
by
#event = current_user.events.build(event_params)`
The error message indicates that, event_params method is not available for the EventsController. I think you mistakenly put the event_params inside another method or missed an end to end the create method. It should be an instance method of your EventsController class.
This code should work as it is:
class EventsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_user_from_token!
before_action :set_event, only: [:show, :edit, :update, :destroy]
def index
#events = Event.all
if user_signed_in?
if current_user.is_admin?
#events = Event.all
respond_to do |format|
format.html
format.json { render json: #events }
end
else
#events = current_user.events
respond_to do |format|
format.html
format.json { render json: #events }
end
end
else
render json: {}, status: :unauthorized
end
end
def new
#event = Event.new
end
def edit
end
def create
#event = current_user.events.new(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :show, notice: 'You are not allowed to view this.' }
format.json { render json: {}, status: :unauthorized}
end
end
end
private
def event_params
params.require(:event).permit(:event_name, :event_description, :event_date, :event_time)
end
end
So, the error was a missing 'end' for show method which I didn't post that part.
I completely missed that part because the error was misleading and I assume that it has something to do with the event parameters since the error points there.
I have a server with devise and simple-authentication-token, and a client that use activeResource to authenticate user from server by json. The authantication works fine, but when I try retrieve an entity, the activeResource give me an internal server error. What do I need make to treat this error?
codeStarted GET "/news/1.jsonuser_email=mauricio1#gmail.com&user_token=JcmL-bouqbxSzeNznEb" for ::1 at 2015-05-08 16:41:13 -0300
Processing by NewsController#show as JSON
Parameters: {"user_email"=>"mauricio1#gmail.com", "user_token"=>"JcmL-bouqbxSzeNznEb", "id"=>"1"}
Completed 500 Internal Server Error in 114ms
ActiveResource::Redirection (Failed. Response code = 302. Response message = Found . => http://localhost:3001/users/sign_in.json):
app/controllers/news_controller.rb:68:in `set_news'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.0ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (21.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.4ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (35.1ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.5ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (144.0ms)
My NewsController.rb
class NewsController < ApplicationController
before_action :set_news, only: [:show, :edit, :update, :destroy]
# GET /news
# GET /news.json
def index
#news = News.all
end
# GET /news/1
# GET /news/1.json
def show
end
# GET /news/new
def new
#news = News.new
end
# GET /news/1/edit
def edit
end
# POST /news
# POST /news.json
def create
#news = News.new(news_params)
respond_to do |format|
if #news.save
format.html { redirect_to #news, notice: 'News was successfully created.' }
format.json { render :show, status: :created, location: #news }
else
format.html { render :new }
format.json { render json: #news.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /news/1
# PATCH/PUT /news/1.json
def update
respond_to do |format|
if #news.update(news_params)
format.html { redirect_to #news, notice: 'News was successfully updated.' }
format.json { render :show, status: :ok, location: #news }
else
format.html { render :edit }
format.json { render json: #news.errors, status: :unprocessable_entity }
end
end
end
# DELETE /news/1
# DELETE /news/1.json
def destroy
#news.destroy
respond_to do |format|
format.html { redirect_to news_index_url, notice: 'News was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_news
#news = News.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def news_params
params[:news]
end
end
Problem resolved with:
def set_news
begin
#news = News.find(params[:id])
rescue ActiveResource::Redirection
respond_to do |format|
format.json { render :json => { :state => {:code => 403, :message => "Forbidden"} }, :status => :forbidden }
end
rescue ActiveResource::ResourceNotFound
respond_to do |format|
format.json { render :json => { :state => {:code => 404, :message => "Not Found"} }, :status => :not_found }
end
rescue Exception => e
respond_to do |format|
format.json { render :json => { :state => {:code => 422, :message => "Unprocessabel Entity"} }, :status => :unprocessable_entity }
end
end
end
i am getting an error i do not seem to understand. Any help would be much appreciated.
I have a list of users but wanted to display only users that have created events using the inbuilt ruby function 'group_by'. but i get the error "undefined method `group_by' for nil:NilClass"
The console:
Started GET "/users" for 127.0.0.1 at 2014-12-24 15:54:01 +0000
Processing by UsersController#index as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 40 ORDER BY "users"."id" ASC LIMIT 1
Rendered users/index.html.erb within layouts/application (1.1ms)
Completed 500 Internal Server Error in 5ms
ActionView::Template::Error (undefined method `group_by' for nil:NilClass):
10: </tr>
11:
12:
13: <% #events.group_by(&:user).each do |user, events| %>
14: <%= link_to(image_tag("profile_image_gravatar.png"), user) %>
15: <%= user.full_name %>
16: <%= link_to 'Show', user %>
app/views/users/index.html.erb:13:in `_app_views_users_index_html_erb__3564202377022035077_70335954377780'
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered /Users/ARTLoe/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (9.5ms)
events_controller.rb
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!
def index
#events = Event.order(:date)
# #events = current_user.events | displays only events by current user
end
def show
#commentable = #event
#comments = #commentable.comments
#comment = Comment.new
end
def new
#event = Event.new
end
def edit
end
def create
#event = Event.new(event_params)
#event.user = current_user
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :new }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #event.update(event_params)
format.html { redirect_to #event, notice: 'Event was successfully updated.' }
format.json { render :show, status: :ok, location: #event }
else
format.html { render :edit }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
def destroy
#event.destroy
respond_to do |format|
format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
#event = Event.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
params.require(:event).permit(:name, :description, :date, :time, :city, :price, :user_id)
end
end
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show]
before_filter :authenticate_user!
def index
#users = User.all
end
def show
#user = User.find(params[:id])
#user_events = #user.events.order(:date)
#events = Event.all
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :firstname, :lastname, :dob, :gender, :description, :role)
end
end
users/index.html.erb
<% provide(:title, 'Users') %>
<h1>Listed Event Organizers</h1>
<% #events.group_by(&:user).each do |user, events| %>
<%= link_to(image_tag("profile_image_gravatar.png"), user) %>
<%= user.full_name %>
<%= link_to 'Show', user %>
<% end %>
<h6><%= link_to 'Create Event', new_event_path %></h6>
You're not defining #events in UsersController#index.
Your action isn't defining the instance variable that you're trying to use in the view.
In your index action within the UsersController, you need to define the #events variable.
Something like in the index action of your EventsController
class class UsersController < ApplicationController
def index
#users = User.all
#events = Event.includes(:user)
end
end
I keep getting this error, I haven't used ruby in a while so I don't really know how to fix it.
param is missing or the value is empty: sch
Extracted source (around line #72):
70
71
72
73
74
# Never trust parameters from the scary internet, only allow the white list through.
def sch_params
params.require(:sch).permit(:date, :time, :user_id)
end
end
Rails.root: C:/Sites/web
Application Trace | Framework Trace | Full Trace
app/controllers/sches_controller.rb:72:in `sch_params'
app/controllers/sches_controller.rb:27:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"cHq54V7aVCjeBz0udZAYjGiojgrqDANx/BS+oMQ+N+0=",
"sche"=>{"date(1i)"=>"2014",
"date(2i)"=>"4",
"date(3i)"=>"21",
"time(1i)"=>"2014",
"time(2i)"=>"4",
"time(3i)"=>"21",
"time(4i)"=>"17",
"time(5i)"=>"37",
"user"=>"steve"},
"commit"=>"Create Sche"}
This is my controller
class SchesController < ApplicationController
before_action :set_sch, only: [:show, :edit, :update, :destroy]
# GET /sches
# GET /sches.json
def index
#sches = Sche.all
end
# GET /sches/1
# GET /sches/1.json
def show
end
# GET /sches/new
def new
#sch = Sche.new
end
# GET /sches/1/edit
def edit
end
# POST /sches
# POST /sches.json
def create
#sch = Sche.new(sch_params)
respond_to do |format|
if #sch.save
format.html { redirect_to #sch, notice: 'Sche was successfully created.' }
format.json { render :show, status: :created, location: #sch }
else
format.html { render :new }
format.json { render json: #sch.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /sches/1
# PATCH/PUT /sches/1.json
def update
respond_to do |format|
if #sch.update(sch_params)
format.html { redirect_to #sch, notice: 'Sche was successfully updated.' }
format.json { render :show, status: :ok, location: #sch }
else
format.html { render :edit }
format.json { render json: #sch.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sches/1
# DELETE /sches/1.json
def destroy
#sch.destroy
respond_to do |format|
format.html { redirect_to sches_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_sch
#sch = Sche.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def sch_params
params.require(:sch).permit(:date, :time, :user)
end
end
I don't really know whats going on, I've tried googleing the problem but I still haven't found the answer
Any help would be appreciated, thanks
Use this:
def sch_params
params.require(:sche).permit(:date, :time, :user)
end
Your controller name is SchesController so in params hash you are getting key sche and not sch.
If you look at the params generated, notice sche
"sche"=>{"date(1i)"=>"2014", "date(2i)"=>"4", "date(3i)"=>"21",
"time(1i)"=>"2014", "time(2i)"=>"4", "time(3i)"=>"21",
"time(4i)"=>"17", "time(5i)"=>"37", "user"=>"steve"}
So I'm getting this error in one of my views and it seems to have to do with the way client is being initialized in the controller, here's the code for my Client model and controller:
Model:
class Client < ActiveRecord::Base
has_many :visits
has_many :exchanges, through: :visits
Controller:
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
def index
#clients = Client.all
end
# GET /clients/new
def new
#client = Client.new
end
# POST /clients
# POST /clients.json
def create
#client = Client.new(client_params)
respond_to do |format|
if #client.save
format.html { redirect_to #client, notice: 'Client was successfully created.' }
format.json { render action: 'show', status: :created, location: #client }
else
format.html { render action: 'new' }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /clients/1
# PATCH/PUT /clients/1.json
def update
respond_to do |format|
if #client.update(client_params)
format.html { redirect_to #client, notice: 'Client was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #client.errors, status: :unprocessable_entity }
end
end
end
# DELETE /clients/1
# DELETE /clients/1.json
def destroy
#client.destroy
respond_to do |format|
format.html { redirect_to clients_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client
#client = Client.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_params
#hidden for security
end
end
So the error goes off at line 1 for my client form when it calls #client and says "undefined method `model_name' for ActiveRecord::Relation::ActiveRecord_Relation_Client:Class" Really could use some help as I have no idea what is going on, thanks.
Error Stack:
Started GET "/clients/new" for 127.0.0.1 at 2014-04-06 14:05:48 -0400
Processing by ClientsController#new as HTML
Rendered clients/_form.html.erb (62.2ms)
Rendered clients/new.html.erb within layouts/application (65.3ms)
Completed 500 Internal Server Error in 68ms
ActionView::Template::Error (undefined method `model_name' for ActiveRecord::Relation::ActiveRecord_Relation_Client:Class):
1: <%= simple_form_for(#client) do |f| %>
2: <% if #client.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(#client.errors.count, "error") %> prohibited this client from being saved:</h2>
app/views/clients/_form.html.erb:1:in `_app_views_clients__form_html_erb___2200324505640086965_70348410359440'
app/views/clients/new.html.erb:3:in `_app_views_clients_new_html_erb___3059029162300151807_70348376263300'
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (12.8ms)