I am using active admin for my admin panel in Ruby on Rails website. I have this code on my ApartmentPost resource.
ActiveAdmin.register ApartmentPost do
permit_params :title, :max_stay, :min_stay, :bed_configuration, :number_of_guests,
:features, :description, :admin_user_id, :apartment_number,:latitude, :longitude, :clean_fee, :country, :location, photos: []
form(html: { multipart: true }) do |f|
f.inputs do
f.input :title
f.input :max_stay
f.input :min_stay
f.input :bed_configuration
f.input :number_of_guests
f.input :features
f.input :description
f.input :country
f.input :location
f.input :apartment_number
f.input :latitude
f.input :longitude
f.input :clean_fee
f.input :admin_user_id, as: :hidden, input_html: {value: current_admin_user.id}
f.file_field :photos, multiple: true
end
f.actions
end
So the error I am getting is while creating a new ApartmentPost. The error is:
I had created a column called rate in AparmentPost but now it is already removed. Still it gives this error.
Here's the table from schema.rb:
create_table "apartment_posts", force: :cascade do |t|
t.integer "admin_user_id", null: false
t.integer "max_stay", null: false
t.string "bed_configuration", null: false
t.integer "number_of_guests", null: false
t.string "features", null: false
t.string "description", null: false
t.json "photos"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title", null: false
t.string "country", null: false
t.string "apartment_number", null: false
t.string "latitude"
t.string "longitude"
t.integer "min_stay", null: false
t.string "location", null: false
t.integer "clean_fee", default: 100, null: false
t.integer "property_type_id"
t.index ["admin_user_id"], name: "index_apartment_posts_on_admin_user_id", using: :btree
t.index ["property_type_id"], name: "index_apartment_posts_on_property_type_id", using: :btree
end
Here's the code in routes.rb
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
I am new in Rails and Active Admin. I tried searching the solution everywhere even tried uninstalling active_admin gem and installing again and removing apartment_post resource and generating it again but none og them worked.
You probably forgot to remove rate somewhere in your codebase. Try searching if you missed anything.
Related
I'm having trouble creating Sign up to my rails app, I'm using Devise and Simple_form, I have 2 models (User and department), users belongs_to :department and department has_many :users, i get an error when i try to sign up saying that department must exits.
devise/registrations/new.html.erb
<h2>Sign up</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :username, required: true, autofocus: true %>
<%= f.input :email, required: true %>
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %><br>
<%= f.association :department %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
user.rb :
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_uniqueness_of :email, :username
belongs_to :department
has_and_belongs_to_many :courses
end
department.rb :
class Department < ApplicationRecord
has_many :users
has_many :courses
end
I populated the departments table using seeds.rb and checked through mysql console.
schema.rb :
ActiveRecord::Schema.define(version: 20180502071349) do
create_table "courses", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "name"
t.text "description"
t.bigint "department_id"
t.string "instructor_name"
t.integer "credit_hours"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["department_id"], name: "index_courses_on_department_id"
end
create_table "departments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "enrollments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.bigint "user_id"
t.bigint "courses_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["courses_id"], name: "index_enrollments_on_courses_id"
t.index ["user_id"], name: "index_enrollments_on_user_id"
end
create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "username", default: "", null: false
t.bigint "department_id"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["department_id"], name: "index_users_on_department_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
add_foreign_key "courses", "departments"
add_foreign_key "enrollments", "courses", column: "courses_id"
add_foreign_key "enrollments", "users"
add_foreign_key "users", "departments"
end
migration files:
class CreateDepartments < ActiveRecord::Migration[5.1]
def change
create_table :departments do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
# frozen_string_literal: true
class DeviseCreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :username, null: false, default: ""
t.references :department, foreign_key: true
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
add_index :users, :username, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
class CreateCourses < ActiveRecord::Migration[5.1]
def change
create_table :courses do |t|
t.string :name
t.text :description
t.references :department, foreign_key: true
t.string :instructor_name
t.integer :credit_hours
t.timestamps
end
create_table :enrollments do |t|
t.references :user, foreign_key: true
t.references :courses, foreign_key: true
t.timestamps
end
end
end
P.S., I'm just starting out with rails and thanks for your help.
Error screenshot:
Devise doesn't know anything about your none standard department_id field and filters it as unpermitted parameter.
Create your own registrations controller (which extends Devise) and then customize these methods:
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :department_id)
end
This is just a sample. Feel it in with your real field names
Rails since 5.1 or so has a required belongs_to validation with newly generated apps.
You can disable that by:
class User < ApplicationRecord
belongs_to :department, optional: true
end
In this way, you can create users with an empty department first.
I am trying to add a relational field drop down select to the existing table. My current Schema
create_table "hardwares", force: :cascade do |t|
t.string "serialnumber"
t.string "modelnumber"
t.string "modeltype"
t.string "location"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "poc_id"
t.index ["poc_id"], name: "index_hardwares_on_poc_id"
end
create_table "pocs", force: :cascade do |t|
t.string "name"
t.string "address"
t.string "facility"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
The index field in the hardwares table was created when I ran the migration.
rails g migration addPocReferencesToHardwares poc:references
which generated the migration file below as well as the index in the schema
class AddPocReferencesToHardwares < ActiveRecord::Migration[5.1]
def change
add_reference :hardwares, :poc, foreign_key: true
end
end
After setting up the relationship, I would like to be able to select a POC for the hardware by name as a drop down of all available POC's.
I added this to the Hardware form:
<div class="field">
<%= form.label "POC" %>
<%= form.collection_select(:poc_id, Poc.all, :id, :name, { :prompt => 'Select a POC', :selected => #poc.poc_id }, { class: 'form-control' }) %>
</div>
The error I get is " undefined method `poc_id' for nil:NilClass". How do I allow a drop down selection for adding a POC to the hardware?
Issue was the fact that I had set the relationship as POC belongs to Hardware, thus in the Form field it needed to be:
<div class="field">
<%= form.label "POC" %>
<%= form.collection_select(:poc_id, Poc.all, :id, :name, { :prompt => 'Select a POC', :selected => #hardware.poc_id }, { class: 'form-control' }) %>
</div>
error: thrown from the form
undefined method `profession_id' for #<DirectoryMember:0xa12be40>
env:
Rails: 5.0.0.1
Ruby: 2.2.4
form.html.erb
<%= f.collection_select(:profession_id, #professions, :id, :title, {:include_blank => true, :prompt => true}) %>
routes.rb
resources :directory_members
directory_members.rb
#professions = Profession.all.order(:title)
def directory_member_params
params.require(:directory_member).permit(
:user_id,
:directory_id,
:name,
:company,
:profession_id,
:address,
:phone,
:facebook,
:video,
:twitter,
:linkedin,
:google_plus,
:youtube
)
end
schema.rb
create_table "directory_members", force: :cascade do |t|
t.integer "user_id"
t.integer "directory_id"
t.string "name"
t.string "company"
t.text "address"
t.string "phone"
t.string "facebook"
t.string "video"
t.string "twitter"
t.string "linkedin"
t.string "google_plus"
t.string "youtube"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "profession_id"
end
I have ran rake db:migrate and all seems fine until the page is rendered.
Any clues as to why this is throwing the error would be awesome. Thanks
I am using Simple Form and the Invoicing gem, but when I try to render the form, I get "RuntimeError in InvoicingLedgerItems#new" with "Association :sender_id not found". I want to save the primary key of the Users table (Devise gem) in the sender_id field (because the user is the sender of the invoice). I tried using the foreign_key option in the model, but it didn't seem to have any effect. Here's my code without using the foreign_key option.
Models:
class InvoicingLedgerItem < ActiveRecord::Base
acts_as_ledger_item
belongs_to :user
has_many :line_items, class_name: 'InvoicingLineItem', foreign_key: :ledger_item_id
accepts_nested_attributes_for :line_items
end
class User < ActiveRecord::Base
has_many :invoicing_ledger_items
end
View:
<%= simple_form_for #invoicing_ledger_item do |f| %>
<%= f.association :sender_id %>
<%= f.button :submit %>
<% end %>
Schema:
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "deleted_at"
end
create_table "invoicing_ledger_items", force: true do |t|
t.integer "sender_id"
t.integer "recipient_id"
t.string "type"
t.datetime "issue_date"
t.string "currency", limit: 3, null: false
t.decimal "total_amount", precision: 20, scale: 4
t.decimal "tax_amount", precision: 20, scale: 4
t.string "status", limit: 20
t.string "identifier", limit: 50
t.string "description"
t.datetime "period_start"
t.datetime "period_end"
t.string "uuid", limit: 40
t.datetime "due_date"
t.datetime "created_at"
t.datetime "updated_at"
end
You Need to Make a relation with user model through foreign key 'sender_id'
Model
class InvoicingLedgerItem < ActiveRecord::Base
acts_as_ledger_item
belongs_to :user, foreign_key: :sender_id
has_many :line_items, class_name: 'InvoicingLineItem', foreign_key: :ledger_item_id
accepts_nested_attributes_for :line_items
end
Need to make association with user as hidden field
View:
<%= simple_form_for #invoicing_ledger_item do |f| %>
<%= f.association :user, :as => :hidden, :input_html => { :value => curren_user.id } %>
<%= f.button :submit %>
<% end %>
You should use user_id instead of sender_id in the schema first.Then use alias_attribute :user_id, :sender_id in InvoicingLedgerItem model, through this helper method you can assign a new name to the field name.
For more reference visit http://api.rubyonrails.org/classes/Module.html.
I've renamed the SessionsController and Session Model to Periods/Period because it conflicted with Devise and so you'll see this in the update
I have a sessions and events model/controller. When a new session is created, it needs to be associated with a particular event.
In my sessions model, I have an event_id, but I want to have a dropdown on the form that is populated with name of non-past events. Once that is selected, the form should be able to assign the correct event_id to the created session.
What is the correct way to go about doing this?
Here is my schema.rb to help you get a clearer picture of what the Models look like:
ActiveRecord::Schema.define(:version => 20120807154707) do
create_table "events", :force => true do |t|
t.string "name"
t.date "date"
t.string "street"
t.string "city"
t.string "state"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "sessions", :force => true do |t|
t.string "name"
t.integer "event_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "admin", :default => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
end
Here is my form:
<%= form_for(#period) do |f| %>
<%= f.label :Name %>
<%= f.text_field :name%>
<%= f.label :Event %>
<%= f.collection_select(:period, :event_id, Event.all, :id, :name)%>
<%= f.label :time %>
<%= f.text_field :time, id: "timepicker" %>
<%= f.submit "Create Event" %>
<% end %>
and I keep getting the following error: undefined methodmerge' for :name:Symbol`
Breaking out the various arguments of collection select: f.collection_select(:period, :event_id, Event.all, :id, :name)
:period -> The object
:event_id -> the method I want to set on the object.
Event.All -> The collection (for now I'll take all of them)
:id -> the value of the html element option
:name -> the value displayed to the user
Am I doing that correct?
To display a select menu with options from another model (not another controller), try collection_select.
In your new Session form, this might look like:
collection_select(:event, :id, Event.where("date > :date", date: Time.now.strftime("%m/%d/%Y"))
In the Sessions controller, in the create action, build the association like this:
#session.event = Event.find(params[:event][:id])
I have found that this structure works for me:
<%= f.label :event %>
<%= f.collection_select :event_id, Event.all, :id, :name, {:prompt=> "Pick an Event"}, {:class => "form-control"} %>
The last bit was the html part which I used to set the Bootstrap class.
:name here could be :date or :street etc...