Email attribute is invisible, in rails 4 with strong paramaters - ruby-on-rails

In my users controller i hav defined-
def user_params
params.require(:user).permit(:name,:email,:password,:password_confirmation)
end
In ticket template
Created by <%= #ticket.user.email %>
when I write ticket.user.name , it displays the name but when i write email, its invisible.
class Ticket < ActiveRecord::Base
belongs_to :project
belongs_to :user
end
class Project < ActiveRecord::Base
has_many :tickets, dependent: :destroy
validates :name, presence: true
end

I assume the email didn't get saved? Go to your Command Line, type rails c, then find that user..maybe User.first if it's the first user...tell me if that user has an email attached to it

Related

find_or_create on a has many through relationship

I have a has many through relationship in my app:
Shows has many Bands through => Lineups
Bands are unique by :name
class Show < ActiveRecord::Base
attr_accessible :city_id, :title, :dateonly, :timeonly, :image, :canceled, :venue_attributes, :bands_attributes
belongs_to :city
belongs_to :venue
has_many :lineups
has_many :bands, through: :lineups
has_and_belongs_to_many :users
end
class Lineup < ActiveRecord::Base
belongs_to :show
belongs_to :band
end
class Band < ActiveRecord::Base
attr_accessible :name, :website, :country, :state
has_many :lineups
has_many :shows, through: :lineups
validates :name, presence: true
validates_uniqueness_of :name
before_save :titleize_name
private
def titleize_name
self.name = self.name.titleize
end
end
New Bands are created like this:
(lets say we have a show record already saved called s1)
> s1.bands.new(name: "Wet Food")
> s1.save
Right now this will only save if a band named "Wet Food" doesn't already exist
In which model is the best place to do a Band.find_or_create in this relationship so that an existing band can be used if one with the same name exists?
This is generally the type of call that would go in a Controller (or maybe a service object), but not in a Model. It really depends on the particular user flow that you're trying to accomplish in your app. Basically, where ever you are already using s1.bands.new, you could use this instead :
s1.bands.where(name: 'Wet Food').first_or_create

Rails has_many through validation got skipped

I'm feeling a little bit dumb to ask this, but I've been Googling my a*# off.
Well I have the following models:
class Company < ActiveRecord::Base
has_many :employments
has_many :users, through: :employments
validates_presence_of :name
validates_presence_of :description
validates_numericality_of :zip, only_integer: true
validates_presence_of :email
validates_presence_of :street
validates_presence_of :city
validates_presence_of :country
validates_presence_of :telephone
end
class Employment < ActiveRecord::Base
belongs_to :user
belongs_to :company
end
class User < ActiveRecord::Base
has_many :employments
has_many :companies, through: :employments
end
Important here is the company-Model which has some validations.
Now, I have the following Controller to create a new Company:
class CompaniesController < ApplicationController
def create
#company = Company.new(company_params) # The params were set with a private Method
#employment = #company.employments.build(user: current_user, is_admin: true)
if #employment.save
redirect_to :back, flash: { success: 'Success' }
else
#title = 'Create a new company'
render :new
end
end
end
The Problem is, that when I leave the companies-Fields blank, the company gets not created, but the employment-Model gets persistet in the Database.
I believe It has something to do with the Company.new()-Call I have to check, if the #company-Model gets created first, before the #employment-Model gets created.
How can I achieve that the validation gets tested first?
Thank you very much!
To validate associated object you need to use validates_associated. Please note the "Warning" and "Note" in the linked api document.
Try:
class Employment < ActiveRecord::Base
belongs_to :user
belongs_to :company
validates_associated :company
end
in addition to vinodadhikary's answer, you can also try saving the company. so instead of #employment.save, use #company.save. That should also save #employment when #company passes validations.

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

Rails schema scaffolding

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.

Rails Nested Form Attributes Building

I'm having real difficulty understanding how to go about a nested form that I have. A user would sign into the application, click 'create a team' and this page would allow users to enter a team name and a list of members for the team. (effectively creating a team member list).
I have a nested form which contains fields_for memberships so as to create the membership. See screenshot of form
When the form is saved, the membership model runs Entrant.find_or_creates_by_name to create the entrant.
The problem I'm having is that on the creation I get the error messages:
Memberships team can't be blank
How do I prevent this from happening and allow users to add entrants / ensure the membership is created correctly?
Apologies if this has already been answered, (there seems to be many topics on has_many through with nested resources, but none that I could find dealt with my specific issue (I could / seemed to be unclear)
My create action is currently the standard nested form action as follows:
def create
#user = current_user
#team = #user.teams.build(params[:team])
if #team.save
redirect_to(team_url(#team), :notice => "Team was successfully saved")
else
render :action => "new"
end
end
I have the following models:
User Model
class User < ActiveRecord::Base
has_many :teams
end
Team Model
class Team < ActiveRecord::Base
belongs_to :user
has_many :memberships
has_many :entrants, :through => :memberships
attr_accessible :name, :team_type, :website, :memberships_attributes
accepts_nested_attributes_for :memberships, allow_destroy: true
end
Memberships Model
class Membership < ActiveRecord::Base
belongs_to :team
belongs_to :entrant
validates :team_id, presence: true
validates :entrant_id, presence: true
attr_accessor :entrant_name
attr_accessible :entrant_name
def entrant_name
entrant && entrant.name
end
def entrant_name=(name)
self.entrant = Entrant.find_or_create_by_name(name) unless name.blank?
end
end
Entrants Model - This is effectively a member of the team for memberlistings however when a user enters a team they can specify nickname which may change across teams.
class Entrant < ActiveRecord::Base
attr_accessible :name
has_many :memberships
has_many :teams, :through => :memberships
end
I think its a validation error. Try to remove
validates :entrant_id, presence: true
From membership model.

Resources