I have a customer model and book_room model
class Customer < ApplicationRecord
has_many :book_rooms
accepts_nested_attributes_for :book_rooms
end
class BookRoom < ApplicationRecord
belongs_to :customer
end
in the book_room controller the create method is from its parent
class BookRoomsController < ApplicationController
def create
#customer = Customer.find(params[:customer_id])
#customer_room = #customer.book_rooms.create(book_rooms_params)
flash[:notice] = "Customer has been added to room"
redirect_to customer_path(#customer)
end
def destroy
#customer = Customer.find(params[:customer_id])
#customer_room = #customer.book_rooms.find(params[:id])
#customer_room.destroy
flash[:notice] = "Customer room has been deleted"
redirect_to customer_path(#customer)
end
def edit
#customer = Customer.find(params[:customer_id])
end
def update
#customer = Customer.find(params[:customer_id])
#customer.book_rooms.update(book_rooms_params)
flash[:notice] = "Customer has checked out"
redirect_to #customer
end
private
def book_rooms_params
params.require(:book_room).permit(:room, :first_name, :last_name, :phone_number, :checked_out)
end
end
in the Customer show page
<%= form_for [#customer, #customer.book_rooms.build] do |f| %>
<% #room = Room.all %>
<%= f.label "Room: "%>
<%= f.select(:room, #room.collect { |a| [a.room_number, a.id] }) %>
<%= f.submit "Enter" %>
<div class="col-md-12"><%= render #customer.book_rooms.order("created_at DESC") %></div>
This works perfectly and all child objects get created. now when I try to edit the child attributes it doesn't update at all
heres the edit form in the book_room edit page/action
<%= form_for #customer do |f| %>
<%= f.fields_for :book_rooms, #customer.book_rooms do |f| %>
<%= f.check_box :checked_out %>
<% end %>
<%= f.submit "Enter" %>
is there something i am doing wrong? why doesn't it update?
Customers controller
class CustomersController < ApplicationController
before_action :set_customer, only: [:show, :edit, :update, :destroy]
# POST /customers.json
def create
#customer = Customer.new(customer_params)
respond_to do |format|
if #customer.save
format.html { redirect_to #customer, notice: 'Customer was successfully created.' }
format.json { render :show, status: :created, location: #customer }
else
format.html { render :new }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #customer.update(customer_params)
format.html { redirect_to #customer, notice: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: #customer }
else
format.html { render :edit }
format.json { render json: #customer.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_customer
#customer = Customer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone_number, :sex, :book_rooms_attributes => [:checked_out])
end
In your customers controller :
Try to change your function customer_params like:
def customer_params
params.require(:customer).permit(:first_name, :last_name, :phone_number, :sex, {:book_rooms => [:checked_out]})
end
For more explications see here
Change your update method to:
def update
#customer = Customer.find(params[:customer_id])
if #customer.update_attributes(customer_params)
flash[:notice] = "Customer has checked out"
redirect_to #customer
else
...redirect to edit page with a flash error message ...
end
end
You also need to modify your edit page.
<%= form_for(:customer, :url => {:action => 'update', :id => #customer.id}, :method => 'PUT') do |f| %>
Try changing the URL to update and changing the method to patch you will go to update method.
<%= form_for :customer, url: customer_path(#customer),method: :patch do |f| %>
<%= f.fields_for :book_rooms, #customer.book_rooms do |f| %>
<%= f.check_box :checked_out %>
<% end %>
<%= f.submit "Enter" %>
Related
I'm currently trying to add a collection_select of ranches to my staff
And I saw that it's better to create an extra table to make this association.
And I follow some tutorial, but is not working on my side
This is my code :
Staffs/_form :
<%= form_for(#staff) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= fields_for(#staff_ranch) do |x| %>
<div class="field">
<%= x.collection_select(:ranch_id, #all_ranch, :id, :name, { }, {:multiple => true}) %>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My models :
- Ranch :
has_many :ranchstaffs
has_many :staffs, :through => :ranchstaffs
- Staff :
has_many :ranchstaffs
has_many :ranches, :through => :ranchstaffs
-Ranchstaff :
belongs_to :ranch
belongs_to :staff
Staff controller :
class StaffsController < ApplicationController
before_action :set_staff, only: [:show, :edit, :update, :destroy]
# GET /ranches
# GET /ranches.json
def index
#staffs = current_user.staffs
end
# GET /ranches/1
# GET /ranches/1.json
def show
end
# GET /ranches/new
def new
#staff = Staff.new
#all_ranch = current_user.ranches
#staff_ranch = #staff.ranchstaffs.build
end
# GET /ranches/1/edit
def edit
end
# POST /ranches
# POST /ranches.json
def create
#staff = Staff.new(staff_params)
#staff.update(user_id: current_user.id)
respond_to do |format|
if #staff.save
format.html { redirect_to #staff, notice: 'Staff was successfully created.' }
format.json { render :show, status: :created, location: #staff }
else
format.html { render :new }
format.json { render json: #staff.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /ranches/1
# PATCH/PUT /ranches/1.json
def update
respond_to do |format|
if #staff.update(staff_params)
format.html { redirect_to #staff, notice: 'Staff was successfully updated.' }
format.json { render :show, status: :ok, location: #staff }
else
format.html { render :edit }
format.json { render json: #staff.errors, status: :unprocessable_entity }
end
end
end
# DELETE /ranches/1
# DELETE /ranches/1.json
def destroy
#staff.destroy
respond_to do |format|
format.html { redirect_to staffs_url, notice: 'Ranch was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_staff
#staff = Staff.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def staff_params
params.require(:staff).permit(:name, :user_id, :cat, :ranch_id)
end
end
Can you explain me why the model ranchstaff was not created after a creation of a new staff ?
As you are using fields_for you are using nested form but you are not permitting the parameters properly. First make change in your form:
<%= f.fields_for(#staff_ranch) do |x| %>
<div class="field">
<%= x.collection_select(:ranch_id, #all_ranch, :id, :name, { }, {:multiple => true}) %>
</div>
<% end %>
And then in your controller:
def staff_params
params.require(:staff).permit(:name, :user_id, :cat, ranchstaff_attributes: [ranch_id: []])
end
And in your Staff model write:
accepts_nested_attributes_for :ranchstaffs
Then your ranchstaff should be created when the User is being created.
Your ranch_id is coming in an array. So u have to specify that ranch_id would be array in strong parameters.
so your staff_params method would look like this
def staff_params
params.require(:staff).permit(:name, :user_id, :cat, :staff_ranch_attributes =>[:ranch_id => []])
end
I have a problem a week ago with nested form. I have a model called users another Auto and another Perfil. I need that when a user creates a new Auto you can change your perfil data if needed
Please can you help me? We'll be very grateful
Try the following:
user model
class User < ActiveRecord::Base
has_many :autos
has_one :perfil
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Auto model
class Auto < ActiveRecord::Base
belongs_to :user
belongs_to :perfil
accepts_nested_attributes_for :perfil, allow_destroy: true
end
Perfil Model
class Perfil < ActiveRecord::Base
belongs_to :user
has_many :autos
end
Auto Controller
class AutosController < ApplicationController
before_action :set_auto, only: [:show, :edit, :update, :destroy]
def new
#auto = Auto.new
#perfil = #auto.build_perfil
end
def create
#auto = Auto.new(auto_params)
#auto.user_id = current_user.id
#auto.perfil_id = current_user.perfil.id
respond_to do |format|
if #auto.save
format.html { redirect_to #auto, notice: 'Auto was successfully created.' }
format.json { render :show, status: :created, location: #auto }
else
format.html { render :new }
format.json { render json: #auto.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #auto.update(auto_params)
format.html { redirect_to #auto, notice: 'Auto was successfully updated.' }
format.json { render :show, status: :ok, location: #auto }
else
format.html { render :edit }
format.json { render json: #auto.errors, status: :unprocessable_entity }
end
end
end
private
def set_auto
#auto = Auto.find(params[:id])
end
def auto_params
params.require(:auto).permit(:perfil_id, :marca, :modelo, :año, :user_id, perfil_attributes: [:user_id, :nombre, :rut, :telefono, :direccion])
end
end
Perfil Controller
class PerfilsController < ApplicationController
before_filter :authenticate_user!
def show
current_user.perfil ||= Perfil.new
#perfil = current_user.perfil
end
def update
#perfil = current_user.perfil
respond_to do |format|
if current_user.perfil.update_attributes(perfil_params)
format.html {redirect_to root_path, notice: "Datos Actualizados" }
else
format.html {render action: "show"}
end
end
end
def create
current_user.perfil ||= Perfil.new
#perfil = current_user.perfil
respond_to do |format|
if current_user.perfil.update_attributes(perfil_params)
format.html {redirect_to root_path, notice: "Datos Actualizados" }
else
format.html {render action: "show"}
end
end
end
private
def perfil_params
params.require(:perfil).permit(:user_id, :nombre, :rut, :telefono, :direccion)
end
end
And _form.html.erb
<%= simple_form_for #auto do |f| %>
<div class="form-inputs">
<%= f.input :marca %>
<%= f.input :modelo %>
<%= f.input :año %>
<%= f.simple_fields_for :perfil, current_user.perfil do |p| %>
<%= p.input :nombre %>
<%= p.input :telefono %>
<%= p.input :direccion %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
<%= link_to "Save", autos_path, :class => 'button_submit' %>
Routes.rb
Rails.application.routes.draw do
resources :autos
devise_for :users
resources :perfils, only: [:index,:show, :update , :create]
root 'autos#index'
end
This is almost perfect, just 2 little things will get you going.
1 The fields_for need only have :perfil as the object
<%= simple_form_for #auto do |f| %>
<div class="form-inputs">
<%= f.input :marca %>
<%= f.input :modelo %>
<%= f.input :año %>
<%= f.simple_fields_for :perfil do |p| %>
<%= p.input :nombre %>
<%= p.input :telefono %>
<%= p.input :direccion %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
<%= link_to "Save", autos_path, :class => 'button_submit' %>
2 The controller needs to create the object as the current user
def create
#auto = current_user.autos.new(auto_params)
respond_to do |format|
if #auto.save
format.html { redirect_to #auto, notice: 'Auto was successfully created.' }
format.json { render :show, status: :created, location: #auto }
else
format.html { render :new }
format.json { render json: #auto.errors, status: :unprocessable_entity }
end
end
end
My Rails application have two model. Location and Post, Location have many post.I am Using
ancestry gem.
class Post < ActiveRecord::Base
belongs_to :location, :counter_cache => true
end
class Location < ActiveRecord::Base
include Tree
has_ancestry :cache_depth => true
has_many :posts
end
My Post Controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
#posts = Post.all
end
def show
end
def new
#post = Post.new
end
def edit
end
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 action: 'show', status: :created, location: #post }
else
format.html { render action: 'new' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
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(:name)
end
end
If i am create new Post with which Location belongs in _form.html.erb
<%= form_for(#post) do |f| %>
<% 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%= select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %>
<div class="actions">
<%= f.submit %>
</div>
Browser show error message which is display bellow
ArgumentError in Posts#new
Not sure if this fixes your error, but:
To make the dropdown working, change the select line to:
<%= f.select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %>
This is because you want the formbuilder f to handle the creation of the form element.
You also have to whitelist the :location_id parameter in the controller:
def post_params
params.require(:post).permit(:name, :location_id)
end
I'm pretty new to rails and I'm having a bit of a though time to register my employeur.
Here are my routes:
resources :users do
resource :prestataire
resource :employeur
end
I have a has_one relationship between my employeur and user resources:
class User < ActiveRecord::Base
has_one :prestataire
has_one :employeur
has_secure_password
end
class Employeur < ActiveRecord::Base
belongs_to :user
validates :siren, :societe, :code_postal, presence: true
end
And here's where I think the issue is:
<%= form_for [#user,#employeur], url: user_employeur_path(#user) do |f| %>
<% if #employeur.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#employeur.errors.count, "error") %> prohibited this employeur from being saved:</h2>
<ul>
<% #employeur.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :siren, 'Siren: ' %><br>
<%= f.text_field :siren %>
</div>
.
.
.
<div class="actions">
<%= f.submit %>
</div>
<% end %>
When I fill in this issue, I'm redirected to /users/193/employeur.84 and I get this error: Couldn't find Employeur without an ID
Those are the only two params that are send after the form, even though I've indicated :siren, :societe, :code_postal
{"user_id"=>"193",
"format"=>"84"}
I guess that this might also come from my Emmployeur controller:
class EmployeursController < ApplicationController
before_filter :set_user
def index
#employeurs = #user.employeur.all
end
def show
#employeur = Employeur.find(params[:id]) #This is where the error happens since no id is sent.
end
def new
#employeur = #user.build_employeur
end
def edit
#employeur = Employeur.find(params[:id])
end
def create
#employeur = #user.build_employeur(employeur_params)
respond_to do |format|
if #employeur.save
format.html { redirect_to [#user, #employeur], notice: 'Employeur was successfully created.' }
else
format.html { render action: 'new' }
format.json { render json: #employeur.errors, status: :unprocessable_entity }
end
end
end
def update
#employeur = Employeur.find(params[:id])
respond_to do |format|
if #employeur.update_attributes(employeur_params)
format.html { redirect_to [#user, #employeur], notice: 'Employeur was successfully created.' }
format.json { render action: 'show', status: :created, location: #employeur }
else
format.html { render action: 'new' }
format.json { render json: #employeur.errors, status: :unprocessable_entity }
end
end
end
def destroy
#employeur = Employeur.find(params[:id])
#employeur.destroy
respond_to do |format|
format.html { redirect_to #user }
format.json { head :no_content }
end
end
private
def set_user
#user = User.find(params[:user_id])
end
def employeur_params
params.require(:employeur).permit(:siren, :societe, :code_postal)
end
end
Any help would be more then welcome.
In order to give an example of singular and nested resource working, I'll add a little more of my code:
class Employeur < ActiveRecord::Base
model_name.instance_variable_set(:#route_key, 'employeur')
belongs_to :user
has_many :projets, as: :projetable
has_many :prestataires, through: :projets
has_many :offres, through: :projets
has_many :feedbacks, through: :projets
validates :siren, :societe, :code_postal, presence: true, uniqueness: true
validates :code_postal, presence: true
validates_associated :user
end
Here's mu User controller that leads me from the user form to the employeur once filled:
class UsersController < ApplicationController
#TODO index user doit être suprimé quand inutile pour dev
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
# GET /users/1/edit
def edit
#user = User.find(params[:id])
end
# POST /users
# POST /users.json
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
if params[:commit] == 'Employeur'
format.html { redirect_to new_user_employeur_path(user_id: #user), notice: "Renseignez vos informations d'employeur" }
format.json { render action: 'show', status: :created, location: #user }
else
format.html { redirect_to new_user_prestataire_path(user_id: #user), notice: "Renseignez vos informations de prestataire" }
format.json { render action: 'show', status: :created, location: #user }
end
else
format.html { render action: 'new' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update(user_params)
if params[:commit] == 'Prestataire'
format.html { redirect_to new_user_prestataire_path(user_id: #user), notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { redirect_to new_user_employeur_path(user_id: #user), notice: "User was successfully updated." }
format.json { head :no_content }
end
else
format.html { render action: 'edit' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
#user = User.find(params[:id])
#user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :surname, :forename, :civility, :phone)
end
end
And finally, my User form:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :civility, 'Titre de civilité: ' %><br>
<%= f.text_field :civility %>
</div>
<div class="field">
<%= f.label :forename, 'Prénom: ' %><br>
<%= f.text_field :forename %>
</div>
<div class="field">
<%= f.label :surname, 'Nom de famille: ' %><br>
<%= f.text_field :surname %>
</div>
<div class="field">
<%= f.label :email, 'Email: ' %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password, 'Mot de passe: ' %><br>
<%= f.password_field :password, size: 40 %>
</div>
<div class="field">
<%= f.label :password_confirmation, 'Confirmation de mot de passe: ' %><br>
<%= f.password_field :password_confirmation, size: 40 %>
</div>
<div class="field">
<%= f.label :phone, 'Numéro de téléphone: ' %><br>
<%= f.text_field :phone %>
</div>
<div class="actions">
<%= f.submit "Employeur" %>
<%= f.submit "Prestataire" %>
</div>
<% end %>
Hope this will help someone as much as it would have helped me. Cheers !
You are not passing in the #employeur to your nested route user_employeur_path. Try this on your form_for line:
user_employeur_path(#user, #employeur)
But you shouldn't have to specify url; this should work:
<%= form_for [#user,#employeur] do |f| %>
See creating paths and urls from objects.
You don't have your EmployeursController setup correctly. Since employeur is a singular resource, you cannot reference it by id. Instead you need to change your show action to this:
def show
#employeur = User.find(params[:user_id]).employeur
end
The reason is that user_employeur_path(#user) creates a path like /users/193/employeur where you can access the user id in the controller via params[:user_id]
Also, since employeur is a singular resource, there is no index action defined for it. You can remove the index action entirely from your controller.
For people like me who were desperate to find an example of nested and singular resource working, I post my controller corrected thanks to the help of Hamed:
class EmployeursController < ApplicationController
before_filter :set_user
def new
#employeur = #user.build_employeur
end
def show
#employeur = #user.employeur
end
def edit
#employeur = #user.employeur
end
def create
#employeur = #user.build_employeur(employeur_params)
respond_to do |format|
if #employeur.save
format.html { redirect_to [#user, #employeur], notice: 'Employeur was successfully created.' }
else
format.html { render action: 'new' }
format.json { render json: #employeur.errors, status: :unprocessable_entity }
end
end
end
def update
#employeur = #user.employeur
respond_to do |format|
if #employeur.update_attributes(employeur_params)
format.html { redirect_to [#user, #employeur], notice: 'Employeur was successfully created.' }
format.json { render action: 'show', status: :created, location: #employeur }
else
format.html { render action: 'new' }
format.json { render json: #employeur.errors, status: :unprocessable_entity }
end
end
end
def destroy
#employeur = #user.employeur
#employeur.destroy
respond_to do |format|
format.html { redirect_to root_path }
format.json { head :no_content }
end
end
private
def set_user
#user = User.find(params[:user_id])
end
def employeur_params
params.require(:employeur).permit(:siren, :societe, :code_postal)
end
end
In my Rails app, I have a controller tickets_controller.rb and model ticket.rb. For creating a ticket I have the following form,
<%= form_for(#ticket) do |f| %>
<% if #ticket.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#ticket.errors.count, "error") %> prohibited this ticket from being saved:</h2>
<ul>
<% #ticket.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :ref_no, "Reference Number"%><br/>
<%= f.text_field :ref_no%><br />
<%= f.label :category, "Type of Request"%><br/>
<%= f.text_field :category_id %><br />
<%= f.label :issue, "Issue"%><br/>
<%= f.text_area :issue%><br />
<%= f.label :ticket_priority, "Priority level"%><br/>
<%= f.text_field :ticket_priority_id %><br />
<%= f.label :ticket_status, "Current Status"%><br/>
<%= f.text_field :ticket_status_id %><br />
<%= f.label :project, "Project"%><br/>
<%= f.text_field :project_id %><br />
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I want to create a unique random reference number on the form_load (ticket/new), and it should be appended to Reference Number text field. While creating a new reference number, it should check the tickets table for duplication. So I have the following model,
ticket.rb
class Ticket < ActiveRecord::Base
attr_accessible :issue, :ticket_status_id, :ticket_priority_id, :ref_no, :category_id, :project_id
has_many :ticket_statuses , :through => :ticket_histories
has_one :ticket_priority
belongs_to :user
before_create :generate_token
protected
def generate_num
self.token = loop do
random_token = random(1000000000)
break random_token unless Ticket.exists?(:ref_no => random_token)
end
end
end
and
tickets_controller.rb
class TicketsController < ApplicationController
before_filter :authenticate_user!
#load_and_authorize_resource
def index
#tickets = Ticket.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #tickets }
end
end
def show
#ticket = Ticket.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #ticket }
end
end
def new
#ticket = Ticket.new
#ref_no = Ticket.generate_num
#categories = Category.all
#status = TicketStatus.first
#priorities = TicketPriority.all
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #ticket }
end
end
def edit
#ticket = Ticket.find(params[:id])
end
def create
#ticket = Ticket.new(params[:ticket])
respond_to do |format|
if #ticket.save
format.html { redirect_to #ticket, :notice => 'Ticket was successfully created.' }
format.json { render :json => #ticket, :status => :created, :location => #ticket }
else
format.html { render :action => "new" }
format.json { render :json => #ticket.errors, :status => :unprocessable_entity }
end
end
end
def update
#ticket = Ticket.find(params[:id])
respond_to do |format|
if #ticket.update_attributes(params[:ticket])
format.html { redirect_to #ticket, :notice => 'Ticket was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #ticket.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#ticket = Ticket.find(params[:id])
#ticket.destroy
respond_to do |format|
format.html { redirect_to tickets_url }
format.json { head :no_content }
end
end
end
When I run my app, I am getting the following error. Can anyone help?
NoMethodError in TicketsController#new
undefined method `generate_num' for #<Class:0x7f5cdc1f21c0>
Rails.root: /home/local/Rajesh/ticket_system
Application Trace | Framework Trace | Full Trace
app/controllers/tickets_controller.rb:27:in `new'
Change your model method generate_num to self.generate_num.
def self.generate_num
token = loop do
random_token = random(1000000000)
break random_token unless Ticket.exists?(:ref_no => random_token)
end
end
You have defined and instance method and you are calling it using Class
Call method using object
#ticket.generate_num