0

I have gotten a comment model to work based on railscast 238 but I would like to add voting and rating as well.

My current show for the article controller is:

 def show
    @article = Article.find(params[:id])
    @commentable = @article
    @comments = @commentable.comments
    @comment = Comment.new


    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @article }
    end
  end

I have tried replicating the same process and adding rateable but I do not know how to integrate it into the show action. I have considered creating a universal function from application controller and then trying to assign it in to multiple things(newb) in the show function but that seems over complicated.

I tried adding:

@rateable = @article
@ratings = @rateable.ratings
@rating = Rating.new

It did not work. But now I am thinking it might work if I assign it to

@ratings = Article.find(params[:id]}
@ratings = @rateable.ratings
@rating = Rating.new

Even if that does work, there must be cleaner way to do this.

Edit:

Error after correction from this line of code, which is identical to the working comment version.

<%= form_for [@rateable, @rating] do |f| %>
  <% if @rating.errors.any? %>
    <div class="error_messages">
      <h2>Please correct the following errors.</h2>
      <ul>
      <% @rating.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.text_area :content, rows: 8 %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
BoDogLLL
  • 82
  • 1
  • 9
  • You just want the parameters `@rateable`, `@ratings`, and `@rating` to all be available in the `show` view? – drew.cuthbert Nov 15 '13 at 07:17
  • Also, why set `@commentable = @article` and `@rateable = @article`? Why not just refer to `@article`? – drew.cuthbert Nov 15 '13 at 07:19
  • When I do that I get `undefined method article_ratings_path' ` https://github.com/railscasts/154-polymorphic-association-revised/blob/master/blog-after/app/controllers/articles_controller.rb I am pretty much going off this and don't know how to add another object to the mix. So the answer to your first question is yes. – BoDogLLL Nov 15 '13 at 07:51
  • Oh okay, you can just add the second block of code from your question into the `show` method and those variables will be available in your view as well. – drew.cuthbert Nov 15 '13 at 08:26
  • I notice you said doing that "didn't work". If it doesn't I'll need to see the error message. Also, why did you add the `respond_to` block into your version of the `show` method? In the json response you refer to `@articleyi` is that a typo? – drew.cuthbert Nov 15 '13 at 08:28
  • and if it is coming back with the undefined method then that is an error somewhere else in the code? – BoDogLLL Nov 15 '13 at 08:29
  • What method is it saying is undefined? – drew.cuthbert Nov 15 '13 at 08:32
  • `article_ratings_path` And yes it is a typo just on stack overflow, not in the code. – BoDogLLL Nov 15 '13 at 08:34

1 Answers1

1

Okay, think I figured it out. Just change <%= f.submit %> to:

<%= f.submit 'Submit', { controller: 'ratings', action: 'create', method: 'post' } %>

In your routes.rb file you will also need put '/ratings/create' => 'ratings#create' or simply resources :ratings

This will route your form submit to ratings#create which I think is what you want.

drew.cuthbert
  • 1,005
  • 1
  • 9
  • 21