Arch
Arch

Reputation: 133

Rails 4 Button to Action

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

Upvotes: 0

Views: 63

Answers (1)

Andy Gauge
Andy Gauge

Reputation: 1428

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

Rails Guide routing from the outside in.

Upvotes: 3

Related Questions