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
Related
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.
I have database table called managers and here is the corresponding section for schema.rb
create_table "managers", force: true do |t|
t.string "fname"
t.string "lname"
t.string "company"
t.string "phone"
t.string "cell"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "plan_id"
t.string "country"
t.string "address"
t.string "timezone"
t.string "zipcode", default: "0"
t.datetime "open_time"
t.datetime "close_time"
t.integer "shifts"
t.integer "shiftlength"
end
In my controller I am trying to update it by calling the update action
def update
#manager=Manager.find(params[:id])
if #manager.update(manager_params)
redirect_to manager_path(:id=>params[:id])
else
render action: 'edit'
end
end
def manager_params
params.require(:manager).permit :fname,
:lname,
:company,
:country,
:address,
:timezone,
:zipcode,
:phone,
:cell,
:open_time,
:close_time,:shifts,:shiftlength
end
But the update action throws the following error
undefined method `each' for "4":String
The values :shifts and :shiftlength are being taken from number input fields. I tried to convert :shifts and :shiftlengths to integers using to_i but that did not work.
It then gave the error
undefined method `each' for 4:Fixnum
Relevant section of update form
<div class="form-group">
<%=m.label :number_of_shifts%><br />
<%= m.number_field :shifts,:class=>"form-control",:value=>#manager.shifts %>
</div>
<div class="form-group">
<%=m.label :shift_length%><br />
<%= m.number_field :shiftlength,:class=>"form-control",:value=>#manager.shiftlength %>
</div>
I have 3 tables- OwnerofProperty , Property and Ticket. I want to make a form using form_for to represent property booking; can I make a form to retrieve data from Property where the submit button saves the data in the Ticket table? I am asking because I have no idea if that can be possible or how to make it.
Note: I have only created the relations :
OwnerofProperty one-to-many Property
Property one-to-one Ticket
I need this form just to make a user able to see the avaliable properties and can book only one , how to make this form ?
Schema.rb for the three models :
create_table "owners", :force => true do |t|
t.string "f_name"
t.string "l_name"
t.string "address"
t.string "tel_no"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "properties", :force => true do |t|
t.string "p_street"
t.string "p_city"
t.string "postcode"
t.string "property_type"
t.integer "rooms"
t.integer "rent"
t.integer "owner_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "properties", ["owner_id"], :name => "index_properties_on_owner_id"
create_table "tickets", :force => true do |t|
t.string "city"
t.string "street"
t.string "ticket_type"
t.integer "rooms"
t.integer "rent"
t.integer "property_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "tickets", ["property_id"], :name => "index_tickets_on_property_id"
Yes, it is possible.
let's see ticket_controller.rb
def new
#property = Property.find 20 #20 is property id
#properties = Property.all
##ticket = Ticket.new
end
now in view (where you want to create form):
<%= form_for #ticket do |f| %>
<%= f.select :property_id, #properties.collect {|p| [ p.name, p.id ] }%> <!-- just an example, Ticket model has a field named "property_id" -->
<%= f.submit %>
<%= end %>
this form submits to create action of ticket_controller. And you are able to get all data as params and save it to table.
def create
#ticket = Ticket.new(params[:ticket])
#ticket.save
respond_to do |format|
format.html{redirect_to( your_desired_path)}
end
end
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...
I keep getting this exception: "SQLite3::SQLException: no such column: books.user_id: SELECT "books".* FROM "books" WHERE ("books".user_id = 4)". Which sounds like there is no user_id in the books table.
So I just installed the Foreigner plugin and added "t.integer :user_id, :null => false" and "add_foreign_key(:books, :users)" in the book migration file. I ran "rake db:migrate", but still it is giving me the same exception.
I am using Rails 3 in Windows and Devise to authenticate user.
HOME VIEW
<p><%= link_to "Add new Book",:controller =>"book", :action => 'new' %></p>
<% #books.each do |b| %>
<p><%= b.author%></p>
<p><%= b.title%></p>
<%end%>
HOME CONTROLLER
class HomeController < ApplicationController
def index
#user = current_user
#user.books||=Book.new
#books=#user.books
end
end
BOOK CONTROLLER
class BookController < ApplicationController
def new
#books = Book.new
# redirect_to :controller=>"home" ,:action=>"index"
end
def create
#books = Book.new(params[:book])
if #books.save
render "home/index"
#redirect_to :controller=>"home" ,:action=>"index"
else
render :action => 'new'
end
end
CREATE TABLE/BOOK MIGRATION
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.text :title
t.text :author
t.integer :user_id, :null => false
t.timestamps
end
add_foreign_key(:books, :users)
end
BOOK VIEW
<h1>Book#new</h1>
<%= form_for(:book) do |f| %>
<p><%= f.text_field :title %></p>
<p><%= f.text_field :author %></p>
<p><%= f.submit "Add book"%>
BOOK MODEL
class Book < ActiveRecord::Base
belongs_to :user
end
USER MODEL
class User < ActiveRecord::Base
has_many :books
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation,:firstname,:lastname,:school,:major,:sex,:zipcode
end
ROUTE
Campus::Application.routes.draw do
get "book/index"
get "book/edit"
get "book/new"
get "home/edit"
devise_for :users
resources :book
root :to=> "home#index"
match '/book/new' =>"home#index"
end
DATABASE SCHEMA
ActiveRecord::Schema.define(:version => 20110609055608) do
create_table "books", :force => true do |t|
t.text "title"
t.text "author"
t.integer "user_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "courses", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "strong_ins", :force => true do |t|
t.string "subject"
t.string "topic"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.string "remember_token"
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"
t.datetime "updated_at"
t.string "firstname"
t.string "lastname"
t.text "school"
t.text "major"
t.string "sex"
t.integer "zipcode"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
create_table "weak_ins", :force => true do |t|
t.string "subject"
t.string "topic"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
end
The user_id column should appear in the schema after running the migration. It's not in your listing, so I'd say that's the problem. Make sure rake db:migrate is completing without errors. You can redo the migration with rake db:rollback && rake db:migrate, if necessary.