1

I have a post model and an upvote model.

upvote.rb

class Upvote < ActiveRecord::Base
  has_one :user
  belongs_to :post
end
# == Schema Information
#
# Table name: upvotes
#
#  id         :integer         not null, primary key
#  user_id    :integer
#  post_id    :integer
#  comment    :text
#  created_at :datetime
#  updated_at :datetime
#

Now within posts/index I want to add an upvote for the current user and the post.

Some research pointed me to write a helper posts_helper.rb

module PostsHelper
  def upvote_post(post)
    @upvote = Upvote.new
    @upvote.user_id = current_user.id
    @upvote.post_id = post.id
    if @upvote.save
     flash.now[:notice] = 'Upvote was successfully created.'
    end
  end
end

Inside my view I want to call the helper only on clicking a link but can't seem to get link_to working properly.

<% @posts.each do |post| %>
  <tr>
    <td><%= link_to "upboats" upvote_post(post) %></td>
    <td><%= post.name %></td>
...

I get the error syntax error, unexpected tIDENTIFIER, expecting ')' and can't seem to find any good alternatives.

What am I missing here?


UPDATED

It was a comma. facepalm

Plenty of other issues, but that was what led to the error.

<% @posts.each do |post| %>
  <tr>
    <td><%= link_to "upboats", upvote_post(post) %></td>
    <td><%= post.name %></td>
...
4
  • 3
    A comma, you're missing a comma. And your helper smells like a controller. Commented Oct 16, 2011 at 6:22
  • In retrospect it ought to be a controller. I'm just hacking on 0 sleep. Commented Oct 16, 2011 at 7:17
  • Arguable. Definitely lower than average though. Commented Oct 16, 2011 at 7:57
  • And don't forget to add an answer and accept it later, so that others may skip your (now open) question. Commented Oct 17, 2011 at 12:06

1 Answer 1

1

Closing open question.

It was a comma.


UPDATED

It was a comma. facepalm

Plenty of other issues, but that was what led to the error.

<% @posts.each do |post| %>
  <tr>
    <td><%= link_to "upboats", upvote_post(post) %></td>
    <td><%= post.name %></td>
...
Sign up to request clarification or add additional context in comments.

Comments

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.