如何修复丢失的模板,应用程序/在轨道中创建(how to fix missing template , application/create in rails)

编程入门 行业动态 更新时间:2024-10-27 14:24:04
如何修复丢失的模板,应用程序/在轨道中创建(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"

,任何时候我试图创建一个对象,在堆栈​​溢出搜索后,一些建议重定向或渲染,最初在我的创建操作,没有明确的重定向和对象创建后,重定向到显示页面(预期的行为)。尝试了两种解决方案,但仍然得到相同的错误,并且根据我的理解,不需要为创建操作提供相应的视图。

我如何将创建操作重定向到显示页面而不创建视图?

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

i keep getting this missing template error

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

,anytime i tried to create an object,after a couple of search in stack overflow , some suggest to redirect or render , initially in my create action , there was no explicit redirect and after object creation the redirect was done to the show page (the intended behavior).Tried both solutions , but still getting the same error and in my understanding there's no need to have a corresponding views for the create action.

How do i got the create action redirect to the show page without creating a a view?.

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

The form that trigger the create action is rendered via a modal

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

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

This error comes if you're hitting the create action using HTML. create doesn't typically have an associated view; you use it to process an entity, and redirect the user elsewhere.

Therefore, you should just be able to use:

redirect_to @listing

at the end of your controller code.

Using @listing is a bit of Rails magic - it would more commonly appear as redirect_to listing_path(@listing).

I.E.

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

Does that do it?

Another common practice is to have different approaches depending on whether or not an object successfully saves to the db. For example:

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

A good way to play about with this is to use the generator to see how Rails handles things by default - you can use the following in the terminal to have a dig around: rails g controller test_controller.

Hope that helps - let me know if you've any questions.

更多推荐

本文发布于:2023-07-14 17:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   轨道   模板   fix   missing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!