繁体   English   中英

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

[英]how to fix missing template , application/create in 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"

,无论何时我尝试创建对象,在堆栈​​溢出中进行几次搜索后,有人建议重定向或渲染,最初在我的create动作中,没有显式的重定向,并且在对象创建后,将重定向完成到显示页面(尝试了两种解决方案,但仍然遇到相同的错误,据我所知,create操作不需要相应的视图。

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

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

如果您使用HTML命中create动作,则会出现此错误。 create通常没有关联的视图; 您可以使用它来处理实体,然后将用户重定向到其他地方。

因此,您应该只能使用:

redirect_to @listing

在控制器代码的末尾。

使用@listing是Rails的一点魔力-它通常会显示为redirect_to listing_path(@listing)

IE

  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

希望对您有所帮助-如有任何疑问,请与我们联系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM