NoMethodError in Clients#new - ruby-on-rails

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)

Related

Rails | Undefined method `constantize' for nil:NilClass | Access Method in other controller

Hey Stackoverflow community (first time asking for help, searched a lot, found nothing specific for my problem, links appreciated).
I have the following structure:
Employees and Comments.
The Employee has this in its model:
has_many :comments, as: :commentable, dependent: :destroy
The Comment has this in its model:
belongs_to :commentable, polymorphic: true
I have a "show.html" view in the "Employee MVC". In this view I want to show an input textfield to add a comment to an employee. This is not working.
If I click submit, I get an undefined method `constantize' for nil:NilClass Error. (see image)
(On a seperate "new.html" in the "Comment MVC", I can create comments perfectly fine and add them to an employee).
Code:
show.html.slim for the Employee
h1
| Mitarbeiter
i<> = #employee.name
p Was für ein wundervoller Name!
hr
h2 Kommentare:
- #employee.comments.each do |comms|
p => comms.text
hr
**
= simple_form_for Comment.new do |f|
= f.input :text, label: "Neuer Kommentar: "
= f.hidden_field :commentable_id, value: #employee.id
= f.hidden_field :commentable_type, value: #employee.class
= f.submit class: 'btn btn-primary'**
a.btn.btn-success href=edit_employee_path(#employee)
| Bearbeiten
p
a.btn.btn-warning href=employees_path Zurück
class CommentsController < ApplicationController
before_action :set_comment, only: %i[ show edit update destroy ]
# GET /comments or /comments.json
def index
#comments = Comment.all
end
# GET /comments/1 or /comments/1.json
def show
end
# GET /comments/new
def new
klaz_name = params[:commentable_type]
thing_id = params[:commentable_id]
#thing = klaz_name.constantize.find(thing_id)
#comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments or /comments.json
def create
klaz_name = params[:commentable_type]
thing_id = params[:commentable_id]
#thing = klaz_name.constantize.find(thing_id)
#comment = Comment.new(comment_params.merge(commentable: #thing))
respond_to do |format|
if #comment.save
format.html { redirect_to comment_url(#comment), notice: "Comment was successfully created." }
format.json { render :show, status: :created, location: #comment }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1 or /comments/1.json
def update
respond_to do |format|
if #comment.update(comment_params)
format.html { redirect_to comment_url(#comment), notice: "Comment was successfully updated." }
format.json { render :show, status: :ok, location: #comment }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1 or /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: "Comment was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Only allow a list of trusted parameters through.
def comment_params
params.require(:comment).permit(:text)
end
end
I tried to do it with a link_to and remote: true, but this didnt help either :/

Create new item and place it into the Database

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).

Routing with parameters not working on rails

I've been trying to figure this one out but I'm becoming desperate because I don't see why this isn't working.
Whatever I try, no route is matching my link and I get the following error:
Routing Error: uninitialized constant LineItemsController
My link looks like this:
<%= button_to 'Add to template', line_items_path(template_id: #template, position_id: position) %>
So the link being created is:
http://localhost:3000/line_items?position_id=2&template_id=1
routes.rb:
Rails.application.routes.draw do
resources :line_items
resources :templates
resources :positions
line_item_controller.rb
class LineItemsController < ApplicationController
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
position = Position.find(params[:position_id])
template = Template.find(params[:template_id])
#line_item = LineItem.new(position, template)
respond_to do |format|
if #line_item.save
format.html { redirect_to template_url}
format.js {#current_item = #line_item}
format.json { render action: 'show',
status: :created, location: #line_item }
else
format.html { render action: '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 { head :no_content }
else
format.html { render action: '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 }
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(:position_id, :template_id)
# end
#...
end
From my understanding, my link should send a POST request that should call the create action of the line_item controller, thereby matching the route POST /line_items(.:format) line_items#create
Thanks for the help guys!
I think the issue is the filename of your controller:
line_item_controller.rb should be line_items_controller.rb

Rails: deep nested resources gives error: undefined method 'comments'

I'm getting this error undefined method 'comments' in my rails application. I know I shouldn't do a >level 1 nested resources, but I don't know how to apply the correct way in this case.
Currently this is my routes:
resources :performance_indicators do
resources :improvement_actions do
member do
put "like" => "improvement_actions#upvote"
put "unlike" => "improvement_actions#downvote"
end
resources :comments
end
end
As I said I'm getting this error:
NoMethodError in PerformanceIndicators#show
Showing .../app/views/comments/_form.html.erb where line #1 raised:
undefined method `comments' for nil:NilClass
I don't know if my problem is in the controller. Anyone can help? :)
EDIT:
My comment/_form:
<%= form_for([#performance_indicator, #improvement_action, #improvement_action.comments.build]) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is my CommentsController:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :set_improvement_action
# GET /comments
# GET /comments.json
def index
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
#comment = #improvement_action.comments.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
#comment = #improvement_action.comments.new(comment_params)
if #comment.save
format.html { redirect_to [#improvement_action.performance_indicator, #improvement_action], notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [#improvement_action, #comment] }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if #comment.update(comment_params)
format.html { redirect_to #comment, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: #comment }
else
format.html { render :edit }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:body)
end
def set_improvement_action
#improvement_action = ImprovementAction.includes(:comments).find(params[:improvement_action_id])
end
end
Here Is my PerformanceIndicatorController:
class PerformanceIndicatorsController < ApplicationController
before_action :set_performance_indicator, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /performance_indicators
# GET /performance_indicators.json
def index
#performance_indicators = PerformanceIndicator.all
end
# GET /performance_indicators/1
# GET /performance_indicators/1.json
def show
##performance_indicators = PerformanceIndicator.all.order("created_at DESC")
end
# GET /performance_indicators/new
def new
#performance_indicator = PerformanceIndicator.new
#comments = Comment.new
end
# GET /performance_indicators/1/edit
def edit
end
# POST /performance_indicators
# POST /performance_indicators.json
def create
#performance_indicator = PerformanceIndicator.new(performance_indicator_params)
respond_to do |format|
if #performance_indicator.save
format.html { redirect_to #performance_indicator, notice: 'Performance indicator was successfully created.' }
format.json { render :show, status: :created, location: #performance_indicator }
else
format.html { render :new }
format.json { render json: #performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /performance_indicators/1
# PATCH/PUT /performance_indicators/1.json
def update
respond_to do |format|
if #performance_indicator.update(performance_indicator_params)
format.html { redirect_to #performance_indicator, notice: 'Performance indicator was successfully updated.' }
format.json { render :show, status: :ok, location: #performance_indicator }
else
format.html { render :edit }
format.json { render json: #performance_indicator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /performance_indicators/1
# DELETE /performance_indicators/1.json
def destroy
#performance_indicator.destroy
respond_to do |format|
format.html { redirect_to performance_indicators_url, notice: 'Performance indicator was successfully destroyed.' }
format.json { head :no_content }
end
end
def set_comment
#improvement_action = ImprovementAction.find(params[:improvement_action_id])
#comment = Comment.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_performance_indicator
#performance_indicator = PerformanceIndicator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def performance_indicator_params
params.require(:performance_indicator).permit(:name, :numberTimesIdentifiedProblems, :numberTimesAnalysed)
end
end
Lets start by fixing the deep nesting.
resources :performance_indicators, shallow: true do
resources :improvement_actions
end
resources :improvement_actions, only: [] do
member do
put "like" => "improvement_actions#upvote"
put "unlike" => "improvement_actions#downvote"
end
resources :comments
end
only: [] is a clever trick that nests routes under a resource but suppresses the generation of routes. We want that since the first block actually declares all the routes we need.
Prefix Verb URI Pattern Controller#Action
performance_indicator_improvement_actions GET /performance_indicators/:performance_indicator_id/improvement_actions(.:format) improvement_actions#index
POST /performance_indicators/:performance_indicator_id/improvement_actions(.:format) improvement_actions#create
new_performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/new(.:format) improvement_actions#new
edit_performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/:id/edit(.:format) improvement_actions#edit
performance_indicator_improvement_action GET /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#show
PATCH /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#update
PUT /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#update
DELETE /performance_indicators/:performance_indicator_id/improvement_actions/:id(.:format) improvement_actions#destroy
performance_indicators GET /performance_indicators(.:format) performance_indicators#index
POST /performance_indicators(.:format) performance_indicators#create
new_performance_indicator GET /performance_indicators/new(.:format) performance_indicators#new
edit_performance_indicator GET /performance_indicators/:id/edit(.:format) performance_indicators#edit
performance_indicator GET /performance_indicators/:id(.:format) performance_indicators#show
PATCH /performance_indicators/:id(.:format) performance_indicators#update
PUT /performance_indicators/:id(.:format) performance_indicators#update
DELETE /performance_indicators/:id(.:format) performance_indicators#destroy
like_improvement_action PUT /improvement_actions/:id/like(.:format) improvement_actions#upvote
unlike_improvement_action PUT /improvement_actions/:id/unlike(.:format) improvement_actions#downvote
improvement_action_comments GET /improvement_actions/:improvement_action_id/comments(.:format) comments#index
POST /improvement_actions/:improvement_action_id/comments(.:format) comments#create
new_improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/new(.:format) comments#new
edit_improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/:id/edit(.:format) comments#edit
improvement_action_comment GET /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#show
PATCH /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#update
PUT /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#update
DELETE /improvement_actions/:improvement_action_id/comments/:id(.:format) comments#destroy
Un-nesting the resources means there will be a lot less hoops to jump through.
class CommentsController < ApplicationController
before_action :set_improvement_action
# GET /improvement_actions/:improvement_action_id/comments/new
def new
#comment = #improvement_action.comments.new
end
# POST /improvement_actions/:improvement_action_id/comments
def create
#comment = #improvement_action.comments.new(comment_params) do |c|
# #todo - you should associate comment with the user who created it at some point.
# c.author = current_user
end
# note that you where saving the record twice!
if #comment.save
format.html { redirect_to [#improvement_action.performance_indicator, #improvement_action], notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: [#improvement_action, #comment] }
else
format.html { render :new }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
# ...
private
def comment_params
params.require(:comment).permit(:body)
end
def set_improvement_action
#improvement_action = ImprovementAction.includes(:comments)
.find(params[:improvement_action_id])
end
end
You should handle seeding with new records on the controller side if possible.
<%= form_for([#improvement_action, #comment]) do |f| %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

NameError at /events path - Undefined Local Variable or Method

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.

Resources