Rails has_one Association (user,company) - ruby-on-rails

class User < ActiveRecord::Base
has_one :comapny
end
class Comapny < ApplicationRecord
belongs_to :user
end
Hi! i created rails association mentioned above. I want user to create only one company (for example: User with id(1) should only make 1 company only).
But even with has_one i am able to make more then 1 company for user1.
can anyone help?
user_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:edit, :update, :show]
before_action :require_user, only: [:index, :show, :update ]
before_action :require_same_user, only: [:edit, :destroy, :update]
before_action :require_admin, only: [:destroy]
def index
#user = User.all
end
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
session[:user_id] = #user.id
flash[:success] = "Welcome #{#user.username}"
redirect_to user_path(#user)
else
render 'new'
end
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find(params[:id])
if #user.update(user_params)
flash[:success] = "Account was updated"
else
render 'new'
end
end
def show
end
def destroy
#user = User.find(params[:id])
#user.destroy
flash[:danger] = "User is destroyed"
redirect_to root_path
end
private
def user_params
params.require(:user).permit(:username, :email, :password, :role, :first_name, :last_name)
end
def set_user
#user = User.find(params[:id])
end
def require_same_user
if current_user != #user and !current_user.admin?
flash[:danger] = "Not authorized"
redirect_to user_path
end
end
def require_admin
if logged_in? and !current_user.admin?
flash[:danger] = "Not authorized"
redirect_to user_path
end
end
end
**comapny_controller.rb**
class ComapniesController < ApplicationController
before_action :set_comapny, only: %i[ show edit update destroy ]
before_action :require_user, only: [:create]
# GET /comapnies or /comapnies.json
def index
#comapnies = Comapny.all
end
# GET /comapnies/1 or /comapnies/1.json
def show
end
# GET /comapnies/new
def new
#comapny = Comapny.new
end
# GET /comapnies/1/edit
def edit
end
# POST /comapnies or /comapnies.json
def create
#comapny = Comapny.new(comapny_params)
respond_to do |format|
if #comapny.save
format.html { redirect_to comapny_url(#comapny), notice: "Comapny was successfully created." }
format.json { render :show, status: :created, location: #comapny }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #comapny.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comapnies/1 or /comapnies/1.json
def update
respond_to do |format|
if #comapny.update(comapny_params)
format.html { redirect_to comapny_url(#comapny), notice: "Comapny was successfully updated." }
format.json { render :show, status: :ok, location: #comapny }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #comapny.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comapnies/1 or /comapnies/1.json
def destroy
#comapny.destroy
respond_to do |format|
format.html { redirect_to comapnies_url, notice: "Comapny was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comapny
#comapny = Comapny.find(params[:id])
end
# Only allow a list of trusted parameters through.
def comapny_params
params.require(:comapny).permit(:name, :year_founded, :user_id)
end
end
**Coampny_form.html.erb**
<%= form_with(model: comapny, local: true) do |form| %>
<% if comapny.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comapny.errors.count, "error") %> prohibited this comapny from being saved:</h2>
<ul>
<% comapny.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :comapny_name %>
</div>
<div class="field">
<%= form.label :year_founded %>
<%= form.date_select :year_founded, id: :comapny_year_founded %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.text_field :user_id, id: :comapny_user_id, value: current_user.id %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
I made a typo while generating scaffold of coapmny instead of company. ignore that

Add a uniqueness constraint to the db table and a uniqueness validation to the company model:
class Comapny < ApplicationRecord
belongs_to :user
validates :user_id, uniqueness: true
end

Related

No route matches {:action=>"new", :controller=>"profiles", :id=>"address"}, missing required keys: [:user_id]

I work with Device and just added the 'wicked' gem to my Rails app. In my RegistrationsController I have defined the following:
class Users::RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
super
end
def update
super
end
protected
def after_sign_up_path_for(resource)
user_steps_path
end
def after_update_path_for(resource)
new_user_profile_path(current_user.id)
end
end
Basically I wish my form to have one further step after sign up, which would be the address:
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :address
def show
#user = current_user
render_wizard
end
end
After the User has given his address, I want him/her to be redirected to their profile, where they can also give the information about themselves. My thinking, this is an update action in my RegistrationsController. Or how do I redirect to the profile, after my multistep form is finilised, meaning the step address is done? Here is address.html.erb:
<%= form_for #user, url: wizard_path do |f| %>
<div class="field">
<%= f.label :street %>
<%= f.text_area :street %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
For now ActionContoller complains about my header routes and looks for id address and I don't get why... Here is my ProfilesContorller:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def show
#user = User.eager_load(:profile).find(params[:user_id])
#profile = #user.profile
#review = Review.new
#reviews = Review.where(profile: #profile)
end
def new
#user = current_user
#profile = Profile.new
end
def edit
#profile = #user.profile
end
def create
#user = current_user
#profile = #user.build_profile(profile_params)
respond_to do |format|
if #profile.save
format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully created.' }
format.json { render :show, status: :created, location: #profile }
else
format.html { render :new, notice: 'Did not save' }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #profile.update(profile_params)
format.html { redirect_to user_profile_path(current_user.id), notice: 'Profile was successfully updated.' }
format.json { render :show, status: :ok, location: #profile }
else
format.html { render :edit }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
def destroy
#profile.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'Profile was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_profile
#profile = current_user.profile
end
def profile_params
params.permit(:about, :avatar)
end
end
And here is the error part in my header.html.erb
<% if current_user.profile == nil %>
<li><span class="bg-primary text-white rounded"><%= link_to 'Create profile', new_user_profile_path%></span></li>
<% else %>
<li><span class="bg-primary text-white rounded"><%= link_to 'My profile', user_profile_path(#user) %></span></li>
<% end %>
<li><span class="bg-primary text-white rounded"><%= link_to 'Log out', destroy_user_session_path, method: :delete %></span></li>
ActionController basically complains about the second line and the "new_user_profile_path". How did address got into profiles contoller -> new -> id and how do I procees with the error mentioned in the title. Thank you!
As per your Error, user_id is missing in your route
Try, new_user_profile_path(#user) in second line
So finally it should be <%= link_to 'Create profile', new_user_profile_path(#user)%>

Rails: fatal (exception reentered):

I am trying to implement the railscast (http://railscasts.com/episodes/217-multistep-forms?view=asciicast) within a modal window.
I am receiving the fatal (exception reentered) error message after I click submit on my first page. I believe that it is getting stuck somewhere .next_step.
I am using a modal window that calls new.js.erb and . I will add additional code as needed. Sorry for creating the question improperly.
---Code:
Model:
estimate.rb
class Estimate < ApplicationRecord
attr_writer :current_step
has_many :estimateitems
def current_step
#current_step || current_step.first
end
def next_step
self.current_step = steps[steps.index(current_step + 1)]
end
def steps
%w[1st 2nd]
end
end
Views:
new.js.erb
// Add the dialog title
$('#dialog h3').html("<i class='glyphicon glyphicon-plus'></i> New Estimate");
// Render the new form
$('.modal-body').html('<%= j render("clients/partials/form_create_estimate") %>');
// Show the dynamic dialog
$('#dialog').modal("show");
// Set focus to the first element
$('#dialog').on('shown.bs.modal', function () {
$('.first_input').focus()
})
form_create_estimate.html.erb
<%= form_for(#estimate, remote: true, :html => { :role => "form" }, :'data-update-target' => 'update-container') do |f| %>
<% if #estimate.current_step = 1 %>
<%= render '1st_step', :f => f %>
<% elsif #estimate.current_step = 2 %>
<%= render '2nd_step', :f => f %>
<% end %>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
Controllers:
class EstimatesController < ApplicationController
before_action :logged_in_user, only: [:index, :show, :edit, :update, :destroy]
before_action :set_estimate, only: [:show, :edit, :update, :destroy]
def index
#estimates = Estimate.all
end
def show
end
def new
#estimate = Estimate.new
end
def edit
end
def create
#estimate = Estimate.new(params[:estimate_params])
#estimate.next_step
render 'new'
end
def update
respond_to do |format|
if #estiamte.update_attributes(estimate_params)
format.json { head :no_content }
format.js
else
format.json { render json: #estimate.errors.full_messages, status: :unprocessable_entity }
end
end
end
def destroy
#estimate.destroy
respond_to do |format|
format.js
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
def set_estimate
#estimate = Estimate.find_by(id: params[:id])
end
def estimate_params
params.require(:estimate).permit(:nickname, :date)
end
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end

Rails 4: Correct syntax for nested resources - #user or :user?

I am using simple form for a nested resource.
resources :users do
resources :interests
end
I have the following call-backs in my InterestsController
class InterestsController < ApplicationController
before_action :set_interest, only: [:show, :create, :edit, :update, :destroy]
before_action :set_user, only: [:index, :show, :create, :edit, :update, :destroy]
private
# Use callbacks to share common setup or constraints between actions.
def set_interest
#interest = Interest.find(params[:id])
end
def set_user
#user = current_user
end
end
I have a simple form - this works......
<%= simple_form_for [:user, #interest] do |f| %>
<% if #interest.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#interest.errors.count, "error") %> prohibited this response from being saved:</h2>
<ul>
<% #interest.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-inputs">
<%= f.input :user_id %>
<%= f.input :discipline_id %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
But this does not.....
<%= simple_form_for [#user, #interest] do |f| %>
I can find no explanation for this - surely #user in the simple form is the correct was to declare the User?
Here is more information:
This is the error I get for the edit action:
No route matches {:action=>"show", :controller=>"interests", :format=>nil, :id=>nil, :user_id=>#<Interest id: 1, user_id: 1, discipline_id: 10, created_at: "2015-02-22 11:00:14", updated_at: "2015-02-22 11:00:14">} missing required keys: [:id]
Interestingly, the new action works. If I switch the simple form to
<%= simple_form_for [#user, #interest] do |f| %>
The edit action works but the new action does not. So I am guessing this has to be the way I have my InterestsController set up.
Here it is in full.
class InterestsController < ApplicationController
before_action :set_interest, only: [:show, :create, :edit, :update, :destroy]
before_action :set_user, only: [:index, :show, :create, :edit, :update, :destroy]
def index
#users_interests = #user.interests
end
def show
end
def new
#interest = current_user.interests.new
end
def edit
end
def create
#interest = User.find(params[:user_id]).interests.build(interest_params)
#interest.user = current_user
respond_to do |format|
if #interest.save
flash[:success] = "Interest created!"
format.html { redirect_to user_interests_path(current_user) }
format.json { render :show, status: :created, location: #interest }
else
flash[:failure] = "Interest was not added!"
format.html { render :new }
format.json { render json: #interest.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #interest.update(interest_params)
format.html { redirect_to user_interests_path(current_user) }
format.json { render :show, status: :ok, location: #interest }
else
format.html { render :edit }
format.json { render json: #interest.errors, status: :unprocessable_entity }
end
end
end
def destroy
#interest.destroy
respond_to do |format|
format.html { redirect_to interests_url, notice: 'Interest was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_interest
#interest = Interest.find(params[:id])
end
def set_user
#user = current_user
end
# Never trust parameters from the scary internet, only allow the white list through.
def interest_params
params.require(:interest).permit(:user_id, :discipline_id)
end
end
SOLVED:
easy fix - need to set #user = current_user in the new action:
def new
#interest = current_user.interests.new
#user =current_user
end

Facing error while using render in rails

I am having 2 models:
app/models/employee.rb:
class Employee < User
has_many :insurances
end
app/models/insurance.rb:
class Insurance < ActiveRecord::Base
belongs_to :employee
end
app/controllers/employees_controller.rb:
class EmployeesController < ApplicationController
before_action :set_employee, only: [:show, :edit, :update, :destroy]
before_action :employee_params, only: [:create, :update]
# GET /employees
# GET /employees.json
def index
#employees = Employee.all
end
# GET /employees/1
# GET /employees/1.json
def show
#employee = Employee.find(params[:id])
end
# GET /employees/new
def new
#employee = Employee.new
end
# GET /employees/1/edit
def edit
#employee = Employee.find(params[:id])
end
# POST /employees
# POST /employees.json
def create
#employee = Employee.new(employee_params)
respond_to do |format|
if #employee.save
format.html { redirect_to employees_url, notice: "#{#employee.first_name} was successfully created." }
format.json { render :index, status: :created, location: #employee }
else
format.html { render :new }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /employees/1
# PATCH/PUT /employees/1.json
def update
respond_to do |format|
if #employee.update(employee_params)
format.html { redirect_to employees_url, notice: "#{#employee.first_name} was successfully updated."}
format.json { render :index, status: :ok, location: #employee }
else
format.html { render :edit }
format.json { render json: #employee.errors, status: :unprocessable_entity }
end
end
end
# DELETE /employees/1
# DELETE /employees/1.json
def destroy
#employee.destroy
respond_to do |format|
format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_employee
#employee = Employee.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def employee_params
if params[:employee][:password].blank? && params[:employee][:password_confirmation].blank?
params[:employee].delete(:password)
params[:employee].delete(:password_confirmation)
end
params[:employee].permit(:email, :password, :employee_id,:employee_work_id, :first_name, :middle_name, :last_name, :gender, :date_of_birth, :driver_license_no, :driver_license_expiry_date, :martial_status, :nationality, :office_address, :residence_address, :city, :state_province, :zip_code, :country, :work_number, :mobile_number, :home_number, :other_email)
end
end
app/controllers/insurance_controller.rb:
class InsurancesController < ApplicationController
before_action :set_insurance, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#insurances = Insurance.all
respond_with(#insurances)
end
def show
respond_with(#insurance)
end
def new
#insurance = Insurance.new
respond_with(#insurance)
end
def edit
end
def create
#insurance = Insurance.new(insurance_params)
#insurance.save
respond_with(#insurance)
end
def update
#insurance.update(insurance_params)
respond_with(#insurance)
end
def destroy
#insurance.destroy
respond_with(#insurance)
end
private
def set_insurance
#insurance = Insurance.find(params[:id])
end
def insurance_params
params.require(:insurance).permit(:employee_id,:name_of_dependent, :relationship, :name, :of_spouse, :children, :date_of_birth, :policy_number, :policy_provider, :policy_type)
end
end
app/views/insurances/_show.html.erb:
<%= employee.insurances.each do |emp| %>
<p>
<strong>Name of dependent:</strong>
<%= emp.name_of_dependent %>
</p>
<p>
<strong>Name:</strong>
<%= emp.name %>
</p>
<% end %>
When i use link_to with show path, it is working fine.
app/views/employees/show.html.haml:
%p
%strong Title:
= #employee.full_name
%p
%strong Text:
= #employee.gender
%p
= link_to 'Insurance', insurance_path
after accordion code, i used render as follows:
%p
%strong Title:
= #employee.full_name
%p
%strong Text:
= #employee.gender
%p
#accordion2.panel-group
#new-student-widget.panel.panel-default.left-column-entry
.header.panel-heading
.header-content.panel-title
%a#newStudentToggle{"data-parent" => "#accordion2", "data-target" => "#newStudent", "data-toggle" => "collapse"} Insurance Details
#newStudent.panel-collapse.collapse
#newStudentInner.panel-body
= render :template => 'insurances/show', locals: { employee: #employee }
When i am using render in accordion, i am getting error as:
NoMethodError in Employees#show
undefined method `name_of_dependent' for nil:NilClass
Please help me out.
If you are calling show as partial then rename show.html to _show.html
and pass #employeee instance variable which is present in accordion code
= render :template => 'insurances/show', locals: { employee: #employee }
and in _show page
- employee.insurances.each do |ins|
%p.strong Name of dependent:
= ins.name_of_dependent
%p.strong Name:
= ins.name
Pass the insurance object when you are rendering the template :
- #insurances.each do |insurancce|
= render template: 'insurances/show', locals: { insurance: insurance }
- end
Make sure you instantiated the #insurance object in your controller action, where you instantiated the #employee object.
#employee = current_user # or anything else
#insurances = #employee.insurances
Then in your Insurances#show template use insurance instead of #insurance like this :
<p id="notice"><%= notice %></p>
<p>
<strong>Name of dependent:</strong>
<%= insurance.name_of_dependent %>
</p>
<p>
<strong>Name:</strong>
<%= insurance.name %>
</p>

Post and Location Association with ancestry gem

My Rails application have two model. Location and Post, Location have many post.I am Using
ancestry gem.
class Post < ActiveRecord::Base
belongs_to :location, :counter_cache => true
end
class Location < ActiveRecord::Base
include Tree
has_ancestry :cache_depth => true
has_many :posts
end
My Post Controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
#posts = Post.all
end
def show
end
def new
#post = Post.new
end
def edit
end
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #post }
else
format.html { render action: 'new' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:name)
end
end
If i am create new Post with which Location belongs in _form.html.erb
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved: </h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%= select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %>
<div class="actions">
<%= f.submit %>
</div>
Browser show error message which is display bellow
ArgumentError in Posts#new
Not sure if this fixes your error, but:
To make the dropdown working, change the select line to:
<%= f.select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %>
This is because you want the formbuilder f to handle the creation of the form element.
You also have to whitelist the :location_id parameter in the controller:
def post_params
params.require(:post).permit(:name, :location_id)
end

Resources