0

ユーザーがプロジェクトをデータベースに入力できるアプリがあります。

一部のフィールドのテキストボックスを介して新しいデータを入力するか、そのフィールドに以前に入力されたドロップダウンメニューを介してデータを選択するかを選択できます。

ユーザーがフォームに入力して[送信]をクリックしたが、フィールドの1つが欠落しているなどの問題がある場合、ページはどのフィールドが欠落しているかを示すエラーを報告します。これは問題ありません。

ただし、ユーザーがテキストボックスに新しいデータを入力した場合、そのデータは削除され、代わりにドロップダウンの最初のオプションが選択されます。

これが私のプロジェクトコントローラーです。

class ProjectsController < ApplicationController


before_filter :authenticate_user!
#:except => [:show, :index]




    def index
        @projects = Project.all

respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @projects }
    end
  end

  # GET /projects/1
  # GET /projects/1.json
  def show
    @project = Project.find(params[:id])
        @project_project_id = params[:id]

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @project }
    end
  end

  # GET /projects/new
  # GET /projects/new.json
  def new
    @project = Project.new
        @technol = Technol.new(params[:tech])

        @all_technols = Technol.all
        tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?


        @project_technol = @project.projecttechnols.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @project }
    end
  end

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



puts @project.inspect
puts @project.technols.inspect
  end

  # POST /projects
  # POST /projects.json
  def create    
    @project = Project.new(params[:project])
        @project.client = params[:new_client] unless params[:new_client].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?


if !params[:technols].nil?

            params[:technols][:id].each do |tech|

                if !tech.empty?

                    @project_technol = @project.projecttechnols.build(:technol_id => tech) 

                end
            end

end

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

puts @project.inspect
puts @project.technols.inspect
        @project.client = params[:new_client] unless params[:new_client].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.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

  # DELETE /projects/1
  # DELETE /projects/1.json
  def destroy
    @project = Project.find(params[:id])
    @project.destroy

    respond_to do |format|
      format.html { redirect_to projects_url }
      format.json { head :no_content }
    end
  end




private

  helper_method :sort_column, :sort_direction
  def sort_column
    Project.column_names.include?(params[:sort]) ? params[:sort] : "project_name"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

    def per_page
      params[:per_page] ||= 1
    end
    def page 
      params[:page] ||= 1
   end
end

これが私の新しいプロジェクトビューの一部です

<%= stylesheet_link_tag "new" %>


<h1>Create New Project</h1>
<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 %>

<CENTER>

   <div id = "project_name">
<div class="project_name">
    Project Name:
    <%= f.text_field :project_name,:maxlength => 30 %>
  </div>
</div>


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



<div class="client" STYLE="text-align: left;">
<%= 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 previous..") %>
</div>


   <div class="business_div" STYLE="text-align: left;">
<%= 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 previous") %>
</div>

   <div class="start_date" STYLE="text-align: left;">
    <b>Start Date:</b>
    <%= f.text_field :start_date, :class => 'datepicker', :style => 'width: 80px;' %>
  </div>  

</P>


<div class="create_button">
<div class="actions">
    <%= f.submit "Save New Project", :class => "button",  :confirm => "Are you sure you want to save the new project?" %>
  </div>
</div>

</div> <%#= small div %>


<% end %>



<div class="back_button2">
<%= button_to "Back", projects_path , :class => "button", :method => "get" %>
</div>

これが私のプロジェクトモデルです

class Project < ActiveRecord::Base
  attr_accessible  :fullname, :edited_first_name, :edited_last_name, :first_name, :last_name, :business_div, :client, :customer_benefits, :edited_date, :end_date, :entry_date, :financials, :industry, :keywords, :lessons_learned, :project_name, :role, :start_date, :status, :summary, :technol_ids, :tech , :technols



validates_presence_of :business_div, :client, :customer_benefits, :end_date, :financials, :industry, :lessons_learned, :project_name,  :role, :start_date, :status, :summary #, :keywords

validates_format_of :industry, :with => /\A[^\d]+\Z/, :message => "field should only have letters"
validates_format_of :business_div, :with => /\A[^\d]+\Z/, :message => "field should only have letters"
validates_format_of :client, :with => /\A[^\d]+\Z/, :message => "field should only have letters"
validates_format_of :exception_pm, :with => /\A[^\d]+\Z/, :message => "field should only have letters"
validates_format_of :project_owner, :with => /\A[^\d]+\Z/, :message => "field should only have letters"
validates_format_of :role, :with => /\A[^\d]+\Z/, :message => "field should only have letters"

has_many :projecttechnols
has_many :technols, :through => :projecttechnols

  def set_fullname(a, b)
    fullname = [a, b].join(' ')
  end


accepts_nested_attributes_for(:technols)

end

他に何かを含める必要がある場合は、お知らせください。私はしばらくの間この問題に悩まされてきました。前もって感謝します。

4

1 に答える 1

1

railsdogが示唆するように、に渡されたデータを使用しますProjects#create。フォームはデータを送信し、そのデータはparamsキーと値のペアと呼ばれる変数に保持されます。Projects#createその変数は、どのビューがレンダリングされても引き続きアクセス可能である必要があります。

あなたがする必要があるのはあなたのフォーム要素にいくつかのデフォルトのコンテンツを設定することです。text_field呼び出された「project_name」の例を次に示します

<div class="project_name">
 Project Name:
  <%= f.text_field :project_name, params[:project_name],:maxlength => 30 %>
</div>

他のフィールドと同じまたは同様のことができるはずです。

于 2012-10-16T18:25:17.540 に答える