I have a page that allows users to create new projects. They have the option to select from a drop down menu that is linked to a table colomn, incase the option they are looking for is already in the database, and another option where they can enter new data, that when the project is submitted, the new data for that filed gets saved into the database for the next person.
This is part of my new/edit project view:
<div class="client">
<%= label_tag :new_client, "Client" %><br/>
<%= text_field_tag :new_client, nil, :maxlength => 30 %>
Or
<%= f.select( :client, Project.all.map {|p| [p.client]}.uniq, :prompt => "Select a previous Client") %>
</div>
The user can either input new data via the text field, or select from the drop down. If the user puts new data in the text box AND selects something from the drop down, the new data is saved with the project as it takes priority.
My problem lies with the edit view:
When the user clicks the edit link, the project is shown with all of the data they put in, but all of it is held in drop down menus.
If they try to enter new data in to the textbox, and save the project, it gets saved with whatever is highlighted in the dropdown, and ignores the new data put into the text box.
The edit view is exactly the same as the new view.
Can anyone point me in the right direction. I am new to rails, so please remember this when trying to help.
Thanks in advance.
# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end
# POST /projects
# POST /projects.json
def create
@all_technols = Technol.all
@project = Project.new(params[:project])
@technol = Technol.new(params[:tech])
params[:technols][:id].each do |technol|
if !technol.empty?
#@project.projecttechnols.build(:technol_id => technol)
@project_technol = @project.projecttechnols.build(:technol_id => technol)
end
end
@project.client = params[:new_client] unless params[:new_client].blank?
@project.project_owner = params[:new_project_owner] unless params[:new_project_owner].blank?
@project.tech = params[:new_tech] unless params[:new_tech].blank?
@project.role = params[:new_role] unless params[:new_role].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PUT /projects/1
# PUT /projects/1.json
# PUT /projects/1
# PUT /projects/1.json
def update
@project = Project.find(params[:id])
[:client, :project_owner, :tech, :role, :industry, :business_div].each do |attribute|
params[attribute] = params["new_#{attribute}".to_sym] unless params["new_#{attribute}".to_sym].blank?
end
respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
EDIT VIEW:
<h1>Editing project</h1>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<%= stylesheet_link_tag "form" %>
<%= form_for(@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<BODY LANG="en-GB" TEXT="#000000" DIR="LTR">
<P LANG="en-US" CLASS="western" ALIGN=RIGHT STYLE="margin-bottom: 0in">
<BR>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<div class ="padding">
</div>
<CENTER>
<TABLE WIDTH=624 CELLPADDING=2 CELLSPACING=0 >
<COL WIDTH=283>
<COL WIDTH=20>
<COL WIDTH=152>
<COL WIDTH=138>
<COL WIDTH=9>
<THEAD>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=29 BGCOLOR="#999999" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<H1 LANG="en-US" CLASS="western"><FONT COLOR="#003366"><FONT SIZE=4>
<div id = "project_name">
<div class="project_name">
<%= f.label :project_name %> :
<%= f.text_field :project_name,:maxlength => 30 %>
</div>
</div>
</FONT></FONT></H1>
</TD>
</TR>
</THEAD>
<TBODY>
<TR>
<TD COLSPAN=2 WIDTH=307 HEIGHT=15 BGCOLOR="#e6e6e6" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<H2 LANG="en-US" CLASS="western" ALIGN=LEFT>Database id:</H2>
</TD>
<TD COLSPAN=3 WIDTH=307 BGCOLOR="#e6e6e6" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<H2 LANG="en-US" CLASS="western" ALIGN=LEFT>
<div id ="smallbox">
<div id = "status">
<div class="status">
<%= f.label :status %> :
<%#= f.select :status, [['Active'],['Completed']], {:include_blank => true} %>
<%= f.select :status, [['Active'],['Completed']] %>
</div></div>
</H2>
</TD>
</TR>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=13 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><B>
<div class="client">
<%= label_tag :new_client, "Client" %><br/>
<%= text_field_tag :new_client, nil,:maxlength => 30 %>
Or
<%= f.select( :client, Project.all.map {|p| [p.client]}.uniq, :prompt => "Select a previous Client") %>
</div>
</B>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P LANG="en-US" CLASS="western"><BR>
</P>
</TD>
</TR>
<TR>
<TD COLSPAN=2 WIDTH=307 HEIGHT=13 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><B>
<div class="business_div">
<%= label_tag :new_business_div, "Business Division" %><br/>
<%= text_field_tag :new_business_div, nil,:maxlength => 30%>
Or
<%= f.select( :business_div, Project.all.map {|p| [p.business_div]}.uniq, :prompt => "Select Business Division") %>
</div>
</B>
</P>
<P LANG="en-US" CLASS="western"><BR>
</P>
</TD>
<TD COLSPAN=3 WIDTH=307 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><B>
<div class="project_owner">
<%= label_tag :new_project_owner, "Project Owner" %><br/>
<%= text_field_tag :new_project_owner, nil,:maxlength => 30 %>
Or
<%= f.select( :project_owner, Project.all.map {|p| [p.project_owner]}.uniq, :prompt => "Select a previous Project Owner") %>
</div>
</B></P>
<P LANG="en-US" CLASS="western"><BR>
</P>
</TD>
</TR>
<TR>
<TD WIDTH=283 HEIGHT=13 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western">
<div class="start_date">
<%= f.label :start_date %> :
<%= f.text_field :start_date, :class => 'datepicker', :style => 'width: 80px;' %>
</div>
</P>
</TD>
<TD COLSPAN=2 WIDTH=176 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western">
Entry Created by:<br> <%= @project.first_name %> <%= @project.last_name %>
<%= f.hidden_field :first_name, :value => @project.first_name %>
<%= f.hidden_field :last_name, :value => @project.last_name %>
</P>
</TD>
<TD COLSPAN=2 WIDTH=151 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western">
Entry date: <br> <%= l DateTime.now.to_date %>
</P>
</TD>
</TR>
<TR>
<TD WIDTH=283 HEIGHT=13 VALIGN=TOP BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western">
<div class="end_date">
<%= f.label :end_date %> :
<%= f.text_field :end_date, :class => 'datepicker', :style => 'width: 80px;' %>
</div>
</P>
</TD>
<TD COLSPAN=2 WIDTH=176 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western">
Last Edited by: <br> <%= @project.edited_first_name %> <%= @project.edited_last_name %></P>
<%= f.hidden_field :edited_first_name, :value => current_user.firstname %>
<%= f.hidden_field :edited_last_name, :value => current_user.lastname %>
</TD>
<TD COLSPAN=2 WIDTH=151 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western">
Edited date: <br>
<% if !(@project.edited_date.blank?) %>
<%= l @project.edited_date %>
<% end %>
<%= f.hidden_field :edited_date, :value => DateTime.now.to_date %>
</P>
</TD>
</TR>
<TR>
<TD WIDTH=283 HEIGHT=13 VALIGN=TOP BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in">
<div class="tech">
<%= label_tag :new_tech, "Technologies Used :" %><br/>
<%= text_field_tag :new_tech, nil,:maxlength => 30 %>
Or
<%= f.select( :tech, Project.all.map {|p| [p.tech]}.uniq, :prompt => "Select a previous Technology") %>
</div>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P LANG="en-US" CLASS="western"><BR>
</P>
</TD>
<TD COLSPAN=2 WIDTH=176 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in">
<div class="role">
<%= label_tag :new_role, "Role" %><br/>
<%= text_field_tag :new_role, nil,:maxlength => 30%>
Or
<%= f.select( :role, Project.all.map {|p| [p.role]}.uniq, :prompt => "Select a previous role") %>
</div>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P LANG="en-US" CLASS="western"><BR>
</P>
</TD>
<TD COLSPAN=2 WIDTH=151 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in">
<div class="industry">
<%= label_tag :new_industry, "Industry:" %><br/>
<%= text_field_tag :new_industry, nil,:maxlength => 30%>
Or
<%= f.select( :industry, Project.all.map {|p| [p.industry]}.uniq, :prompt => "Select an industry") %>
</div>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<P LANG="en-US" CLASS="western"><BR>
</P>
</TD>
</TR>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=15 BGCOLOR="#e6e6e6" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<H2 LANG="en-US" CLASS="western">
Summary Of Assignment:
</H2>
</TD>
</TR>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=13 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<div class="summary">
<%= f.label :summary %><br />
<%= f.text_area :summary, :style => 'width: 500px;' 'height: 100px;' %>
</div>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
</TD>
</TR>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=15 BGCOLOR="#e6e6e6" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<H2 LANG="en-US" CLASS="western">Additional Informaton</H2>
</TD>
</TR>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=13 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in">
<div class="lessons_learned">
<%= f.label :lessons_learned %><br />
<%= f.text_area :lessons_learned, :style => 'width: 500px;' 'height: 100px;' %>
</div>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
</TD>
</TR>
<TR VALIGN=TOP>
<TD COLSPAN=5 WIDTH=618 HEIGHT=67 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western">
<div class="customer_benifits">
<%= f.label :customer_benifits %><br />
<%= f.text_area :customer_benifits,:style => 'width: 500px;' 'height: 100px;' %>
</div>
</P>
</TD>
</TR>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=13 BGCOLOR="#ffffff" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in">
<div class="financials">
<%= label_tag :financials, "Financial Responsibility:" %><br />
<%= f.select :financials, [['£0 - £99,999'],['£100,000 - £249,999'],['£250,000 - £499,999 '],['£500,000 - £999,999 '],['£1,000,000 +']], :prompt => "Select" %>
</div>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
<div class="keywords">
Any other searchable keywords and tags?<br />
<%= f.text_field :keywords %>
</div>
</P>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
</TD>
</TR>
<TR>
<TD COLSPAN=5 WIDTH=618 HEIGHT=14 BGCOLOR="#e6e6e6" STYLE="border: 1px solid #00000a; padding: 0.02in 0.06in">
<H2 LANG="en-US" CLASS="western"><A NAME="_GoBack"></A></H2>
</TD>
</TR>
</TBODY>
</TABLE>
</CENTER>
<P LANG="en-US" CLASS="western" STYLE="margin-bottom: 0in"><BR>
</P>
</BODY>
</HTML>
<div class="create_button">
<div class="actions">
<%= f.submit "Save Edit", :class => "button", :confirm => "Are you sure you want to save the edit?" %>
</div>
</div>
</div> <%#= small div %>
<% end %>
<%#= button_to 'Show', @project, :class => "button" %>
<%#= button_to "Back", projects_path , :class => "button", :method => "get" %>
I thin the problem is in your update method.
You change
params[attribute]and then update project withparams['project']So you need to change
params['project'][attribute]By the way, you have to read about virtual attributes, you can use them to do this in more clear way.
For example
end
It’s not tested, but just shows an idea how to use it