2012-09-28 118 views
1

我有一個允許用戶創建新項目的頁面。他們可以選擇鏈接到表格colomn的下拉菜單,選擇他們正在查找的選項已經在數據庫中,以及另一個選項,他們可以輸入新數據,當提交項目時,該字段的新數據將保存到數據庫中供下一個人使用。Ruby on Rails:編輯表單不保存新數據

這是我的新建/編輯項目視圖的一部分:

<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> 

用戶可以通過文本字段中輸入任何新數據,或者從下拉菜單中選擇。如果用戶在文本框中輸入新數據並從下拉菜單中選擇某些內容,則新數據將與項目一起保存,因爲它優先考慮。

我的問題在於編輯觀點:

當用戶點擊編輯鏈接,該項目顯示了所有他們把在數據的,但是這一切在下拉菜單舉行。

如果他們嘗試在文本框中輸入新數據並保存該項目,它將與下拉列表中突出顯示的內容一起保存,並忽略放入文本框中的新數據。

編輯視圖與新視圖完全相同。

任何人都可以指向正確的方向。我是新來的鐵軌,所以請嘗試幫助時記住這一點。

在此先感謝。

# 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 

編輯觀點:

<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" %> 

回答

2

我瘦的問題是在你的更新方法。 您與params['project']

所以,你需要改變params['project'][attribute]

順便說改變params[attribute],然後更新項目,一定要仔細閱讀有關虛擬屬性,你可以用它們來做到這一點更明確的方式。

例如

class Project < Activerecord::Base 
#your code here 

attr_accessor :new_client 

before_save :assign_new_client 

def assign_new_client 
    if @new_client 
     self.client = @new_client 
     @new_client = nil 
    end 
end 

end 

這不是測試,但只顯示了一個知道如何使用它

+0

'PARAMS [ '項目'] [:客戶端] =參數[:new_client除非PARAMS [:new_client] .blank' – Jazz

+0

似乎工作。只需要測試。非常感謝 – Jazz

+1

沒問題,我加了一個例子如何使用虛擬屬性 – Fivell

0

它看起來就像你不能從文本字段獲取值。你需要添加f。在text_field標識符之前。

f.text_field_tag