Rails schema scaffolding - ruby-on-rails

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.

Related

creating object with nested attributes

i need to create a form, which will create object which has another two objects as attributes, but those objects should be available from a dropdown list that contains templates of those objects.
class User < ActiveRecord::Base
accepts_nested_attributes_for :adresses, :profiles
end
class Address < ActiveRecord::Base
attr_accessible :city, :country
belongs_to :user
end
class Profile < ActiveRecord::Base
attr_accessible :nickname, :password
belongs_to :user
end
tricky part might be, that User has no column 'address_id' or 'profiles_id', everything should go to the Profile and Address, which are being created in the same moment as the User (they have the same attributes as their templates)
I could really use some help, dont expext full code solution, but some hints would be nice
Try this setup:
class User < ActiveRecord::Base
has_one :address
has_one :profile
accepts_nested_attributes_for :address, :profile
attr_accessible :adress_attributes, :profile_attributes
end
class Address < ActiveRecord::Base
attr_accessible :city, :country
belongs_to :user
end
class Profile < ActiveRecord::Base
attr_accessible :nickname, :password
belongs_to :user
end
See doc

Ruby on Rails: Possible/preferrable to implement single table inheritance after creating separate models?

I've built a simple address book application in my intro Ruby on Rails class with separate models for street addresses (Address), email addresses (Email), and web addresses (Web) using nested forms using a single controller (Entry). I'd like to now change the last two models (Email and Web) to use single table inheritance from a base Url table. Is that preferable (or even possible) compared to rebuilding the app from scratch with the correct inheritance relationships?
I've included my existing models below:
class Entry < ActiveRecord::Base
attr_accessible :first_name, :last_name, :addresses_attributes, :webs_attributes, :emails_attributes
has_many :addresses, dependent: :destroy
has_many :emails, dependent: :destroy
has_many :webs, dependent: :destroy
accepts_nested_attributes_for :addresses, :emails, :webs, allow_destroy: true, reject_if: :all_blank
end
class Address < ActiveRecord::Base
belongs_to :entry
belongs_to :address_type
attr_accessible :address_type_id, :city, :state, :street, :zip
end
class Email < ActiveRecord::Base
belongs_to :entry
belongs_to :address_type
attr_accessible :address_type_id, :email, :entry_id
validates_email_format_of :email
end
class Web < ActiveRecord::Base
belongs_to :entry
belongs_to :address_type
attr_accessible :address_type_id, :web, :entry_id
end
How would changing
class Email < ActiveRecord::Base
and
class Web < ActiveRecord::Base
to
class Email < Url
and
class Web < Url
affect my existing application?
Thanks in advance for your help and advice.
Be sure to also add the Url class that's inheriting the ActiveRecord::Base class.
class Url < ActiveRecord::Base
belongs_to :entry
belongs_to :address_type
attr_accessible :address_type_id :entry_id
end
class Email < Url
attr_accessible :email
validates_email_format_of :email
end
class Web < Url
attr_accessible :web
end
also add the extra line to your entry.rb:
has_many :urls, dependent: :destroy
It is possible to generate a migration that sets up the single table inheritance, but I was unfortunately not able to do this successfully without breaking other things in my app. I went ahead and restarted fresh with a new application and correctly implemented the proper inheritance. This was in the interests of time and the practice of building an application from scratch. In a real-world environment, I'm sure it would be worth investing the time to create the proper migration and changes to the various dependencies.
Thanks Zippie for your advice. I appreciate it.

Something seems to be broken in my belongs_to, has_one relationship

I have a data model that looks like this:
A customer has subscription_id and setup_id as parameters. In some cases, customer will only have one of the parameters. In other cases, it will have both.
Currently, if I make a new customer through either the subscriptions flow or the setups flow, either Subscription.last or Setup.last will reflect the most recent customer that was created (with customer_id equalling the last customer created)
However, I am having the problem of Customer.setup_id or Custumer.subscription_id being nil in all cases.
Here's my code from both subscription.rb and setup.rb:
class Subscription < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
class Setup < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
And in customer.rb:
class Customer < ActiveRecord::Base
attr_accessible :email, :name, :stripe_token, :subscription_id, :setup_id, :phone, :plan
has_one :subscription
has_one :setup
end
I'm not sure what I'm doing incorrectly here but I'd love it if the three data models could talk to each other correctly.
Edit: Is it bad that both setup and subscription belong to :user rather than :customer?
Edit 2: Updated the code of setup.rb and subscriptions.rb to correctly reflect the data model currently. And customer.rb is still not recognizing the correct setup_id or subscription_id
class Subscription < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
class Setup < ActiveRecord::Base
attr_accessible :status, :customer_id
belongs_to :customer
end
class Customer < ActiveRecord::Base
attr_accessible :email, :name, :stripe_token, :phone, :plan
has_one :subscription
has_one :setup
end
customer = Customer.first
customer.subscription # Instance of Subscription that belongs to customer
customer.setup # Instance of Setup that belongs to customer

Ruby on Rails model/controller access

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

Can't figure out associations in Rails_Admin

I have two models like so:
class Kid < ActiveRecord::Base
belongs_to :sex
attr_accessible :name
end
class Sex < ActiveRecord::Base
attr_accessible :description
has_many :kids
end
But for the life of me, I cannot figure out how to get the association to show up in the admin. When I go to edit a kid, I see a label for sex, but there is no dropdown, no hint whatsoever that RailsAdmin sees the association. It just shows the label name, a blank space and the word "optional" below.
I've searched through the dox over and over and over, yet I can't find a solution. I'm a noob, so it's possible I looked right over it and should be subject to ridicule.
I have not modified any other admin code.
The relation should be accessible in Kid, try to add sex_id to accessible attributes.
class Kid < ActiveRecord::Base
belongs_to :sex
attr_accessible :name, :sex_id
end
Thanks to Gaƫl Marziou, I figured out how to 'connect' with the intermediate model (the one used with has_many :through... association) like so:
class CategoryPets < ActiveRecord::Base
belongs_to :category
belongs_to :pet
attr_accessible :category_id, :pet_id
end

Resources