rails ajax渲染表单

时间:2014-04-21 14:34:28

标签: javascript ruby-on-rails ajax ruby-on-rails-4

我的页面上有一个“加入”按钮,点击后,用户joins模型。与关注按钮类似。在我的join.js.erb文件中,我在用户加入后呈现部分内容,例如部分显示取消加入按钮以及他们现在可以在模型上发表评论的表单。这是它的外观。

*join.js.erb*

$("#restaurant-<%= @restaurant.id %>").html("<%= escape_javascript render :partial => 'restaurants/join_button', :locals => { :restaurant => @restaurant } %>");
$("#restaurantCommentForm").html("<%= escape_javascript render :partial => 'restaurants/comments_form', :locals => { :restaurant => @restaurant } %>");

这是评论部分

<% if current_user.voted_on?(restaurant) %>
  <div class="section-wrapper section-wrapper-two">
    <h4 class="text-muted text-center" style="margin: 0 0 10px;">Write a review</h4>

    <%= render :partial => "comments/form", :locals => { :comment => @comment } %>
  </div>
<% end %>

因此js文件渲染了一个部分,它渲染了另一部分,都是本地的。

我得到的错误是First argument in form cannot contain nil or be empty

ActionView::Template::Error (First argument in form cannot contain nil or be empty):
  1: <%= form_for([@commentable, @comment]) do |f| %>

我认为这是我的本地人在comment_partial中的一个问题。有谁知道这个适当的解决方案? 感谢

我已经在comments_form部分

中尝试了这个
<%= render :partial => "comments/form", :locals => { :restaurant => @commentable, :comment => @comment } %>

1 个答案:

答案 0 :(得分:1)

根据聊天会话,@comment@commentable个实例变量未在操作join中设置。因此,错误First argument in form cannot contain nil or be empty

 def join
    begin
      @vote = current_user.vote_for(@restaurant = Restaurant.friendly.find(params[:id]))     
      @commentable = @restaurant ## Set the value
      @comment = Comment.new    ## Set the value
      respond_with @restaurant, :location => restaurant_path(@restaurant)
    rescue ActiveRecord::RecordInvalid
      redirect_to restaurant_path(@restaurant)
    end
  end