I'm learning RoR and I'm having trouble getting a value from a related table to display. I've tried the suggestions mentioned here and here but I still haven't gotten it to work.
I have a customer record that has a one-to-many relationship with contacts.
Here are snips of my models:
class Contact < ActiveRecord::Base
belongs_to :customer
class Customer < ActiveRecord::Base
has_many :contacts, dependent: :destroy
and this is my Contacts index.html.erb that is producing the error
<% #contacts.each do |contact| %>
<tr class="<%= cycle("even pointer", "odd pointer") %>">
<td><%= contact.first_name %> <%= contact.last_name %></td>
<td><%= contact.customer.name %></td> <!-- this is the error line -->
<td><%= contact.office_phone %></td>
<td><%= contact.cell_phone %></td>
<td><%= contact.email %></td>
<td>
<% if current_user.admin? %>
<%= link_to "edit", contact, method: :edit %>
<% end %>
</td>
</tr>
<% end %>
The error I get is: undefined method `name' for nil:NilClass
What am I doing wrong? It seems to me like the way I'm referencing the field I'm not actually getting to the data in the Customers table but I'm not really sure. Did I build the relationship correctly in my models?
UPDATE to add controller code
class ContactsController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :new, :create]
def index
#contacts = Contact.paginate(page: params[:page])
end
def show
#contact = Contact.find(params[:id])
end
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
flash[:success] = "Contact created!"
redirect_to #contact
else
render 'new'
end
end
def edit
#contact = Contact.find(params[:id])
end
def update
#contact = Contact.find(params[:id])
if #contact.update_attributes(contact_params)
flash[:success] = "Contact updated"
redirect_to #contact
else
render 'edit'
end
end
private
def contact_params
params.require(:contact).permit(:first_name, :last_name, :email, :address1,
:address2, :office_phone, :cell_phone, :website, :city, :zip, :facebook, :twitter)
end
end
class CustomersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :new, :create]
def index
#customers = Customer.paginate(page: params[:page])
end
def show
#customer = Customer.find(params[:id])
end
def new
#customer = Customer.new
end
def create
end
def edit
#customer = Customer.find(params[:id])
end
def update
#customer = Customer.find(params[:id])
if #customer.update_attributes(customer_params)
flash[:success] = "Customer updated"
redirect_to #customer
else
render 'edit'
end
end
private
def customer_params
params.require(:customer).permit(:name, :email, :address1,
:address2, :phone, :fax, :website, :city, :zip, :facebook, :duns_number)
end
end
EDIT:
This is my contact.html.erb form.
<%= form_for #contact, html: { class: "form-horizontal form-label-left" } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<%= f.label :first_name, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= f.text_field :first_name, class: 'form-control col-md-7 col-xs-12' %>
</div>
</div>
<div class="form-group">
<%= f.label :last_name, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= f.text_field :last_name, class: 'form-control col-md-7 col-xs-12' %>
</div>
</div>
<div class="form-group">
<%= f.label :customer_id, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= select_tag :customer_id, options_for_select(Customer.all.collect {|c| [ c.name, c.id ] }), class: 'form-control' %>
</div>
</div>
<div class="form-group">
<%= f.label :office_phone, nil, class: 'control-label col-md-3 col-sm-3 col-xs-12' %>
<div class="col-md-6 col-sm-6 col-xs-12">
<%= f.text_field :office_phone, class: 'form-control col-md-7 col-xs-12' %>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<%= f.submit "Save changes", class: "btn btn-primary" %>
</div>
</div>
<% end %>
You are referencing it correctly.
It looks like your contact has not yet been saved to the database. This would cause the relation to not yet exist.
You would might need to be creating the relationship differently.
I usually do something like:
customer = Customer.create
customer.contacts << Contact.create({first_name: 'John', last_name: 'Smith'})
which creates the associations and commits to the DB right away. Then, in the same request, your contact's customer will be set and accessible.
You could also do this, but it's a little redundant:
customer = Customer.create
customer.contacts << Contact.create({first_name: 'John', last_name: 'Smith', customer: customer})
EDIT
It seems that perhaps you need to be assigning a customer to the contact when it is created/updated. In your Contact form, you'll likely need something like this (seeing your existing forms would help here):
<%= select_tag :customer_id, options_for_select(Customer.all.collect {|c| [ c.name, c.id ] }) %>
or (using simple_form):
f.input :customer_id, collection: Customer.all, selected: #contact.customer_id
# or more simply
f.association :customer
Or maybe a hidden form field if you know the Customer when building the form:
f.input :customer_id, as: :hidden, value: #customer.id
Then in your controller, add customer_id to the contact_params like this:
def contact_params
params.require(:contact).permit(
:first_name, :last_name, :email, :address1,
:address2, :office_phone, :cell_phone,
:website, :city, :zip, :facebook, :twitter,
:customer_id
)
end
Try .try() activesupport method:
<td><%= contact.customer.try(:name) %></td>
If contact.customer is nil (Contact doesn't belongs_to Customer), :name method wouldn't be invoked.
UPDATED Another hints:
a. That's a bad thing that you have a contact without customer. Consider to add this to your model:
class Contact < ActiveRecord::Base
...
validates :customer, presence: true
b. You can replace code like that:
<tr class="<%= cycle("even pointer", "odd pointer") %>">
with
<%= content_tag :tr, class: cycle('event pointer', 'odd pointer') %>
c. Replace this:
<%= contact.email %>
with this:
<%= mail_to contact.email, contact.customer.try(:name) %>
Generally this can help
For example, you have 2 tables City and Post, each post has city_id and you want to display in the views the name of the city associated with the post, you should call in the views the bloc then the name of the table then the name of column like that:
<% #post.each do |post| %>
<%= post.city.name %
<% end %>
Related
I am stuck in a situation where I need to give prefilled data for the fields_for with an empty form. Let me explain with an example
Association
class User < ApplicationRecord
has_one :account, foreign_key: :user_id
accepts_nested_attributes_for :account
end
Class Account
belongs_to :user
end
Now I have a form on the dashboard index page.
<%= form_for #account, :url => account_path, html: { class: "abc" } do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.fields_for :user do |user| %>
<div class="medium-4 columns">
<label>First Name</label>
<%= user.text_field :first_name , class: 'xyz', data: {input: 'someinput'} %>
</div>
<div class="medium-4 columns">
<label><b>Last Name<span class="invalid_message"> This field is required</span></b></label>
<%= user.text_field :last_name, class: 'xyz', data: {input: 'someinput'} %>
</div>
<% end %>
<div class="medium-4 medium-offset-2 columns">
<label>Phone Number</label>
<%= f.text_field :phone_number, class: 'xyz', data: {input: 'someinput'} %>
</div>
<% end %>
Controller
class AccountsController < ApplicationController
def create
#account = Account.new(account_params)
if #account.save
render json: {status: 'successfull'}
else
render json: {error: "#{#account.errors.full_messages}"}, status: 400
end
end
private
def account_params
params.require(:account).permit(:phone_number, :user_id, user_attributes: [:first_name, :last_name])
end
end
class DashboardController < ApplicationController
def index
##I will always have current_user. For the account it will be blank for the first time
if current_user.account.blank?
#account = Account.new
else
#account = current_user.account
end
end
end
Parameters
{"utf8"=>"✓", "authenticity_token"=>"asdasdadadadadsadadadadadQpy0tA82asdaalAgJsUcNk1i/kGETfZqnuQA==", "account"=>{"user_id"=>"123", "user"=>{"first_name"=>"sd", "last_name"=>"ad"}, "phone_number"=>"1212"}}
There are two issues
1) First and Last name are not getting prefilled
2) Params are not going correctly. It should be account_attributes instead of account in the parameter.
Note: In my case, #account will be blank for the first time but still user object(which is the current user) has first_name and last_name already wHich I need to prefill in the fields_for. Also, I need a way to update the first and last name
Can anyone tell me where I am doing wrong
Change #account = Account.new for #account = current_user.build_account. You should see the fields prefilled.
Finally, I found the solution. I was doing a couple of things wrong
1) In form_for it should be #user instead of #account
2) Then in the controller this form will always send it to update action instead of create. The reason is I will always have current_user so rails will automatically check if the object(current_user) exists so instead of sending to create it will send to update action
3) Lastly, when working with one to one assocation we need to load the parent object and build the child object.
<%= form_for #user, :url => user_path, html: { class: "abc" } do |f| %>
<div class="medium-4 columns">
<label>First Name</label>
<%= f.text_field :first_name , class: 'xyz', data: {input: 'someinput'} %>
</div>
<div class="medium-4 columns">
<label><b>Last Name<span class="invalid_message"> This field is required</span></b></label>
<%= f.text_field :last_name, class: 'xyz', data: {input: 'someinput'} %>
</div>
<%= f.fields_for :account do |account_info| %>
<div class="medium-4 medium-offset-2 columns">
<label>Phone Number</label>
<%= account_info.text_field :phone_number, class: 'xyz', data: {input: 'someinput'} %>
</div>
<% end %>
<% end %>
class UsersController < ApplicationController
def update
if current_user.update(account_params)
render json: {status: 'successfull'}
else
render json: {error: "#{#account.errors.full_messages}"}, status: 400
end
end
private
def account_params
params.require(:account).permit(:first_name, :last_name, account_attributes: [:phone_number])
end
end
class DashboardController < ApplicationController
def index
##I will always have current_user. For the account it will be blank for the first time
if current_user.account.blank?
#account = current_user
#account.build_account
end
end
end
I am working on a form for a editorial calendar app. I have two things going out that are pretty similar and not working.
Working with 3 models: Platforms, Posts and Calendars. They are join tables. Platform <=> Post, Post <=> Calendars
Post/new & Post/edit form:
<div class="container">
<div class="form-field">
<%= form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title, required: true %> <br>
Title is required.
</div>
<div class="form-field">
<%= f.label :content%>
<%= f.text_area :content%>
</div>
<div class="form-field">
<%= f.label :link %>
<%= f.text_field :link %>
</div>
<div class="file-field">
<%= f.label :picture%>
<%= f.file_field :picture, id: :post_picture%>
</div>
<div class="file-field">
<%= f.label :finalized %>
<%= f.radio_button :finalized , true%>
<%= f.label :finalized, "Yes" %>
<%= f.radio_button :finalized, false %>
<%= f.label :finalized, "No" %>
</div>
<%= f.hidden_field :user_id %> <br>
<div class="form-field">
<%= f.fields_for :platform_attributes do |platform| %>
<%= platform.label :platform, "Social Platforms"%>
<%= platform.collection_check_boxes :platform_ids, Platform.all, :id, :name %> <br> <br>
</div>
<div>
<h4> Or Create a new platform: </h4>
<%= platform.label :platform, 'New Platform'%>
<%= platform.text_field :name%> <br> <br>
</div>
<% end %>
<%= f.submit%>
<% end %>
</div>
My post controller is handling the checkboxes issue, and the "schedule post" issue. It will only allow me to schedule for one calendar, and it does not save the updates and add additional calendars.
Posts Controller:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :schedule_post, :destroy]
def new
#posts = current_user.posts.select {|p| p.persisted?}
#post = current_user.posts.build
#platforms = Platform.all
end
def edit
#calendars = current_user.calendars
#platforms = Platform.all
end
def create
#post = current_user.posts.build(post_params)
if #post.save
redirect_to post_path(#post)
else
redirect_to new_post_path
end
end
def update
#post.update(post_params)
if #post.save
redirect_to post_path(#post), notice: 'Your post has been updated.'
else
redirect_to edit_post_path(#post)
end
end
def schedule_post
#calendar_post = CalendarPost.new(calendar_post_params)
if #calendar_post.save
binding.pry
redirect_to post_path(#post)
else
render 'show'
end
end
private
def set_post
#post = Post.find(params[:id])
end
def set_calendars
#calendars = current_user.calendars
end
def post_params
params.require(:post).permit(:title, :content, :link, :finalized, :picture, :user_id, :platform_attributes => [:platform_ids, :name])
end
def calendar_post_params
params.require(:calendar_post).permit(:post_id, :calendar_id, :date, :time)
end
end
I want the user to be able to add a post to multiple platforms and multiple calendars because of the versatility of what someone may need.
I also have my setter in my Post model.
class Post < ApplicationRecord
has_many :calendar_posts
has_many :calendars, through: :calendar_posts
has_many :platform_posts
has_many :platforms, through: :platform_posts
belongs_to :user
def platform_attributes=(platform_attributes)
if platform_attributes['platform_ids']
platform_attributes.platform_ids.each do |id|
platform = Platform.find(id: id)
self.platforms << platform
end
end
if platform_attributes['name'] != ""
platform = Platform.find_or_create_by(name: platform_attributes['name'])
self.platforms << platform
end
end
thoughts? why are they not saving to more than one calendar or more than one platform if they choose to have more than one?
Here is the updated code... and more of what I know about these changes and what is happening.
My submit button is not working for some odd reason on my form, so I'm trying to get the params submitted but it won't even route to give me params even if I raise them, nothing is happening.
On the form you can choose checkboxes or add in a platform. If you add in a platform it creates that one but it does not also save the other ones you selected. If you go to edit the post, and click submit with changes, no page loads at all and nothing is happening in log. It's just idle.
<%= f.fields_for :platform_attributes do |platform| %>
assumes you are creating one platform... it says "these are the fields for this platform"
but platform_ids is intended to be a selection of a set of platforms... and probably should be outside of the fields_for section (which should only surround the name field).
try something like the following:
<div class="form-field">
<%= f.label :platform_ids, "Social Platforms"%>
<%= f.collection_check_boxes :platform_ids, Platform.all, :id, :name %> <br> <br>
</div>
<div>
<%= f.fields_for :platform_attributes do |platform| %>
<h4> Or Create a new platform: </h4>
<%= platform.label :name, 'New Platform'%>
<%= platform.text_field :name%> <br> <br>
<% end %>
<%# end fields_for %>
</div>
Also you'll need to update permit/require appropriately eg
def post_params
params.require(:post).permit(:title, :content, :link, :finalized, :picture, :user_id, :platform_ids, :platform_attributes => [:name])
end
Note: not tested - bugs are left as an exercise for the reader ;)
I am new to develop rails project, I have a nested form for submitting Event and Ticket by using gem 'cocoon'.And the form have some check_boxes .
When you submit new , it does not matter, but in edit, you can not put checked in my checkboxes.
How do i do that given my code below?
My error
undefined method `include?' for nil:NilClass
<%= f.check_box :payment_type, { multiple: true ,checked:f.object.payment_type.include?(pay), include_hidden: false }, pay %>
<%= f.label :payment_type, pay, :value => i %>
My Model
class Event < ActiveRecord::Base
has_many :tickets, inverse_of: :event
accepts_nested_attributes_for :tickets, allow_destroy: true
class Ticket < ActiveRecord::Base
belongs_to :event
My Controller
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
def new
#event = Event.new
end
def create
#event = Event.new(event_params)
if #event.save
flash[:notice] = 'wow'
redirect_to #event
else
flash[:notice] = 'oh..'
render 'new'
end
end
def edit
end
def update
if #event.update(event_params)
redirect_to #event
else
render 'edit'
end
end
My form
<div class="nested-fields">
<h3>tickets</h3>
<div class="row formsection text-center">
<div class="col-md-3">
<span class="label-title">fee</span>
</div>
<div class="col-md-9">
<div class='input-group'>
<%= f.text_field :fee, :placeholder => "fee" , :class => "form-control" %>
</div>
</div>
</div>
<div class="row formsection text-center">
<div class="col-md-3">
<span class="label-title">payment</span>
</div>
<div class="col-md-9">
<%= f.hidden_field :id %>
<% payments = { 'paypal' => 1, 'credit card' => 2 } %>
<% payments.each do |pay,i| %>
<%= f.check_box :payment_type, { multiple: true ,checked: f.object.payment_type.include?(i), include_hidden: false }, i %>
<%= f.label :payment_type, pay, :value => i %>
<br>
<% end %>
</div>
</div>
<%= link_to_remove_association 'remove this', f %>
</div>
Not sure why it works on new, but if payment_type is not yet set, and so is nil, this will give the error you showed.
So instead write something like:
<%- checked_value = f.object.payment_type.present? && f.object.payment_type.include?(i) %>
<%= f.check_box :payment_type, { multiple: true ,checked: checked_value, include_hidden: false }, i %>
I'd like to add images to a model. I have Users set up with Devise and they have_many Items that they are selling.
I would now like to add an array of images to the Item model (not sure if this is the best approach).
I have looked a Paperclip but can only do one image. Also looked at carrier wave but not sure how to implement on existing model.
Here is some of my code.
Item.rb
class Item < ActiveRecord::Base
belongs_to :user
validates :title, presence: true
validates :price, presence: true
validates :description, presence: true
end
items_controller.rb
class ItemsController < ApplicationController
before_action :findItem, only: [:edit, :update, :sold]
def index
#items = Item.all
end
def new
#item = Item.new
end
def create
#item = Item.create(item_params)
#item.user = current_user
if #item.save
flash[:success] = "Your item was successfully listed."
render 'show'
else
flash[:error] = "Your item could not be listed. Please try again."
render 'new'
end
end
def show
#item = Item.find(params[:id])
end
def edit
end
def update
if #item.update(item_params)
flash[:success] = "Your item listing was updated successfully."
redirect_to item_path(#item)
else
flash[:error] = "Your listing was not updated. Please try again."
render 'edit'
end
end
def sold
#item.toggle(:sold)
#item.save
redirect_to item_path(#item)
end
private
def item_params
params.require(:item).permit(:title, :price, :description)
end
def findItem
#item = Item.find(params[:id])
end
end
Form for Item/new.html.erb
<div class="row">
<div class="col-xs-12">
<%= form_for(#item, :html => {class: "form-horizontal", role: "form"}) do |f| %>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :title %>
</div>
<div class="col-sm-8">
<%= f.text_field :title, class: "form-control", placeholder: "What are you selling?", autofocus: true %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :price %>
</div>
<div class="col-sm-8">
<%= f.number_field :price, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :description %>
</div>
<div class="col-sm-8">
<%= f.text_area :description, rows: 10, class: "form-control", placeholder: "Describe your item. The more detail you include, the more likely it is to sell." %>
</div>
</div>
<div class="form-group">
<div class="center col-sm-offset-1 col-sm-10">
<%= f.submit class: "btn btn-primary btn-lg" %>
</div>
</div>
<% end %>
<div class="center col-xs-4 col-xs-offset-4">
<%= link_to "[ Cancel and return to listing page ]", items_path %>
</div>
You simply want to create an Image model. And set the relation with Item as follows: Item has_many :images, and Image belongs_to :item.
But, yeah, paperclip is a good start.
EDIT: Oh, and welcome Rails, you will find a good community ready to help. You might also find useful to search for accepts_nested_attributes_for so you can have the images uploaded in your Item form and cocoon to dynamically add and remove images on the item's form.
#app/models/item.rb
class Item < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images
validates :title, :price, :description, presence: true
end
#app/models/image.rb
class Image < ActiveRecord::Base
belongs_to :item
has_attached_file :picture
end
--
The above will allow you to create an Item, which will have any number of images that you want. You can pass the new images through accepts_nested_attributes_for:
#app/controllers/items_controller.rb
class ItemsController < ApplicationController
def new
#item = Item.new
#item.images.build
end
def create
#item = Item.new item_params
#item.save
end
private
def item_params
params.require(:item).permit(images_attributes: [:picture])
end
end
#app/views/items/new.html.erb
<%= form_for #item do |f| %>
<%= f.fields_for :images do |i| %>
<%= i.file_field :picture %>
<% end %>
<%= f.submit %>
<% end %>
I am creating a customer service web app for our clients that allows them to submit new customer service tickets, once logged in. I have a login system that I created using the sorcery gem, and I have modified it to accept an additional field: client code.
We are assigning client codes to help prevent unauthorized users from creating accounts.
Upon login, it asks for the information like so:
Name:
Email:
Client Code: (we assign this)
Password:
My question is this, is there a way to only display customer service tickets to clients with the same client code? For example, The "Coca-Cola" clients would only see other "Coca-Cola" tickets and the "Pepsi" clients would only see other "Pepsi" tickets, etc.
Here is my Tickets Controller:
class TicketsController < ApplicationController
before_filter :require_login
def new
#ticket = Ticket.new
end
def create
#ticket = Ticket.new(ticket_params)
if
#ticket.save
redirect_to #ticket
flash[:notice] = "Your Ticket has been submitted. We will contact you very soon!"
else
flash[:notice] = "Something went wrong :("
render 'new'
end
end
def show
#ticket = Ticket.find(params[:id])
end
def index
#tickets = Ticket.all
end
def edit
#ticket = Ticket.find(params[:id])
end
def update
#ticket = Ticket.find(params[:id])
if #ticket.update(ticket_params)
redirect_to #ticket
else
render 'edit'
end
end
def destroy
#ticket = Ticket.find(params[:id])
#ticket.destroy
redirect_to tickets_path
end
private
def ticket_params
params.require(:ticket).permit(:name, :email, :phone, :help)
end
end
Here is the Ticket Index View:
<div class="panel panel-default">
<div class="panel-heading">Ticket Queue</div>
<div class="panel-body">
<table class="table">
<tr>
<th>Name</th>
<th>Phone</th>
<th></th>
<th></th>
</tr>
<% #tickets.each do |ticket| %>
<tr>
<td><%= ticket.name %></td>
<td><%= ticket.phone %></td>
<td><%= button_to "View or Edit", ticket_path(ticket), :class => "btn btn-primary btn-sm", :method => :get %></td>
<td><%= button_to "Delete", ticket_path(ticket), :class => "btn btn-primary btn- sm", :method => :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
</div>
Here is the New Ticket View:
<div class="panel panel-info">
<div class="panel-heading">
<div id="wrapper">
<h1 class="panel-title">New Ticket
<div id="first"><%= button_to "Back", root_path, :class => "btn btn-primary btn-sm", :method => :get %></div>
</div>
</h1>
</div>
<div class="panel-body">
<%= form_for :ticket, url: tickets_path do |f| %>
<p>
<%= f.label "Name:" %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label "Email:" %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :"Phone #:" %>
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :"How can we help?" %>
<p><%= f.text_area :help, :cols=> 38, :rows => 8 %></p>
</p>
<p>
<button type="submit" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-envelope"></span> Submit Ticket</button>
</p>
<% end %>
</div>
</div>
Here is the User Model:
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates :password, length: { minimum: 3 }
validates :password, confirmation: true
validates :password_confirmation, presence: true
validates :code, inclusion: { in: %w(Client1, Client2), message: "Please enter a valid Client Code", :allow_nil => false}
validates :email, uniqueness: true
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
end
Here is the New User View:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.label "Client Code" %><br />
<%= f.text_field :code %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here is the User Controller:
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
skip_before_filter :require_login, only: [:index, :new, :create]
# GET /users
def index
#users = User.all
end
# GET /users/1
def show
end
# GET /users/new
def new
#user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
def create
#user = User.new(user_params)
if #user.save
redirect_to(:users, notice: 'User was successfully created')
else
render :new
end
end
# PATCH/PUT /users/1
def update
if #user.update(user_params)
redirect_to #user, notice: 'User was successfully updated.'
else
render :edit
end
end
# DELETE /users/1
def destroy
#user.destroy
redirect_to users_url, notice: 'User was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :code)
end
end
Let me know if you need to see anything else, thank you!
From your example, I would think Users would have an additional database layer:
class User < ActiveRecord::Base
belongs_to :company
has_many :tickets, through: :companies
end
class Company < ActiveRecord::Base
has_many :users
has_many :tickets
end
class Ticket < ActiveRecord::Base
belongs_to :company
end
Then you can easily display tickets associated with each company