I have 2 models named Subject & Page. there is one to many association between them.
class Subject < ActiveRecord::Base
has_many :pages
attr_accessible :name
attr_accessible :position
attr_accessible :visible
attr_accessible :created_at
end
and
class Page < ActiveRecord::Base
belongs_to :subject
attr_accessible :subject_id
attr_accessible :name
attr_accessible :permalink
attr_accessible :position
attr_accessible :visible
attr_accessible :created_at
end
I have list.html.erb in view -> pages folder.
Q. My qus is I want to show all the subject_id in list.html.erb. how?
for that which changes I have to do in pages_controller & list.html.erb so that I will got solution...
You can access any Model in any Controller in following way
#instance_variable = ModelClass.all
In your case it should be something like following
pages_controller.rb
def list
#subjects_list = Subject.all
end
app/views/pages/list.html.erb
<% for subject in #subjects_list %>
<!-- Your Code Here -->
<% end %>
Related
I am building a simple booking application for learning rails. I scaffolded people, cars and bookings.
Now when I try to create bookings I get
2 errors prohibited this booking from being saved:
Person must exist
Car must exist
Code
car.rb
class Car < ApplicationRecord
has_many :bookings
def brand_with_licence_plate
"#{brand} #{licence_plate}"
end
end
person.rb
class Person < ApplicationRecord
has_many :bookings
def complete_name
"#{first_name} #{last_name}"
end
end
bookings.rb
class Booking < ApplicationRecord
belongs_to :Person
belongs_to :Car
end
I added the columns for ID like so:
class AddItemsToBookings < ActiveRecord::Migration[6.1]
def self.up
add_column :bookings, :car_id, :integer
add_column :bookings, :person_id, :integer
end
end
I added to following to _form.html.erb
<div class="field">
<%= form.label :Person %>
<%= form.collection_select(:person_id, Person.all, :id, :complete_name) %>
</div>
<div class="field">
<%= form.label :Car %>
<%= form.select :car_id, options_from_collection_for_select(Car.all, 'id','brand_with_licence_plate') %>
</div>
and added to bookings_controller.rb
def booking_params
params.require(:booking).permit(:start, :end, :person_id, :car_id)
end
I have looked here and tried to change to <%= form.select :car_id, options_from_collection_for_select(Car.all, 'id', 'brand_with_licence_plate') %> as stated in one answer, but giving me the same error.
When I look at the documentation everything seems to be fine as well.
It seems as if am missing something fundamental here. Any ideas how to fix this are appreciated.
Update:
I deleted the integer id column and run a new migration
class AddReferencesToBookingForPersonAndCar < ActiveRecord::Migration[6.1]
def change
add_reference :bookings, :people, foreign_key: true
add_reference :bookings, :cars, foreign_key: true
end
end
and adjusted the permissions for params as well.
You code doesn't look ok to me. I would change it like:
class Booking < ApplicationRecord
belongs_to :person
belongs_to :car
end
Mainly, person and car are lower case. Also, I noticed that in your migration you create cars, in plural. It should be:
class AddReferencesToBookingForPersonAndCar < ActiveRecord::Migration[6.1]
def change
add_reference :bookings, :person, foreign_key: true
add_reference :bookings, :car, foreign_key: true
end
end
belongs_to :car expects a car_id and it looks like that the migration is creating a cars_id instead.
It would be easier to provide more help if you post the full controller code.
Can't find a solution a week along, please help
Here I have 2 models
Item.rb
class Item < ActiveRecord::Base
has_one :specs_group, class_name: Spec, :dependent => :nullify
attr_accessible :specs_group_attributes
accepts_nested_attributes_for :specs_group, update_only: true
end
Spec.rb
class Spec < ActiveRecord::Base
belongs_to :item
attr_accessible :item_id
end
Here is admin/item.rb
ActiveAdmin.register Item do
form do |f|
f.inputs "Specs group" do
f.has_many :specs_group do |s|
s.input :title, as: :select, collection: Spec.roots.map { |a| [a.title] }
end
end
f.actions
end
end
I want to choose element from Spec collection and update corresponding Spec item. With code above I can choose from dropdown list, but it updates Spec's title or creates new. It's a Rails 3.2 app.
I have one project for school and I am little bit confused how to make tag and category asociated posts so when I was looking for some tips in google I found this thread. So I tried scaffolding as described and it was working just fine, but when I ran the server and tried to create new post this appeared:
ActiveModel::MassAssignmentSecurity::Error in PostsController#create
Can't mass-assign protected attributes: category, user
So I really don't know what is wrong but I can use some help. Or maybe there can be suggested another way, mabe simpler how to scaffold posts with tags and categories.
Thank you very much
Here are the models:
class Post < ActiveRecord::Base
belongs_to :category
belongs_to :user
attr_accessible :body, :title, :category, :user
end
class Category < ActiveRecord::Base
attr_accessible :name
end
class Serie < ActiveRecord::Base
attr_accessible :name, :website
end
class Tag < ActiveRecord::Base
attr_accessible :name
end
class TagsSerie < ActiveRecord::Base
belongs_to :serie
belongs_to :tag
# attr_accessible :title, :body
end
class TagsPost < ActiveRecord::Base
belongs_to :post
belongs_to :tag
# attr_accessible :title, :body
end
class User < ActiveRecord::Base
attr_accessible :email, :password
end
Add attr_accessible in your post model:
class Post < ActiveRecord::Base
attr_accessible :category_id, :user_id, :other_attributes_from_post_model
end
Try setting attr_accessible :category_id, :user_id in your post model.
By default, Rails creates the scaffolded models with all its attributes non-accessible, so they are not available to edit by an external user.
So, when you tried to create a new Post, the error message raised, as category and user are protected attributes of Post.
You should review your app/models/post.rb and the rest of your models in the same folder to define as accessible those attributes that should be editable by an external user (a web user, for instance).
class Post < ActiveRecord::Base
attr_accessible :category_id, :user_id
end
On the other hand, the so accessible attributes are not protected any more for external edition so you should not use attr_accessible for all of them but just for ones that you will really allow to be modified externally.
I have two models, subject and page. I created a one-to-many association between them.
class Subject < ActiveRecord::Base
has_many :pages
attr_accessible :name
attr_accessible :position
attr_accessible :visible
attr_accessible :created_at
end
and
class Page < ActiveRecord::Base
belongs_to :subject
attr_accessible :subject_id
attr_accessible :name
attr_accessible :permalink
attr_accessible :position
attr_accessible :visible
attr_accessible :created_at
end
As mentioned above, I have two models, and I want to access all subject names which are in the Subject model to the page model/controller...
Give this a try:
Subject.select("subjects.name").joins(:pages).uniq
If you want to get all the subjects saved in a subject model use:
Subject.pluck(:name)
or to access a subjects associated with particular page:
page = Page.first
page.subject #subject object associated with the page
Below is how you can access subject's pages or page's subject.
s = Subject.create(<params>)
s.pages # array of page objects
p = Page.create(<params>)
p.subject # subject object
I am having trouble with some theory.
I have a model called Promos, and I also have a model called Categories.
I want the admin to be able to create set Categories from which the users will select in a dropdown to assign the Promo. So Promos will belong to a Category but the assignment ought to happen in the create.
What is the recommended structure?
To ensure that every Promo has a Category:
class Category < ActiveRecord::Base
has_many :promos
end
class Promo < ActiveRecord::Base
belongs_to :category
validates_association_of :category
end
How to set the Category at Promo creation time
promo = Promo.new(:category => #category)
As far as forms go:
<% form_for :promo do |f| %>
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => "Choose a category" %>
...
Other promo fields
...
<% end %>
Matching controller code:
class PromosController < ActionController
def create
#promo = Promo.create(params[:promo])
...
redirect or render whether #promo was successfully created
...
end
end
A user has_many a promos, which belongs to a category.
A category has_many promos.
Such as:
class User < Activerecord::Base
has_many :promos
class Promo < Activerecord::Base
belongs_to :user
belongs_to :category
class Category < Activerecord::Base
has_many :promos