我的ajax请求似乎运行正常。出现console.log,状态为200,参数已保存在数据库中。我在索引页面(home_controller)上呈现表单的方式有问题,或者类似的问题或一些愚蠢的HTML错误。一直在看着它,无法破译它。
这是我显示帖子的地方views / home / index.html.erb
<div class="row">
<div class="col-md-2 col-md-offset-4">
<%= link_to new_post_path, remote: true do %>
<button class="btn btn-default">+New Post</button>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-2" id="post-form" style="display:none;">
</div>
</div>
<div class="row">
<div class="col-md-7 col-md-offset-1" id="all-posts">
<%= render @posts %>
</div>
</div>
这是我的观点/职位/ create.js.erb
$('#all-posts').html(" <%= j (render @posts) %> ");
$('#post-form').slideUp(350);
console.log('hello')
这是我的帖子控制器controllers / posts_controller.rb
class PostsController < ApplicationController
respond_to :html, :js
def new
@post = Post.new
end
def create
@post = Post.create(post_params)
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
@post.update_attributes(post_params)
end
def destroy
@post = Post.find(params[:id])
@post.destroy
end
private
def post_params
params.require(:post).permit(:content)
end
end
这是我创建帖子的表格views / posts / _form.html.erb
<%= simple_form_for @post, remote: true do |f| %>
<%= f.input :content, input_html: {class: 'form-group' } %>
<%= f.button :submit, class: 'btn btn-success' %>
<% end %>
家庭控制器controllers / home_controller.rb
class HomeController < ApplicationController
def index
@posts = Post.all
end
end
您应该添加@posts = Post.all
到Posts控制器的create动作中,以使其可用于views / posts / create.js.erb。
我不认为将其添加到家庭控制器将使它在视图/帖子中可用。如果您希望跨控制器使用某些东西,则可以将其添加到Application控制器中,但在此我不做。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句