如何修复丢失的模板,在rails中创建应用程序/创建

时间:2018-02-01 12:25:06

标签: ruby-on-rails

我不断收到此模板错误

" Missing template listings/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/codio/workspace/app/views" * "/var/lib/gems/2.2.0/gems/kaminari-0.16.3/app/views" * "/var/lib/gems/2.2.0/gems/commontator-4.10.3/app/views" * "/home/codio/.bundler/ruby/2.2.0/devise-a9d90503e903/app/views" * "/home/codio/.bundler/ruby/2.2.0/koudoku-9e73e64e5520/app/views" * "/var/lib/gems/2.2.0/gems/mailboxer-0.12.4/app/views"

,无论何时我试图创建一个对象,在堆栈​​溢出中进行几次搜索之后,有些人建议重定向或渲染,最初在我的创建操作中,没有显式重定向,并且在创建对象之后,重定向已完成到节目中页面(预期的行为)。尝试了两个解决方案,但仍然得到相同的错误,在我的理解中,没有必要为创建操作提供相应的视图。

如何在不创建的情况下将创建操作重定向到显示页面  观点?。

    listings_controller.rb
    class ListingsController < ApplicationController
       ...

      def create
        @listing = Listing.new(listing_params)
        if @listing.save
          if params[:images]
              params[:images].each { |image|
                @listing.pictures.create(image: image)
              }
           end
         (@users - [current_user]).each do |user|
            Notification.create(recipient: user, actor: current_user, action: "posted", notifiable: @listing)
         end   
         flash[:notice]= "L'annonce #{@listing.listing_number} a eté publiee avec succès."
         respond_with(@listing)  
        end
      end
         ...
  end

触发创建操作的表单通过模式

呈现
_form.html.erb

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Publication d'annonce</h4>
          </div>
          <div class="modal-body"> 
             <%= form_for :listing, :url => {:action => :create} do |f| %>
              <div class="form-group row">
                   <%= f.label :name,"Titre de l'annonce", class: 'col-4 col-form-label'%> 
                <div class="col-8">
                   <%= f.text_field :name, placeholder: "Titre de l'annonce",class: "form-control here" %>  
                </div>
              </div>
              <div class="form-group row">
                <%= f.label :price,"Prix d'offre", class:'col-4 col-form-label'%>
                <div class="col-8">
                  <div class="input-group">
                    <div class="input-group-addon">
                      <i class="fa fa-usd"></i>
                    </div>
                    <%= f.text_field :price,placeholder: "Prix d'offre" ,class:"form-control here"%>
                  </div>
                </div>
              </div>
              <div class="form-group row">
                 <%= f.label :display_usd ,'Prix en USD', class: 'col-4' %>  
                <div class="col-8">
                  <div class="form-check form-check-inline">
                    <label class="form-check-label">
                      <%= f.check_box :display_usd, class:'form-check-input' %>
                      USD
                    </label>
                  </div>
                </div>
              </div>
              <div class="form-group row">
                <%= f.label :category_id,class:"col-4 col-form-label" %>
                <div class="col-8">
                  <%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "choose a category"}, {class: "form-control here"}%>
                </div>
              </div>
              <div class="form-group row">
                <label for="select1" class="col-4 col-form-label">Localisation</label> 
                <div class="col-8">
                  <select id="select1" name="select1" class="form-control">
                    <option value="rabbit">Rabbit</option>
                    <option value="duck">Duck</option>
                    <option value="fish">Fish</option>
                  </select>
                </div>
              </div>
               <div class="form-group row">
                <%= f.label :image, "Image Principale", class:'col-4 col-form-label' %>   
                <div class="col-8">
                <%= f.file_field :image, class:'form-control here'%>
                </div>
              </div> 
              <div class="form-group row">
                <label class="col-4">Condition</label> 
                <div class="col-8">
                  <div class="form-check form-check-inline">
                    <label class="form-check-label">
                      <input name="radio1" type="radio" class="form-check-input" value="rabbit">
                            Usé
                    </label>
                  </div>
                  <div class="form-check form-check-inline">
                    <label class="form-check-label">
                      <input name="radio1" type="radio" class="form-check-input" value="duck">
                            Neuf
                    </label>
                  </div>
                  <div class="form-check form-check-inline">
                    <label class="form-check-label">
                      <input name="radio1" type="radio" class="form-check-input" value="fish">
                            normal
                    </label>
                  </div>
                </div>
              </div>
              <div class="form-group row">
                 <%= f.label :description,'Produit Description', class:'col-4 col-form-label' %> 
                <div class="col-8">
                  <%= f.text_area :description,class:" form-control here " do%>  
                  <span id="textareaHelpBlock" class="form-text text-muted">veuillez donner une description exacte de votre produit.</span>
                  <%end%>
                </div>
              </div>
              <div class="form-group row">
                <%= f.label :image, "Image additionel", class:'col-4 col-form-label' %>   
                <div class="col-8">
                <%= file_field_tag "images[]", type: :file, multiple: true, class:'form-control here'%>
                </div>
              </div> 
              <div class="form-group row">
                <label class="col-4">Sauvegarder sans publier</label> 
                <div class="col-8">
                  <div class="form-check form-check-inline">
                    <label class="form-check-label">
                      <input name="radio" type="radio" class="form-check-input" value="rabbit">
                            Unpublished
                    </label>
                  </div>
                </div>
              </div>
           </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>
            <%= f.button "Publier Produit" , class: 'btn btn-primary pull-right', data: {disable_with: "<i class='fa fa-spinner fa-spin'></i> Publication en cours..."} %> 
          </div>
          <%end%>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

1 个答案:

答案 0 :(得分:1)

如果您使用HTML点击create操作,则会出现此错误。 create通常没有关联的视图;您使用它来处理实体,并将用户重定向到其他地方。

因此,您应该能够使用:

redirect_to @listing

在控制器代码的末尾。

使用@listing有点Rails魔法 - 它通常会显示为redirect_to listing_path(@listing)

  def create
    @listing = Listing.new(listing_params)
    if @listing.save
      if params[:images]
          params[:images].each { |image|
            @listing.pictures.create(image: image)
          }
       end
     (@users - [current_user]).each do |user|
        Notification.create(recipient: user, actor: current_user, action: "posted", notifiable: @listing)
     end   
     flash[:notice]= "L'annonce #{@listing.listing_number} a eté publiee avec succès."
     redirect_to @listing 
    end
  end

这样做吗?

另一种常见做法是根据对象是否成功保存到数据库而采用不同的方法。例如:

def create
  @listing = Listing.new(listing_params)
  if @listing.save
    ...
    redirect_to @listing, notice: "..."
  else
    flash.now[:alert] = "Listing failed to save" 
    render :new
  end
end

使用此生成器的一个好方法是使用生成器来查看Rails默认处理的方式 - 您可以在终端中使用以下内容进行挖掘:rails g controller test_controller

希望有所帮助 - 如果您有任何问题,请告诉我。