我一直在试图实现一个简单的像一个rails项目的按钮,而不使用ajax。我用尽了一切我能想到的,但它一直来下来得到一个错误说:Rails params.require not showing up
param is missing or the value is empty: vote
我得到,这意味着没有表决权参数被我的要求发送,但在这一点上我不知道还有什么可以尝试才能使其发挥作用。
class VotesController < ApplicationController
def index
@votes = Vote.all
end
def new
@vote = Vote.new
end
def create
@vote = Vote.new(vote_params)
if @vote.save
puts @vote
flash[:notice] = "Thanks for voting!"
redirect_back(fallback_location: root_path)
else
puts "No"
flash[:notice] = "Something went wrong"
redirect_back(fallback_location: root_path)
end
end
def show
@vote = Vote.find(params[:id])
end
def destroy
@vote = Vote.find(params[:id])
if @vote.destroy!
flash[:notice] = "Unvoted!"
redirect_to user_path(current_user)
end
end
private
def vote_params
params.require(:vote).permit(:food_id, :user_id)
end
end
<%= form_for [@user, @vote] do |f| %>
<%= hidden_field_tag 'food_id', food.id %>
<%= hidden_field_tag 'user_id', current_user.id %>
<%= f.submit %>
<% end %>
用户/ show.html.erb
<h2>Hey <%= @user.firstname %>!</h2>
<p>Check out these dank Soups and Salads you've served up or <%= link_to "Upload some new Soup or Salad", new_food_path %></p>
<strong>Image:</strong>
<% @user.foods.each do |food| %>
<% vote = current_user.votes.where(food_id: food.id).first %>
<h3><%= link_to food.title.capitalize, food_path(food.id) %></h3>
<p><%= link_to (image_tag (food.image.url)), food_path(food.id) %></p>
<%= render 'shared/vote_form', :food => food, :vote => vote %>
<%= link_to "Delete", food, method: :delete, data: {confirm: "Really delete this article?"} %>
<% end %>
class Vote < ApplicationRecord
belongs_to :user
belongs_to :food
end
resources :users do
resources :votes
end
resources :foods
不,你没有使用表单对象'F',因此为什么他们不露面'PARAMS [:票]'。 – Rashmirathi
对不起,我发布了我正在处理的内容的错误副本。我也尝试过使用块表示法,但仍然收到相同的错误 –
@HarryB。你可以在提交表单后发布'params'的值吗? – Rashmirathi