【问题标题】:Rails params.require not showing upRails params.require 不显示
【发布时间】:2017-04-01 12:53:56
【问题描述】:

我一直在尝试在不使用 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

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    尝试在 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
    

    【讨论】:

      【解决方案2】:

      它们没有在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 %>
      

      【讨论】:

      • 不,您没有使用表单对象f,因此它们没有出现在params[:votes] 中。
      • 抱歉,我发布了我正在处理的错误副本。我也尝试使用块表示法,但仍然收到相同的错误
      • @HarryB。提交表单后,您可以发布params 的值吗?
      • 我正在尝试在我的用户/显示页面上显示按钮/表单,此时我什至无法加载用户/显示页面,因为它现在只是说:第一个参数in 形式不能包含 nil 或为空
      • @HarryB。我猜@usernil,实际上我看不到你在哪里设置实例变量@user。您没有在任何地方设置@user 中的VoteController
      猜你喜欢
      • 2016-04-20
      • 2014-08-06
      • 2017-08-20
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2018-08-27
      • 1970-01-01
      相关资源
      最近更新 更多