0
votes

I am creating a button which will send 100 points from the user to the owner of the article. For now, I wrote the following code, but Rails says:

ActionController::UrlGenerationError in Articles#show | No route matches {:action=>"upvote", :controller=>"budgets", :id=>7}

What could be wrong?

Controller:

 class BudgetsController < ApplicationController
    def upvote
        @article = Article.find(params[:id])
        if @article.budgets.exists?(user_id: current_user.id)
            @article.budgets.create
            @article.budgets.user_id = current_user.id
            @article.budgets.user_gill = 1000
        end

        @budget = @article.budgets.find(user_id: current_user.id)

    end
end

html:

<div class = "row">
	<div class = "col-md-1">
		<%= button_to "+", {:action => "upvote", :controller => "budgets",  :id => @article.id }%>
	</div>
</div>

Model:

class Article < ActiveRecord::Base
    belongs_to :user
    has_many :comments
    has_and_belongs_to_many :budgets
end


class Budget < ActiveRecord::Base
    belongs_to :user
    has_and_belongs_to_many :articles
end

class User < ActiveRecord::Base
    has_many :articles
    has_many :comments
    has_one :budget
end

Routes:

 resources :articles do
    resources :comments
    resources :budgets
  end
  resources :users do
    resources :budgets
  end
1
How do you have routes setup for upvote? - vee
button_to method send a POST request by default. - Philidor
I edited the routes. thank you @vee - Arch
I didn't create any routes for upvote, thanks - Arch

1 Answers

3
votes
# config/routes.rb
resources :budgets do
  post :upvote, on: :member
end

Rails Guide routing from the outside in.