【问题标题】:rails ajax to render formrails ajax 渲染表单
【发布时间】:2014-04-21 14:34:28
【问题描述】:

我的页面上有一个“加入”按钮,单击该按钮时,用户 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 中的当地人的问题。有谁知道这个的正确解决方案? 谢谢

我已经在 cmets_form 部分尝试过

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

【问题讨论】:

  • @commentable@comment的值在哪里设置?
  • 在控制器中。 @commentable = @restaurant,因为这里可评论的模型是餐厅模型。 @comment = Comment.new

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


【解决方案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

【讨论】:

  • 同样的错误表单不能包含 nil 或为空
猜你喜欢
  • 2016-06-19
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 2015-04-30
相关资源
最近更新 更多