Ruby On Rails. Destroy Method does not work if link has been voted on *Reddit Clone* -


i able delete links assigned each user, unless voted upon. if link has votes on delete method being called no longer works.

here link website deployed on heroku. if sign up. submit link, vote or down , try delete see error experiencing.

https://salty-eyrie-2549.herokuapp.com/

edit: after launching app live found code works long app not being hosted locally... strange...

this error getting.

nomethoderror in linkscontroller#destroy

undefined method `name' nil:nilclass

extracted source (around line #59):

# delete /links/1.json def destroy     @link.destroy     respond_to |format|       format.html { redirect_to links_url, notice: 'link destroyed.' }       format.json { head :no_content } 

rails.root: /users/ipbyrne/firstrailsapp/raddit

application trace | framework trace | full trace app/controllers/links_controller.rb:59:in `destroy' request

parameters:

{"_method"=>"delete",  "authenticity_token"=>"3jyyngnphhdvcq7tzq64t+ouqjinfnwxqxw25fgjgx4=",  "id"=>"5"} 

i using act_as_votable ruby gem.

the first block below show.html.erb destroy method being called from

the second block links_controller.rb file.

<div class="page-header">      <h1><a href="<%= @link.url %>"><%= @link.title %></a><br> <small>submitted <%= @link.user.name %></small></h1>  </div>    <div class="btn-group">  	<%= link_to 'visit url', @link.url, class: "btn btn-primary" %>  </div>    <div class="btn-group pull-right">    <%= link_to like_link_path(@link), method: :put, class: "btn btn-default btn-sm" %>      <span class="glyphicon glyphicon-chevron-up"></span>      upvote      <%= @link.get_upvotes.size %>    <% end %>    <%= link_to dislike_link_path(@link), method: :put, class: "btn btn-default btn-sm" %>      <span class="glyphicon glyphicon-chevron-down">      downvote      <%= @link.get_downvotes.size %>    <% end %>  </div>    <% if @link.user == current_user -%>  	<div class="btn-group">  		<%= link_to 'edit', edit_link_path(@link), class: "btn btn-default" %>  		<%= link_to 'destroy', @link, method: :delete, data: { confirm: 'are sure?' }, class: "btn btn-default" %>  	</div>  <% end %>    <h3 class="comments_title">    <%= @link.comments.count %> comments  </h3>    <div id="comments">    <%= render :partial => @link.comments %>  </div>  <%= simple_form_for [@link, comment.new]  |f| %>    <div class="field">      <%= f.text_area :body, class: "form-control" %>    </div>    <br>    <%= f.submit "add comment", class: "btn btn-primary" %>  <% end %>
class linkscontroller < applicationcontroller    before_action :set_link, only: [:show, :edit, :update, :destroy]    before_action :authenticate_user!, except: [:index, :show]    before_action :authorized_user, only: [:edit, :update, :destroy]      # /links    # /links.json    def index      @links = link.all    end      # /links/1    # /links/1.json    def show    end      # /links/new    def new      @link = current_user.links.build    end      # /links/1/edit    def edit    end      # post /links    # post /links.json    def create      @link = current_user.links.build(link_params)        respond_to |format|        if @link.save          format.html { redirect_to @link, notice: 'link created.' }          format.json { render :show, status: :created, location: @link }        else          format.html { render :new }          format.json { render json: @link.errors, status: :unprocessable_entity }        end      end    end      # patch/put /links/1    # patch/put /links/1.json    def update      respond_to |format|        if @link.update(link_params)          format.html { redirect_to @link, notice: 'link updated.' }          format.json { render :show, status: :ok, location: @link }        else          format.html { render :edit }          format.json { render json: @link.errors, status: :unprocessable_entity }        end      end    end      # delete /links/1    # delete /links/1.json    def destroy      @link.destroy      respond_to |format|        format.html { redirect_to links_url, notice: 'link destroyed.' }        format.json { head :no_content }      end    end      def upvote      @link = link.find(params[:id])      @link.upvote_by current_user      redirect_to :back    end      def downvote      @link = link.find(params[:id])      @link.downvote_from current_user      redirect_to :back    end      private      # use callbacks share common setup or constraints between actions.      def set_link        @link = link.find(params[:id])      end        def authorized_user        @link = current_user.links.find_by(id: params[:id])        redirect_to links_path, notice: "not authorized edit link" if @link.nil?      end        # never trust parameters scary internet, allow white list through.      def link_params        params.require(:link).permit(:title, :url)      end  end

full trace error

activerecord (4.0.0) lib/active_record/associations/has_many_association.rb:80:in `cached_counter_attribute_name' activerecord (4.0.0) lib/active_record/associations/has_many_association.rb:103:in `inverse_updates_counter_cache?' activerecord (4.0.0) lib/active_record/associations/has_many_association.rb:113:in `delete_records' activerecord (4.0.0) lib/active_record/associations/collection_association.rb:493:in `remove_records' activerecord (4.0.0) lib/active_record/associations/collection_association.rb:486:in `block in delete_or_destroy' activerecord (4.0.0) lib/active_record/associations/collection_association.rb:152:in `block in transaction' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:200:in `transaction' activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction' activerecord (4.0.0) lib/active_record/associations/collection_association.rb:151:in `transaction' activerecord (4.0.0) lib/active_record/associations/collection_association.rb:486:in `delete_or_destroy' activerecord (4.0.0) lib/active_record/associations/collection_association.rb:247:in `destroy' activerecord (4.0.0) lib/active_record/associations/collection_association.rb:170:in `destroy_all' activerecord (4.0.0) lib/active_record/associations/has_many_association.rb:26:in `handle_dependency' activerecord (4.0.0) lib/active_record/associations/builder/association.rb:97:in `has_many_dependent_for_votes_for' activesupport (4.0.0) lib/active_support/callbacks.rb:377:in `_run__4103742494544083763__destroy__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' activerecord (4.0.0) lib/active_record/callbacks.rb:289:in `destroy' activerecord (4.0.0) lib/active_record/transactions.rb:265:in `block in destroy' activerecord (4.0.0) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction' activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction' activerecord (4.0.0) lib/active_record/transactions.rb:323:in `with_transaction_returning_status' activerecord (4.0.0) lib/active_record/transactions.rb:265:in `destroy' app/controllers/links_controller.rb:59:in `destroy' actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action' actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action' actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action' activesupport (4.0.0) lib/active_support/callbacks.rb:443:in `_run__1823692881447443679__process_action__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action' actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action' activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument' activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument' actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action' actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action' activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process' actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process' actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch' actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call' warden (1.2.3) lib/warden/manager.rb:35:in `block in call' warden (1.2.3) lib/warden/manager.rb:34:in `catch' warden (1.2.3) lib/warden/manager.rb:34:in `call' rack (1.5.5) lib/rack/etag.rb:23:in `call' rack (1.5.5) lib/rack/conditionalget.rb:35:in `call' rack (1.5.5) lib/rack/head.rb:11:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call' rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context' rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call' activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call' activerecord (4.0.0) lib/active_record/migration.rb:369:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1414070385979111640__call__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app' railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged' railties (4.0.0) lib/rails/rack/logger.rb:21:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.5.5) lib/rack/methodoverride.rb:21:in `call' rack (1.5.5) lib/rack/runtime.rb:17:in `call' activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call' rack (1.5.5) lib/rack/lock.rb:17:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call' railties (4.0.0) lib/rails/engine.rb:511:in `call' railties (4.0.0) lib/rails/application.rb:97:in `call' rack (1.5.5) lib/rack/lock.rb:17:in `call' rack (1.5.5) lib/rack/content_length.rb:14:in `call' rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service' /users/ipbyrne/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service' /users/ipbyrne/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run' /users/ipbyrne/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread' 

link.rb

class link < activerecord::base      acts_as_votable      belongs_to :user      has_many :comments end 

schema

# encoding: utf-8 # file auto-generated current state of database. instead # of editing file, please use migrations feature of active record # incrementally modify database, , regenerate schema definition. # # note schema.rb definition authoritative source # database schema. if need create application database on # system, should using db:schema:load, not running migrations # scratch. latter flawed , unsustainable approach (the more migrations # you'll amass, slower it'll run , greater likelihood issues). # # it's recommended check file version control system.  activerecord::schema.define(version: 20150722133429)    create_table "comments", force: true |t|     t.integer  "link_id"     t.text     "body"     t.integer  "user_id"     t.datetime "created_at"     t.datetime "updated_at"   end    add_index "comments", ["link_id"], name: "index_comments_on_link_id"   add_index "comments", ["user_id"], name: "index_comments_on_user_id"    create_table "links", force: true |t|     t.string   "title"     t.string   "url"     t.datetime "created_at"     t.datetime "updated_at"     t.integer  "user_id"   end    add_index "links", ["user_id"], name: "index_links_on_user_id"    create_table "users", force: true |t|     t.string   "email",                  default: "", null: false     t.string   "encrypted_password",     default: "", null: false     t.string   "reset_password_token"     t.datetime "reset_password_sent_at"     t.datetime "remember_created_at"     t.integer  "sign_in_count",          default: 0,  null: false     t.datetime "current_sign_in_at"     t.datetime "last_sign_in_at"     t.string   "current_sign_in_ip"     t.string   "last_sign_in_ip"     t.datetime "created_at"     t.datetime "updated_at"     t.string   "name"   end    add_index "users", ["email"], name: "index_users_on_email", unique: true   add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true    create_table "votes", force: true |t|     t.integer  "votable_id"     t.string   "votable_type"     t.integer  "voter_id"     t.string   "voter_type"     t.boolean  "vote_flag"     t.string   "vote_scope"     t.integer  "vote_weight"     t.datetime "created_at"     t.datetime "updated_at"   end    add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"   add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"  end 

link model

class link < activerecord::base       acts_as_votable      belongs_to :user      has_many :comments  end 

sounds you're unable delete because link has associated votes. have update relationships in link model allows allow_destroy: true, in case delete associations well.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -