ActiveModel::UnknownAttributeError in PostsController#create - ruby-on-rails

It was working perfectly fine but now I'm getting this error for some reason?
class Post < ApplicationRecord
belongs_to :user
geocoded_by :address
after_validation :geocode, if: :address_changed?
has_many :rsvps
has_many :users, through: :rsvps
end
form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="action">
<%= form.label :address %>
<%= form.text_area :address, id: :post_address %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
post 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(params[:id])
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
unless current_user == #post.user
redirect_back fallback_location: root_path, notice: 'User is not owner'
end
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: '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: '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: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:date, :user_id, :description, :name, :address)
end
load_and_authorize_resource
end
migrations
class AddLatitudeAndLongitudeToPost < ActiveRecord::Migration[5.1]
def change
add_column :posts, :latitude, :float
add_column :posts, :longitude, :float
add_column :posts, :address, :string
end
end
posts migration
class CreatePosts < ActiveRecord::Migration[5.1]
def change
create_table :posts do |t|
t.datetime :date
t.string :name
t.integer :user_id
t.text :description
t.timestamps
end
end
end
I'm not sure how it would stop working automatically as there was literally no changes. I was just figuring out why my google static map was getting an error. However I didn't implement any changes so I don't know what could have possibly caused this error? I would appreciate some advice, thank you.

The possible reason for ActiveModel::UnknownAttributeError is when you try to assign an attribute which is not present in the table.
You must have added some attributes to Post model but must have forgotten to run the migration
bundle exec rake db:migrate
Running this command should solve the problem
NOTE: I can see there is a new migration AddLatitudeAndLongitudeToPost which is adding some columns to posts table. Check the migration status with:
bundle exec rake db:migrate:status
Check if you can see same attributes in schema.rb

Related

Nested form(cocoon gem) not visible in view rails

i made a nested form for my invoice application with the cocoon gem but the form isn't showing on my application but it isn't giving out any errors either.
_form.html.erb - scaffold form partial
<address>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
_customer_fields.html.erb - cocoon partial
<div class="nested-fields">
<div class="form-group">
<%= f.label 'Company Name' %><br/>
<%= f.text_field :company_name, placeholder: 'company name' %>
</div>
<div class="form-group">
<%= f.label 'Address' %><br>
<%= f.text_field :address_line_1, placeholder: 'address' %>
</div>
<div class="form-group">
<%= f.label 'Zip Code' %><br>
<%= f.text_field :zip_code %>
</div>
<%= link_to_remove_association "remove customer", f, class: 'btn btn-primary' %>
</div>
Invoice.rb model
class Invoice < ActiveRecord::Base
has_one :company
has_one :customer
has_many :products
accepts_nested_attributes_for :customer, reject_if: :all_blank, allow_destroy: true
validates :number, :currency, :date, :duedate, :btwtotal, :subtotal, :total, presence: true
end
customer.rb model
class Customer < ActiveRecord::Base
belongs_to :invoice
end
Invoices_controller.rb
class InvoicesController < ApplicationController
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
#invoices = Invoice.all
end
# GET /invoices/1
# GET /invoices/1.json
def show
end
# GET /invoices/new
def new
#invoice = Invoice.new
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
#invoice = Invoice.new(invoice_params)
respond_to do |format|
if #invoice.save
format.html { redirect_to #invoice, notice: 'Invoice was successfully created.' }
format.json { render :show, status: :created, location: #invoice }
else
format.html { render :new }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if #invoice.update(invoice_params)
format.html { redirect_to #invoice, notice: 'Invoice was successfully updated.' }
format.json { render :show, status: :ok, location: #invoice }
else
format.html { render :edit }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
#invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url, notice: 'Invoice was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_invoice
#invoice = Invoice.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:number, :currency, :date, :duedate, :btwtotal,
:subtotal, :total, :footer, customers_attributes: [:id, :company_name, :address_line_1, :zip_code, :_destroy],
companies_attributes: [:id, :btw_number, :iban_number, :kvk_number, :company_name, :_destroy])
end
end
Does anybody know how to fix this? Any help would be much much appreciated!
try it
<address>
<%= f.object.build_customer if f.object.customer.nil? %>
<%= f.fields_for :customer do |customer| %>
<%= render 'customer_fields', f: customer %>
<%= link_to_add_association 'Add customer',f, :customer %>
<% end %>
</address>
edit
in the Luissimo solution was missing to insert invoice_id in customer schema
rails generate migration AddInvoiceIdToCustomer invoice_id:integer
rake db:migrate
Invoice.first.build_customer
Looking at your original code, in your controller you should have added:
def new
#invoice = Invoice.new
#invoice.customer.build
end

nested fields_for not showing up in forms

Everything looks great as far as I can tell -- but the contents for the field_for nested form aren't displaying the 3 question forms I want. Why?
survey.rb
class Survey < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions
end
question.rb
class Question < ActiveRecord::Base
belongs_to :survey
end
surveys_controller.rb
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]
# GET /surveys
# GET /surveys.json
def index
#surveys = Survey.all
end
# GET /surveys/1
# GET /surveys/1.json
def show
end
# GET /surveys/new
def new
#survey = Survey.new
3.times { #survey.questions.build }
end
# GET /surveys/1/edit
def edit
end
# POST /surveys
# POST /surveys.json
def create
#survey = Survey.new(survey_params)
respond_to do |format|
if #survey.save
format.html { redirect_to #survey, notice: 'Survey was successfully created.' }
format.json { render :show, status: :created, location: #survey }
else
format.html { render :new }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /surveys/1
# PATCH/PUT /surveys/1.json
def update
respond_to do |format|
if #survey.update(survey_params)
format.html { redirect_to #survey, notice: 'Survey was successfully updated.' }
format.json { render :show, status: :ok, location: #survey }
else
format.html { render :edit }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# DELETE /surveys/1
# DELETE /surveys/1.json
def destroy
#survey.destroy
respond_to do |format|
format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_survey
#survey = Survey.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
params.require(:survey).permit(:name)
end
end
surveys/new.html.erb
<h1>New Survey</h1>
<%= render 'form' %>
<%= link_to 'Back', surveys_path %>
surveys/_form.html.erb
<%= form_for(#survey) do |f| %>
<% if #survey.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% #survey.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<% f.fields_for :questions do |builder| %>
<p>
<%= builder.label :content, "Question" %><br />
<%= builder.text_area :content, :rows => 3 %>
</p>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Can't believe I did this -- was missing <%= on fields_for

Create and Edit and Update won't work for 3rd laryer objects from Nested form on 1st layer in Rails 4.2

I'm making nested form with Rails 4.2. And I'm having problems with Create and Edit and Update actions on 3rd layer object Answer. I've summarized information in below. If you know what I'm doing wrong or did not do, please kindly let me know.
Scope:
User can create Survey and Questions and Answers.
User can edit/update Survey and Questions and Answers.
User can delete Survey and Questions and Answers
Issues:
I've set up nested has_many associations between Survey > Questions > Answers and setup 'accepts_nested_attributes_for' and added model's attributes in permit parameters. And made Survey form that has fields for all models. It works fine for creating Survey with Questions with one submit from one form, and these two model's attributes appear in Show action as well. But Answer won't saved, and answer from doesn't appear in Edit form.
Code:
Models
Survey.rb
class Survey < ActiveRecord::Base
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions, allow_destroy: true
end
Question.rb
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers, allow_destroy: true
end
Answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
end
Controllers
Surveys_controller.rb
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]
# GET /surveys
# GET /surveys.json
def index
#surveys = Survey.all
end
# GET /surveys/1
# GET /surveys/1.json
def show
end
# GET /surveys/new
def new
#survey = Survey.new
#questions = #survey.questions.build
#answers = #questions.answers.build
##survey.questions.build
##questions.answers.build
end
# GET /surveys/1/edit
def edit
# #questions = #survey.questions.update(survey_params) #This will get ActionController::ParameterMissing in SurveysController#edit
# #answers = #questions.answers.update(survey_params) #This will get ActionController::ParameterMissing in SurveysController#edit
end
# POST /surveys
# POST /surveys.json
def create
#survey = Survey.new(survey_params)
respond_to do |format|
if #survey.save
format.html { redirect_to #survey, notice: 'Survey was successfully created.' }
format.json { render :show, status: :created, location: #survey }
else
format.html { render :new }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /surveys/1
# PATCH/PUT /surveys/1.json
def update
respond_to do |format|
if #survey.update(survey_params)
format.html { redirect_to #survey, notice: 'Survey was successfully updated.' }
format.json { render :show, status: :ok, location: #survey }
else
format.html { render :edit }
format.json { render json: #survey.errors, status: :unprocessable_entity }
end
end
end
# DELETE /surveys/1
# DELETE /surveys/1.json
def destroy
#survey.destroy
respond_to do |format|
format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_survey
#survey = Survey.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
# params.require(:survey).permit(:name, :description)
params.require(:survey).permit(:name, :description, questions_attributes: [:id, :name, :description, :_destroy])
# params.require(:survey).permit(:name, :description, questions_attributes: [:id, :name, :description, :_destroy, answers_attributes: [:id, :content, :_destroy]]) #I don't see any change from addin ansewrs attributes here
end
end
Questions_controller.rb
class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]
# 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
#question.answers.build
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
# 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.require(:question).permit(:name, :description, :survey_id, :_destroy, answers_attributes: [:id, :content, :_destroy])
end
end
Answers_controller.rb
class AnswersController < ApplicationController
before_action :set_answer, only: [:show, :edit, :update, :destroy]
# GET /answers
# GET /answers.json
def index
#answers = Answer.all
end
# GET /answers/1
# GET /answers/1.json
def show
end
# GET /answers/new
def new
#answer = Answer.new
end
# GET /answers/1/edit
def edit
end
# POST /answers
# POST /answers.json
def create
#answer = Answer.new(answer_params)
respond_to do |format|
if #answer.save
format.html { redirect_to #answer, notice: 'Answer was successfully created.' }
format.json { render :show, status: :created, location: #answer }
else
format.html { render :new }
format.json { render json: #answer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /answers/1
# PATCH/PUT /answers/1.json
def update
respond_to do |format|
if #answer.update(answer_params)
format.html { redirect_to #answer, notice: 'Answer was successfully updated.' }
format.json { render :show, status: :ok, location: #answer }
else
format.html { render :edit }
format.json { render json: #answer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /answers/1
# DELETE /answers/1.json
def destroy
#answer.destroy
respond_to do |format|
format.html { redirect_to answers_url, notice: 'Answer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_answer
#answer = Answer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def answer_params
params.require(:answer).permit(:content, :question_id)
end
end
DB
schema.rb
ActiveRecord::Schema.define(version: 20160602204457) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "answers", force: :cascade do |t|
t.string "content"
t.integer "question_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "answers", ["question_id", "created_at"], name: "index_answers_on_question_id_and_created_at", using: :btree
add_index "answers", ["question_id"], name: "index_answers_on_question_id", using: :btree
create_table "questions", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "survey_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "questions", ["survey_id", "created_at"], name: "index_questions_on_survey_id_and_created_at", using: :btree
add_index "questions", ["survey_id"], name: "index_questions_on_survey_id", using: :btree
create_table "surveys", force: :cascade do |t|
t.string "name"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_foreign_key "answers", "questions"
add_foreign_key "questions", "surveys"
end
Views
app/views/surveys/new.html.erb
<h1>New Survey</h1>
<%= render 'form' %>
<%= link_to 'Back', surveys_path %>
app/views/surveys/new.html.erb
<h1>Editing Survey</h1>
<%= render 'form' %>
<%= link_to 'Show', #survey %> |
<%= link_to 'Back', surveys_path %>
app/views/surveys/_form.html.erb
<%= form_for(#survey) do |f| %>
<%#= form_for(setup_survey(#survey)) do |f| %>
<% if #survey.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% #survey.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<%= f.fields_for :questions do |ff| %>
<%= render 'question_fields', ff: ff %>
<% end %>
<%= link_to_add_fields "Add Question", f, :questions %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
app/views/surveys/_question_fields.html.erb
<fieldset>
<div class="field">
<%= ff.label :name, "Question Name" %><br>
<%= ff.text_field :name %><br>
</div>
<div class="field">
<%= ff.label :description, "Question Description" %><br>
<%= ff.text_field :description %>
</div>
<div class="field">
<%= ff.check_box :_destroy %>
<%= ff.label :_destroy, "Remove Question" %>
</div>
<%= ff.fields_for :answers do |fff| %>
<%= render 'answer_fields', fff: fff %>
<% end %>
</fieldset>
app/views/surveys/_answer_fields.html.erb
<fieldset>
<div class="field">
<%= fff.label :content, "Answer" %>
<%= fff.text_field :content %>
</div>
<div class="field">
<%= fff.check_box :_destroy %>
<%= fff.label :_destroy, "Remove Answer" %>
</div>
</fieldset>
app/views/surveys/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #survey.name %>
</p>
<p>
<strong>Description:</strong>
<%= #survey.description %>
</p>
<ul>
<% #survey.questions.each do |question| %>
<li>
<%= question.name %><br>
<%= question.description %>
<% question.answers.each do |answer| %>
<%= answer.content %>
<% end %>
</li>
<% end %>
</ul>
<%= link_to 'Edit', edit_survey_path(#survey) %> |
<%= link_to 'Back', surveys_path %>
Thanks!
You should whitelist the answers_attributes in survey_params. Changing it like below should get you going.
def survey_params
params.require(:survey).permit(:name, :description, questions_attributes: [:id, :name, :description, :_destroy, answers_attributes: [:id, :content, :_destroy]])
end

Can't link movie with a genre_id in form for Rails

I'm trying to link movies with a specific genre via a form in rails. I have seeded several genre instances in the db and have created a select tag that should, in theory, give a movie a genre_id based on the genres seeded.
Here is my Movie Migration:
class CreateMovies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :name
t.integer :year
t.string :director
t.string :lead_actor
t.references :genre, index: true, foreign_key: true
t.timestamps null: false
end
end
end
Here is the form:
<%= form_for(#movie) do |f| %>
<% if #movie.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#movie.errors.count, "error") %> prohibited this movie from being saved:</h2>
<ul>
<% #movie.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :genre_id %><br>
<%= f.select(:genre_id, #genres.map {|p| [ p.name, p.id ] }) %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :year %><br>
<%= f.number_field :year %>
</div>
<div class="field">
<%= f.label :director %><br>
<%= f.text_field :director %>
</div>
<div class="field">
<%= f.label :lead_actor %><br>
<%= f.text_field :lead_actor %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
All of my dropdown boxes are being displayed properly, but when I go into the database and check the movies genre_id, it equals 'nil'.
Full controller:
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
# GET /movies
# GET /movies.json
def index
#movies = Movie.all
end
# GET /movies/1
# GET /movies/1.json
def show
end
# GET /movies/new
def new
#movie = Movie.new
#genres = Genre.all
end
# GET /movies/1/edit
def edit
end
# POST /movies
# POST /movies.json
def create
#movie = Movie.new(movie_params)
respond_to do |format|
if #movie.save
format.html { redirect_to #movie, notice: 'Movie was successfully created.' }
format.json { render :show, status: :created, location: #movie }
else
format.html { render :new }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /movies/1
# PATCH/PUT /movies/1.json
def update
respond_to do |format|
if #movie.update(movie_params)
format.html { redirect_to #movie, notice: 'Movie was successfully updated.' }
format.json { render :show, status: :ok, location: #movie }
else
format.html { render :edit }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
# DELETE /movies/1
# DELETE /movies/1.json
def destroy
#movie.destroy
respond_to do |format|
format.html { redirect_to movies_url, notice: 'Movie was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_movie
#movie = Movie.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def movie_params
params.require(:movie).permit(:name, :year, :director, :lead_actor)
end
end

error stating blank fields when trying to create events and fields not blank

I get "3 errors prohibited this event from being saved:Title can't be blank Date can't be blank Description can't be blank. it was working perfectly but not sure whats wrong with it now?
When they are not even blank?
here is the controller.
class EventsController < ApplicationController
before_action :authenticate_user!, :only => [:create , :destroy]
before_action :set_event, only: [:show, :edit, :update, :destroy]
# GET /events
# GET /events.json
def index
#events = Event.all
end
# GET /events/1
# GET /events/1.json
def show
end
# GET /events/new
def new
#event = Event.new
end
# GET /events/1/edit
def edit
end
# POST /events
# POST /events.json
def create
#event = Event.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 :new }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
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
# DELETE /events/1
# DELETE /events/1.json
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(:title, :date, :description)
end
end
Here is the form:
<%= form_for(#event) do |f| %>
<% if #event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#event.errors.count, "error") %> prohibited this event from being saved:</h2>
<ul>
<% #event.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :date %><br>
<%= f.date_select :date %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
model code as per request :
class Event < ActiveRecord::Base
validates_presence_of :title , :date, :description
validates_uniqueness_of :title
end
Problem was with one of the gems I have installed which I have now removed. working now.

Resources