0

我有一个允许用户创建新项目的页面。他们可以选择从链接到表格列的下拉菜单中进行选择,以防他们正在寻找的选项已经在数据库中,以及他们可以输入新数据的另一个选项,即在提交项目时,该文件的新数据将保存到下一个人的数据库中。

这是我的新/编辑项目视图的一部分:

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

用户可以通过文本字段输入新数据,也可以从下拉列表中选择。如果用户将新数据放入文本框中并从下拉列表中选择某些内容,则新数据将与项目一起保存,因为它具有优先级。

我的问题在于编辑视图:

当用户单击编辑链接时,项目会显示他们输入的所有数据,但所有数据都保存在下拉菜单中。

如果他们尝试在文本框中输入新数据并保存项目,它会与下拉菜单中突出显示的内容一起保存,并忽略放入文本框中的新数据。

编辑视图与新视图完全相同。

谁能指出我正确的方向。我是 Rails 新手,所以在尝试提供帮助时请记住这一点。

提前致谢。

编辑 4

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

2 回答 2

1

这个

@project.client = params[:new_client] unless params[:new_client].blank?

也需要在update方法中。

更新 2

另外,当你打电话

@project.update_attributes(params[:project])

. @project_ params[]所以这里有一个解决方案:

  # GET /projects/1/edit
  def edit
    @project = Project.find(params[:id])
  end

  # 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[:project][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

一些提示:update_attributes还保存一个对象。请参阅API 文档

此外,此代码是正确的:

params[:client] = params[:new_client] unless params[:new_client].blank?
params[:project_owner] = params[:new_project_owner] unless params[:new_project_owner].blank?
params[:tech] = params[:new_tech] unless params[:new_tech].blank?
params[:role] = params[:new_role] unless params[:new_role].blank?
params[:industry] = params[:new_industry] unless params[:new_industry].blank?
params[:business_div] = params[:new_business_div] unless params[:new_business_div].blank?

但这更短:D

[: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
于 2012-09-28T09:58:36.457 回答
0

你没有保存项目你正在更新项目尝试update_attributes @project.save并检查 -</p>

于 2012-09-28T09:44:12.943 回答