2014-04-21 174 views
0

我在页面上有一个“加入”按钮,当点击时,用户joins模型。类似于按钮。在我的join.js.erb文件中,我在用户加入后呈现部分内容,如部分显示不加入按钮以及他们现在可以评论模型的表单。这是它的外观。rails ajax渲染表格

*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 } %> 
+0

您在哪里设置了@ @ commentable'和'@ comment'的值? –

+0

在控制器中。 '@commentable = @ restaurant',因为这里的可评论模型是餐厅模型。 '@comment = Comment.new' –

回答

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

与表单相同的错误不能包含零或为空 –

+0

让我们在聊天上进行调试http://chat.stackoverflow.com/rooms/48530/ror –