Rails - Liking Comments Made on Posts - NoMethodError -


for application, have users, can create project postings. on each project posting, can make comments have made blogupdate model. want users able blogupdates made on each project page.

so, created bloglike model. when try render like/unlike button, following error:

nomethoderror in projects#blogs undefined method `bloglikes_path' extracted source (around line #11): 11:     <%= form_for(current_user.bloglikes.build(blogupdate_id: blogupdate.id)) |f| %> 

question: note, have not built controller actual create/destroy function in bloglikes controller; looking @ attached code below, know how can resolve error like/unfollow button renders?

schema.rb

create_table "bloglikes", :force => true |t|    t.integer  "user_id"    t.integer  "blogupdate_id"    t.datetime "created_at",    :null => false    t.datetime "updated_at",    :null => false end  add_index "bloglikes", ["blogupdate_id"], :name => "index_bloglikes_on_blogupdate_id" add_index "bloglikes", ["user_id", "blogupdate_id"], :name => "index_bloglikes_on_user_id_and_blogupdate_id", :unique => true add_index "bloglikes", ["user_id"], :name => "index_bloglikes_on_user_id" 

user.rb

has_many :bloglikes, foreign_key: "user_id" has_many :liked_blogupdates, through: :bloglikes, source: :blogupdate 

blogupdate.rb

has_many :bloglikes, foreign_key: "blogupdate_id" has_many :liked_by, through: :bloglikes, source: :user  def liking_blogupdate?(blogupdate)    bloglikes.find_by_blogupdate_id(blogupdate.id) end  def like_blogupdate!(blogupdate)    bloglikes.create!(blogupdate_id: blogupdate.id) end  def blogupdate_unlike!(blogupdate)    bloglikes.find_by_blogupdate_id(blogupdate.id).destroy end 

bloglike.rb

class bloglike < activerecord::base   attr_accessible :blogupdate_id    belongs_to :user, foreign_key: "user_id"   belongs_to :blogupdate, foreign_key: "blogupdate_id" end 

projects_controller.rb

def blogs     @project = project.find(params[:id])     @blogupdates = @project.blogupdates.newest.page(params[:blogupdates_page]).per_page(5) end 

views/projects/blogs.html.erb

<%= render 'blogs' %> 

views/projects/_blogs.html.erb

<%= render @blogupdates %> 

views/blogupdates/_blogupdates.html.erb

<%= blogupdate.liked_by.count %> <% if current_user.liking_blogupdate?(blogupdate) %>   <%= form_for(current_user.bloglikes.find_by_blogupdate_id(blogupdate),                html: { method: :delete }) |f| %>     <%= f.submit "unlike", class: "btn btn-medium" %>   <% end %> <% else %>   <%= form_for(current_user.bloglikes.build(blogupdate_id: blogupdate.id)) |f| %>       <div><%= f.hidden_field :blogupdate_id %></div>       <%= f.submit "like", class: "btn btn-medium btn-primary" %>    <% end %> <% end %>  <p><%= raw blogupdate.content %></p> 

update: noted below @dan, forgot update routes.rb file. added "resources :bloglikes" , worked now.

you didn't post routes.rb file i'd wager problem at. undefined method related routes (e.g. bloglikes_path) typically indicates you've not defined routes.

add resources :bloglikes project's routes.rb file , see if resolves issue.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -