簡體   English   中英

Rails Ajax渲染表單

[英]rails ajax to render form

我的頁面上有一個“加入”按鈕,單擊該按鈕后,用戶即可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中的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 } %>

根據聊天會話,未在action join設置@comment@commentable實例變量。 因此, First argument in form cannot contain nil or be empty的錯誤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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM