rails ajax to render form
我的页面上有一个"加入"按钮,单击该按钮时,用户
1 2 3 4 | *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 } %>"); |
这是部分评论
1 2 3 4 5 6 7 | <% if current_user.voted_on?(restaurant) %> <h4 class="text-muted text-center" style="margin: 0 0 10px;">Write a review</h4> <%= render :partial =>"comments/form", :locals => { :comment => @comment } %> <% end %> |
因此js文件正在渲染局部,而局部又渲染了另一个局部。
我遇到的错误是
1 2 | ActionView::Template::Error (First argument in form cannot contain nil or be empty): 1: <%= form_for([@commentable, @comment]) do |f| %> |
我认为这是我的当地人在comment_partial中存在的问题。有谁知道合适的解决方案?
谢谢
我已经在comments_form部分
中尝试过此操作
1 | <%= render :partial =>"comments/form", :locals => { :restaurant => @commentable, :comment => @comment } %> |
根据聊天会话,在操作
1 2 3 4 5 6 7 8 9 10 | 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 |