Justin
Justin

Reputation: 4940

rails ajax to render form

I have a "join" button on my page that when clicked, the user joins the model. Similar to follow button. In my join.js.erb file, I'm rendering partials after a user has joined, like the partial to show the unjoin button as well as a form where they can now comment on the model. Here is how it looks.

*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 } %>");

Here is the comment partial

<% 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 %>

So the js file is rendering a partial, which is rendering another partial, both with locals.

The error I'm getting is where 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| %>

I assume this is an issue with my locals in the comment_partial. Does anyone know a proper solution for this?? Thanks

I've tried this already in the comments_form partial

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

Upvotes: 0

Views: 323

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53048

As per the chat session, @comment and @commentable instance variables were not set in action join. Hence, the error 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

Upvotes: 1

Related Questions