Ruby:缺少参数或值为空

时间:2018-07-14 18:53:05

标签: ruby-on-rails ruby

我想做的是将一个“用户”与一个“活动”相关联,一旦他们单击“加入”按钮,它得到的错误就是var armoryNews = { "armorynews": { "250852079": { "timestamp": 1531570578, "news": "<a href = \"http://www.torn.com/profiles.php?XID=2143655\">3dubbeltjes<\/a> used one of the faction's Xanax items." }, "250848543": { "timestamp": 1531567903, "news": "<a href = \"http://www.torn.com/profiles.php?XID=2072880\">Akine<\/a> used one of the faction's Xanax items." }, "250847811": { "timestamp": 1531567248, "news": "<a href = \"http://www.torn.com/profiles.php?XID=2147631\">Silnt<\/a> used one of the faction's Bottle of Beer items." } } }; var armorynewsKeys = Object.keys(armoryNews["armorynews"]); armorynewsKeys.map(item => { console.log(armoryNews["armorynews"][item]); });
通过其进行通信的类称为“ act_joins”,用于 act_joins controller 如下:

param is missing or the value is empty: act_joins

表格是

class ActJoinsController < ApplicationController def create @act_join = current_user.act_joins.new(act_join_params) respond_to do |format| if @act_join.save! format.html { redirect_to user_dashboard_path, notice: 'You successfully joined the event.' } format.json { render :show, status: :created, location: @act_join } else format.html { render :new } format.json { render json: @act_join.errors, status: :unprocessable_entity } end end end private def act_join_params params.require(:act_joins).permit(:activity_id) end end

如果我在Rails控制台中并输入:<%= form_for :act_joins, url: act_joins_path do |f| %> <%= f.submit 'Join', class: 'btn btn-success' %> <% end %> ,则可以使用。

我在寻找答案时看到了这样的错误,但是所有其他错误都有参数通过,但是只有我的ID才是真正的。

2 个答案:

答案 0 :(得分:0)

应为act_join,而不是act_joins,因此您的声明应为:

params.require(:act_join).permit(:activity_id)

params.require接受一个对象的键名,该键名应为单数。

更新

看起来您在form_for复数形式中也有密钥。也应该是act_join

<%= form_for :act_join, url: act_joins_path do |f| %>

答案 1 :(得分:0)

您实际上并没有发布任何数据。 尝试将隐藏字段添加到表单中,例如:

<%= f.hidden_field(:activity_id, value: however_you_get_your_activity_id) %> 

显然将however_you_get_your_activity_id替换为有效ID。

例如,如果我们有Activity@activities对象的集合,则可以执行以下操作:

<% @activities.each do |activity| %> 
  <%= form_for :act_joins, url: act_joins_path do |f| %>
    <%= f.hidden_field(:activity_id, value: activity.id %>
    <%= f.submit 'Join', class: 'btn btn-success' %>
  <% end %>
<% end %>