Rails params.require没有出现

时间:2016-11-17 20:06:42

标签: ruby-on-rails ruby

我一直试图在不使用ajax的情况下为rails项目实现一个简单的按钮。我已经尝试了所有我能想到的东西,但它却不断地说出错误:

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

2 个答案:

答案 0 :(得分:0)

由于您没有使用表单对象params[:vote],因此未在f中发送它们。如果您不使用表单对象,则只需直接发送它们(尝试检查params[:food_id]params[:user_id]的值)。

试试这个:

<%= form_for [@user, vote] do |f| %>
    <%= f.hidden_field 'food_id', food.id %>
    <%= f.hidden_field 'user_id', current_user.id %>
    <%= f.submit %>
<% end %>

答案 1 :(得分:0)

尝试在VotesController中设置User。此外,请尝试分享控制台中显示的错误,以查看特定错误。

<强> HTML

<%= render partial: 'shared/vote_form', locals: {:user => @user, :food => food, :vote => vote}%>
  

提示:   如果@user应该是current_user,那么使用current_user对象   而是@user!

<强>形式:

<%= form_for [user, vote] do |f| %>
    <%= f.hidden_field_tag 'food_id', food.id %>
    #no need to pass current_user, we are gonna use in controller
    <%= f.submit %>
<% end %>

<强>控制器:

class VotesController < ApplicationController
    before_action :set_user

def create
    @vote = Vote.new(vote_params)
    @vote.user_id = current_user.id

    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

private
    def set_user
      @user = User.find(params[:user_id])
    end
end