Hey all,
I am still struggling to get to grips with Ruby fully and have a new problem.
I am looking to associate contacts with users through a has_many and belongs_to method. I believe I have correctly nested the contacts table as a resource of my users in routes.
At the moment however I still seem to be showing all contacts regardless of which user is signed in?
The code I currently have is :
Routes
ContactApp::Application.routes.draw do
get "sessions/new"
get "logout" => "sessions#destroy"
controller :user do
get "signup" => "user#new"
end
resources :users, :controller => 'user', :has_many => 'contacts'
controller :sessions do
get "login" => "sessions#new"
post "login" => "sessions#create"
delete "logout" => "sessions#destroy"
end
controller :dashboard do
get "home" => "dashboard#home"
end
controller :contact do
get "newcontact" => "contact#new"
get "index" => "contact#index"
end
resources :contacts, :controller => 'contact'
root :to => 'sessions#new'
end
Contact Controller
class ContactController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new (params[:contact])
if @contact.save
redirect_to index_path
else
render "contact/new"
end
end
def index
@title= "All Users"
@contact = Contact.all
end
end
User Controller
class UserController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new (params[:user])
if @user.save
redirect_to root_url
else
render "user/new"
end
end
end
Contact#new Relevant part
<%= form_for @contact do |f| %>
<% if @contact.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @contact.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class = "name-field">
<%= f.text_field :name, :placeholder => "Name" %>
</div>
<div class = "company-field">
<%= f.text_field :company, :placeholder => "Company" %>
</div>
<div class = "email-field">
<%= f.text_field :email, :placeholder => "Email" %>
</div>
<div class = "phone-field">
<%= f.text_field :phone, :placeholder => "Phone" %>
</div>
<div class = "mobile-field">
<%= f.text_field :mobile, :placeholder => "Mobile" %>
</div>
<div class="actions"><%= f.submit "Add Contact" %></div>
</div>
<% end %>
</div>
</div>
Really sorry to post yet another question on the forum but any help people can offer really would be much appreciated.
Cheers,
Tom