验证创建操作导致路由错误

时间:2013-07-16 13:30:37

标签: ruby-on-rails ruby ruby-on-rails-3

创建操作的验证不起作用,我已经对要存在的字段进行了验证,但是如果我将字段保持为空并按提交,则会出现路由错误,如果我填写完整的字段,它的工作完美精细。此外,验证对于更新操作完全正常。

以下是观点:

<%= stylesheet_link_tag 'gmaps4rails' %>

<%= form_for @estate, :html => { :class => 'form-horizontal',:multipart => true } do |f| %>
    <% if @estate.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@estate.errors.count, "error") %> prohibited this estate from being saved:</h2>

          <ul>
            <% @estate.errors.full_messages.each do |msg| %>
                <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
    <% end %>
    <script>
        function enableDisable(bEnable, textBoxID)
        {
            document.getElementById(textBoxID).disabled = !bEnable
        }
    </script>
    <div class="control-group">
      <%= f.label :Name, "Property/Tenant Name", :class => 'control-label' %>
      <div class="controls">
        <%= f.text_field :Name, :class => 'text_field', :placeholder => "e.g., Saxbys Coffee" %>
      </div>
    </div>
    <div class="control-group">
      <%= f.label :Address, :class => 'control-label' %>
      <div class="controls">
        <%= f.text_area :Address, :class => 'text_area', :cols => '10', :rows => '10', :placeholder => "e.g., 1236 36th Street NW Washington, DC 20007"  %>
      </div>
    </div>
    <div class="control-group">
      <%= f.label :asset, "Upload Picture", :class => 'control-label' %>
      <div class="controls">
        <%= f.file_field :asset %>
      </div>
    </div>
    <% if @estate.asset.present? %>
      <div class="control-group">
        <%= f.label "Delete Existing Picture", :class => 'control-label' %>
        <div class="controls">
          <%= f.check_box(:delete_asset) %>
        </div>
      </div>
    <% end %>
    <!-- <div class="control-group">

      <%= f.label :Mgmt, "Would you like to share this property with your Real Estate Management Company?", :class => 'control-label' %>
  <div class="controls">

    <input type="checkbox" id="chkbox" onchange="document.getElementById('txtBox').disabled=!this.checked;" checked="checked" />
  </div>

          </div>   -->

    <% if current_user.Company.nil? %>
        <div id="flip"><a>Would you like to share this property with your Management Company?</a></div>
        <br />
        <div id="panel">
          <div class="control-group">
            <%= f.label :Mgmt, "Company Name", :class => 'control-label' %>
            <div class="controls">
              <%= f.text_field :Mgmt, :class => 'text_field', :id => 'txtBox'%>
            </div>
          </div>
          <div class="control-group">
            <%= f.label :companyemail, "Company Email", :class => 'control-label' %>
            <div class="controls">
              <%= f.text_field :companyemail, :class => 'text_field', :id => 'txtBox'%>
            </div>
          </div>
        </div>
      <% end %>

      <div class="form-actions">
        <% if current_page?(controller:"estates", action:"edit", :id => params[:id] || 0)%>
        <%= f.submit "Update Property Details", :class => 'btn btn-info' %>
        <% else %>
            <%= f.submit "Upload Property Details", :class => 'btn btn-info' %>
        <% end %>
<% end %>
<script>
    $(document).ready(function(){
        $("#flip").click(function(){
            $("#panel").slideToggle("slow");
        });
    });
</script>

这是控制器:

def create
    # @estate = Estate.new(params[:estate])
    if current_user.Company.nil?
      @estate = current_user.estates.build(params[:estate])
    else
      serve = User.find(@@key)
      @estate = Estate.new(params[:estate])
      @estate.user_id = serve.id
      @estate.Mgmt = current_user.Company
    end

    respond_to do |format|
      if @estate.save
        if current_user.Company.nil?

          if @estate.companyemail = ''
            #
          else
            EstateMailer.company_confirmation(@estate).deliver
          end

        end

        format.html { redirect_to @estate, notice: 'Property details were successfully updated.' }
        format.json { render json: @estate, status: :created, location: @estate }
      else
        format.html { render action: "new" }
        format.json { render json: @estate.errors, status: :unprocessable_entity }
      end
    end
  end

错误讯息:

No route matches {:action=>"show", :controller=>"estates", :id=>#<Estate id: nil, Name: "", Address: "", created_at: nil, updated_at: nil, user_id: 5, asset_file_name: nil, asset_content_type: nil, asset_file_size: nil, asset_updated_at: nil, Mgmt: "", companyemail: "", latitude: nil, longitude: nil, gmaps: nil>}

的routes.rb

  resources :feedbacks


  root to: 'home#index'

  devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
             controllers: {omniauth_callbacks: "omniauth_callbacks"}

  resources :profiles


  resources :estates do

    resources :records do
      resources :documents
    end

  end

  get 'faq/faqs'
  match '/records',to: 'estates#record'
  get 'management/index'
  match 'management/show', to: 'management#show'
  match 'management/showrecord', to: 'management#showrecord'

1 个答案:

答案 0 :(得分:1)

Hrishikesh Sardar 进行了一次聊天后,错误得到修复,问题出现在创建操作的if块中:

if current_user.Company.nil?
  @estate = current_user.estates.build(params[:estate])
else
  serve = User.find(@@key)
  @estate = Estate.new(params[:estate])
  @estate.user_id = serve.id
  @estate.Mgmt = current_user.Company
end

这一行:

@estate = current_user.estates.build(params[:estate])

必须替换为:

@estate = Estate.new(params[:estate])
@estate.user_id = current_user.id
即使current_user.estates.new(params[:estate])estate之间的关系是按照要求构建的,

某种方式user也无效。