So we make little tweaks to our model class to make this work. Here I have a model named demo.rb. and it looks like this.
In the above code the demo class is not inheriting the active record class. Instead we are including other modules like validations, conversion and we are extending Naming. These classes have to be added for the form to work properly. For example, Active model validation lets you validate your form.class Demo
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name
validates :name , presence:true
def initialize(attributes ={})
attributes.each do |name , value|
send("#{name}=" , value)
end
end
Since you are posting the data from the form using hashes you need initialize method to get those values in a hash. It loops through the attributes and uses the send method to assign the data and value.
In controller instead of @demo.save? you should use @demo.valid? since we are not saving the data to the database. And the code looks like this<%= form_for(@demo) do |f| %>
<%= render 'shared/bucket_error_messages' %>
<%= f.label :name%>
<%= f.text_field :name%>
<%= f.submit "createbucket", class: "btn btn-default" %>
<% end %>
The create action doesn’t save the data , instead it does the action you mention the if condition.class DemosController < ApplicationController
def index
redirect_to :action => "show"
end
def new
@demo = Demo.new
end
def create
@demo = Demo.new(params[:demo])
if @Demo.valid?
<your action goes here>
end
end