我试图在我的rails应用中实现like / different按钮。现在,当我点击“赞”按钮时,类似的内容会保存在数据库中,但我的控制台会发出此错误:
ArgumentError (wrong number of arguments (given 2, expected 1)):
我明白这是说我应该只提供1个参数而我提供2个,但是当我摆脱:投票论证时它说我错过了一个投票参数,但是当我摆脱它时我得到这个错误的另一个论点:
ActionView::Template::Error (No route matches {:action=>"index", :controller=>"votes", :id=>"36", :vote=>{:food_id=>36, :user_id=>15}} missing required keys: [:user_id]):
这是我的部分形式:
<% unless current_user.votes.pluck(:food_id).include?(food.id) %>
<%= button_to "Like This Food", user_votes_path({:user_id => current_user.id, :vote => { food_id: food.id, user_id: current_user.id }}), :remote => true, :method => "post" %>
<% else %>
<% vote = food.votes.where(user_id: current_user.id).first %>
<%= button_to "Unlike This Food", user_vote_path(current_user, vote), :remote => true, method: "delete", :class => "unlike-button" %>
<% end %>
class VotesController < ApplicationController
def index
@votes = Food.find(params[:food_id]).votes
end
def new
@vote = Vote.new
end
def create
@user = current_user
@vote = Vote.new(vote_params)
if @vote.save!
@food = @vote.food
respond_to do |format|
format.html {redirect_to :back, notice: "Liked!"}
format.js :locals => {:food => @food, :user_id => current_user.id}
end
puts @vote.food.id
else
puts "No"
redirect_back(fallback_location: root_path)
end
end
def show
@user = current_user
@vote = Vote.find(params[:id])
end
def destroy
@vote = Vote.find(params[:id])
if @vote.destroy!
respond_to do |format|
format.html {redirect_to :back, notice: "Unliked!"}
format.js
end
else
puts "NOOOOOO"
end
end
private
def vote_params
params.require(:vote).permit(:food_id, :user_id)
end
end
create.js.erb
console.log('see... this is a regular javascript call.');
<%= escape_javascript render(:partial => 'shared/vote_form', :locals => { :food => food, :user_id => current_user.id }) %>
Rails.application.routes.draw do
get 'home', :to => 'static_pages#home'
get 'browse', :to => 'static_pages#browse'
get 'static_pages/about'
get 'static_pages/contact'
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
devise_scope :user do
delete 'sign_out', :to => 'devise/sessions#destroy'
root 'devise/sessions#new'
end
resources :users do
resources :votes
end
resources :foods do
member do
put "result", to: "foods#result"
end
end
get 'soups', :to => 'foods#soup'
get 'salads', :to => 'foods#salad'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end