3

I'm still getting the hang of Rails. Here I'm using Rails 3 and the goal basically is to have an AJAX call triggered when I click the subscribe button the post_form partial is rendered beneath for the topic I have just subscribed to. The button then becomes an unsubscibe button and the post_form partial is removed. The toggling of the button alone works (i.e: by removing the second line in the two immediately following snippets), but the rendering of the *post_form* partial does not.

The problem is I can't seem to get the right syntax and/or passing of parameters in the two following partials. The topic object is just not passed and I get an invalid model_name for NilClass error when clicking on the subscribe or unsubscribe button. If I refresh the page manually, the partial is rendered or hidden the correct way, so it's really just the AJAX part that isn't working right.

views/subscription/create.js.erb

$("#subscription_form").html("<%= escape_javascript(render('users/unsubscribe')) %>");
$("#post_form").html("<%= escape_javascript(render('shared/post_form', :topic => @topic)) %>");

views/subscription/destroy.js.erb

$("#subscription_form").html("<%= escape_javascript(render('users/subscribe')) %>");
$("#post_form").html("<%= escape_javascript(render('shared/post_form', :topic => @topic)) %>");

views/users/_subscription_form.html.erb

<% unless current_user?(@user) %>
  <div id="subscription_form">
  <% if current_user.subscribed?(@topic) %>
    <%= render 'users/unsubscribe', :topic => @topic %>
  <% else %>
    <%= render 'users/subscribe', :topic => @topic %>
  <% end %>
  </div>
<% end %>

controllers/subscriptions_controller.rb

class SubscriptionsController < ApplicationController
      before_filter :signed_in_user

      respond_to :html, :js

      def create
        @topic = Topic.find(params[:subscription][:topic_id])
        current_user.subscribe!(@topic)
        respond_with @topic
      end

      def destroy
        @topic = Subscription.find(params[:id]).topic
        current_user.unsubscribe!(@topic)
        respond_with @topic
      end
   end

views/shared/_post_form.html.erb

<%= form_for(@post) do |f| %>
  <div class="field">
    <%= f.hidden_field :topic_id, :value => @topic.id %>
    <%= f.text_area :content, placeholder: "Tell us about it ..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

If it is of any help, the relationships are:

post -> belongs_to -> topic and topic -> has_many -> posts

1
  • Is there a reason why you're doing a "respond_with @topic"? You should be able to leave those lines out since you have named your views conventionally. Commented Jul 20, 2012 at 19:15

1 Answer 1

3

Looks like you're using the variable "@post" in the "views/_post_form.html.erb" file.

<%= form_for(@post) do |f| %>

Since you aren't setting that variable anywhere in your actions you would get a null reference error.

You would need to do something like this:

def create
  @post = Post.find(the_post_id)
  @topic = Topic.find(params[:subscription][:topic_id])
  current_user.subscribe!(@topic)
  respond_with @topic
end

Also you are passing in the "topic" variable as a local but accessing it as an instance variable. You should change the your _post_form.html.erb file to look like this:

<%= form_for(@post) do |f| %>
  <div class="field">
    <%= f.hidden_field :topic_id, :value => topic.id %>
    <%= f.text_area :content, placeholder: "Tell us about it ..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

I don't have my ruby environment readily available so I can't verify that this will solve your problem but I think it should move you in the right direction.

Sign up to request clarification or add additional context in comments.

1 Comment

Environment or not, you nailed it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.