我试图让我的创建操作在我的rails应用中运行。当我有这两行时
$('#pit_form').remove(); //remove form
$('#new_link').show(); //show new link again
它正常运行,并删除表单并添加链接,但我不能用我的逃生显示新的坑。我尝试了一些事情,但总是给我这个错误
ActionView::Template::Error (Missing partial pits/_pit with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/Users/markhustad/projects/fire_2/app/views"
* "/usr/local/rvm/gems/ruby-2.1.1/gems/devise-3.2.4/app/views"
):
1: $('#pit_form').remove(); //remove form
2: $('#new_link').show(); //show new link again
3: $('#pit_index').append(<%= j (render(@pit)) %>);
app/views/pits/create.js.erb:3:in `_app_views_pits_create_js_erb___3365989432298988625_2214355520'
app/controllers/pits_controller.rb:20:in `create'
当我添加此行或类似的东西时(不确定为什么我需要_pit partial)
$('#pit_index').append(<%= j (render(@pit)) %>);
我的坑控制器目前
class PitsController < ApplicationController
before_action :current_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def new
@pit = Pit.new
end
def index
@pit = Pit.all
@user = User.find_by(params[:id])
@pits = Pit.paginate(:page => params[:page]).order('created_at DESC').group_by { |pit| pit.created_at.strftime("%B %Y") }
end
def create
@pit = current_user.pits.create(pit_params)
respond_to do |format|
format.html { redirect_to pits_path}
format.js
end
end
def show
@pit = Pit.find(params[:id])
end
def edit
end
def update
@pit = Pit.find(pit_params[:id])
if @pit.update_attributes(pit_params)
redirect_to @pit
end
end
def destroy
@pit = Pit.find(params[:id])
@pit.destroy
end
def upvote
@pit = Pit.find(params[:pit_id])
@pit.upvote_from current_user
redirect_to pit_path(@pit)
end
def downvote
@pit = Pit.find(params[:pit_id])
@pit.downvote_from current_user
redirect_to pit_path(@pit)
end
private
def correct_user
@pit = current_user.pits.find_by_id(params[:id])
redirect_to root_path if @pit.nil?
end
def pit_params
params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id)
end
end
坑指数
<div class = "container list-pits">
<%= link_to "Add New Pit", new_pit_path, id: "new_link", remote: true, class: "btn btn-default" %>
<br>
<br>
<% @pit.each do |pit| %>
<div class = "container">
<div class = "well", id = "pit_index">
<h3 id="pit-title"><%= link_to pit.topic, pit_path(pit) %></h3>
<p>by <%= link_to pit.author, '#' %></p>
<br>
<p><%= pit.summary %></p>
<p>Replies (<%= pit.comments.count %>)</p>
<br>
<p>Pit Created by: <%= link_to pit.user.name, pit.user %> on <%= pit.created_at %></p>
<%= link_to "View Pit", pit_path(pit), class: "btn btn-primary" %>
<%= link_to "Delete Pit", pit_path(pit), remote: true, method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
</div>
<% end %>
</div>
在坑中形成
<div class="container new-pit">
<%= render 'devise/shared/error_messages', obj: @pit %>
<%= form_for @pit, remote: true, :html => {:multipart => true} do |f| %>
<div class = "form-horizontal", id = "pit_form">
<div class="form-group">
<%= f.label :topic, class: "col-sm-2 control-label" %>
<div class="col-sm-6">
<%= f.text_field :topic, class: "form-control", autofocus: true %>
</div>
</div>
<div class="form-group">
<%= f.label :author, class: "col-sm-2 control-label" %>
<div class="col-sm-6">
<%= f.text_field :author, class: "form-control", :placeholder => "Enter name of book author, lecturer, or presenter" %>
</div>
</div>
<div class="form-group">
<%= f.label :summary, class: "col-sm-2 control-label" %>
<div class="col-sm-6">
<%= f.text_area :summary, class: "form-control", :placeholder => "Present an argument for or against the material presented and state why you think it is accurate or not" %>
</div>
</div>
<div class="form-group">
<%= f.label :image, class: "col-sm-2 control-label" %>
<div class="col-sm-6">
<%= f.file_field :image, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :video, class: "col-sm-2 control-label" %>
<div class="col-sm-6">
<%= f.text_field :video_url, class: "form-control", :placeholder => "example: //www.youtube.com/embed/hBHYdK9xtYs (video is optional)" %>
<p class = "youtube-instruction">Must use the shared -> embed option</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<%= f.submit "Start Pit", class: "btn btn-primary" %>
</div>
</div>
</div>
</div>
</div>
<% end %>
我知道我在这附近但需要更多方向。感谢。
答案 0 :(得分:0)
你需要两件事。首先,pit.js.erb
文件具有:
$('#pit_form').remove();
$('#new_link').show();
$('#pit_index').append(<%= j (render(@pit)) %>);
我想你已经有了这个。
您还需要_pit.html.erb
文件夹下的/pits
文件,该文件描述了坑索引上各个坑项目的布局。
操作顺序如下:
remote: true
选项完成,该选项使用Unobstrusive JavaScript。create
在控制器的format.js
操作中处理响应。默认情况下,这会呈现与操作名称对应的.js.erb文件(即create.js.erb
)。_pit.html.erb
或_pit.js.erb
模板,这两个模板都缺失。如果您已在特定文件夹中拥有pit
模板,则可以在渲染操作中指定它,如下所示:
$('#pit_index').append("<%= j (render 'myfolder/pit') %>");
我希望这会有所帮助。