2016-11-17 204 views
1

我一直在试图实现一个简单的像一个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 

回答

0

因为您没有使用表单对象f,所以不会在params[:vote]中发送它们。如果您不使用表单对象,那么它们只是直接发送(尝试检查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 %> 
+0

不,你没有使用表单对象'F',因此为什么他们不露面'PARAMS [:票]'。 – Rashmirathi

+0

对不起,我发布了我正在处理的内容的错误副本。我也尝试过使用块表示法,但仍然收到相同的错误 –

+0

@HarryB。你可以在提交表单后发布'params'的值吗? – Rashmirathi

0

尝试设置用户在VotesController。此外,请尝试分享控制台中显示的错误,以查看具体错误。

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