Ruby on rails 轨道4。在线出现的Javascript语法
我想在用户单击“newcomment”时附加一个视图,但我得到的是javascript的输出 comment/show.html.erbRuby on rails 轨道4。在线出现的Javascript语法,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我想在用户单击“newcomment”时附加一个视图,但我得到的是javascript的输出 comment/show.html.erb <%= link_to "new comment", new_article_comment_path(@article, :format => :js), :remote => true, :id => 'new_comment_link' %> :js),:remote=>true,:id=>“新建注释链接”%> new.
<%= link_to "new comment", new_article_comment_path(@article, :format => :js), :remote => true, :id => 'new_comment_link' %>
:js),:remote=>true,:id=>“新建注释链接”%>
new.js.erb
$("<%= escape_javascript render(:file => 'comments/new.html.erb') %>").insertAfter('#comments');
$('#new_comment').hide().slideDown();
$('#new_comment_link').hide();
$(“'comments/new.html.erb')%>”).insertAfter(“#comments”);
$(“#新注释”).hide().slideDown();
$(“#新建_注释_链接”).hide();
该输出以某种方式将我引向new.js.erb:
$("<form accept-charset=\"UTF-8\" action=\"/articles/3/comments\" class=\"new_comment\" id=\"new_comment\" method=\"post\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"✓\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"zSq3KpEbucFQLa6XStEJ/I0+CpKPLFYcU/WGIdneeMg=\" /><\/div>\n<div class=\"field\">\n<label for=\"comment_name\">Name<\/label><br />\n<input id=\"comment_name\" name=\"comment[name]\" type=\"text\" />\n<\/div>\n<div class=\"field\">\n<label for=\"comment_email\">Email<\/label><br />\n<input id=\"comment_email\" name=\"comment[email]\" type=\"text\" />\n<\/div>\n<div class=\"field\">\n<label for=\"comment_body\">Body<\/label><br />\n<textarea id=\"comment_body\" name=\"comment[body]\">\n<\/textarea>\n<\/div>\n<div class=\"actions\">\n<input name=\"commit\" type=\"submit\" value=\"Add\" />\n<\/div>\n<\/form>").insertAfter('#comments');
$('#new_comment').hide().slideDown();
$('#new_comment_link').hide();
$(“\n\nName
\n\n\n\nMail
\n\n\n正文
\n\n\n\n\n\n”)。插入后面(“#注释”);
$(“#新注释”).hide().slideDown();
$(“#新建_注释_链接”).hide();
尝试在渲染方法中使用:handlers和:formats,这种方法对我很有效
$("<%= escape_javascript render(:file => 'comments/new', :handlers => [:erb], :formats => [:html]) %>").insertAfter('#comments');
$('#new_comment').hide().slideDown();
$('#new_comment_link').hide();
我想_new.html.erb是你的一部分。试试看$(“”)。插入后面(“#注释”);抱歉,它不应该是一个部分,它是一个简单的视图new.html.erb当我在show.html上时,我单击“new Comment”而不是附加new.html,它会在new.js(javascript erb文件)重定向我。请尝试删除链接中的format:js。true,:id=>“新建注释链接”%>删除格式:js有效!但它不会在id=“comment”之后追加内容,而是替换孔体内容。当我点击的时候,我是打开的,我应该呆在同一个页面上,但是。。。。我在七里街。是的,这个方法也很有效,做的和“Bachan Smruty”一样,但我还有一个重定向0.0.0.0:3000/articles/4/comments/new。我应该呆在同一个页面上。您是否在您的评论控制器(新方法)中添加了
format.js
代码行?
def create
@comment = @article.comments.new(comment_params)
if @comment.save
redirect_to @article, :notice => 'Thanks for your comment'
format.js # default to render 'create.js.erb'
else
redirect_to @article, :alert => 'Unable to add comment'
format.js { render 'create_failed.js.erb' }
end
end