I have a Developers Table and a Projects Table.I am trying to assign a one to many relationship between these two tables in my rails application. Were I will add developers to projects.
developer belongs to a project ,project has many developers
so I did the following.
I created this method :
def add_to_project
@project = Project.find_by(:title => params[:title])
@developer = Developer.find_by(:name => params[:name])
unless @developer.nil? || @project.nil?
# @project.developers.push(@developer)
@project.developers << @developer
if @project.save
flash[:notice] = "project was added successfully"
else
flash[:notice] = "project was not added successfully"
end
else
flash[:notice] = "project was not added successfully"
end
redirect_to(:action => 'add_dev_proj') # this goes to a page that shows the two tables with the updates
end
I obtain the values of the title and name from a form_tag I constructed like the following:
<%= form_tag(:controller => 'projects', :action => 'add_to_project') do %>
<%= text_field_tag('name', params[:name]) %>
<%= text_field_tag('title', params[:title]) %>
<%= submit_tag("Add dev to project") %>
<% end %>
My problem is when trying this approach using rails console it works, but in my web app no modifications are being made. is there something I am missing here. Also I made sure my associations are defined in my model.
Any insights would be great, thanks a lot in advance.