Rails 5 - 表单中的嵌套属性,params缺失或为空

时间:2017-02-21 15:15:30

标签: ruby-on-rails nested-forms nested-attributes

我正在尝试从两个不同的模型创建两个关联对象,但我仍然遇到相同的错误" param丢失或值为空:repost "我试图弄清楚为什么它会失败几天但是徒劳无功。任何提示?

我有模型重新发布

class Repost < ApplicationRecord

 has_many :recurrences, inverse_of: :repost
 accepts_nested_attributes_for :recurrences

 def self.create_repost (twit_id)
  twit = Twit.find_by_id(twit_id)
  Repost.find_or_create_by(link: twit.link) do |repost|
   repost.twit_id = twit.id
   repost.content = twit.content
   repost.image_url = twit.image_url
   repost.content_url = twit.content_url
   repost.click = 0
   repost.like = 0
   repost.retweet = 0
   repost.engagement = 0
   repost.number_of_publications = repost.publications.count
   repost.likes_sum = 0
   repost.retweets_sum = 0
   repost.engagement_sum = 0
   repost.recurrence = repost.recurrences.recurring
 end

end

和模型重复

class Recurrence < ApplicationRecord

 serialize :recurring, Hash
 belongs_to :repost, inverse_of: :recurrences

 def self.set(repost_id, frequency)
  repost = Repost.find_by_id(repost_id)
  Recurrence.create do |recurrence|
   recurrence.repost_id = repost.id
   recurrence.recurring = frequency
   recurrence.start_time = recurrence.created_at
   recurrence.end_time = nil
  end
 end

 def recurring=(value)
  if RecurringSelect.is_valid_rule?(value)
   super(RecurringSelect.dirty_hash_to_rule(value).to_hash)
  else
   super(nil)
  end
 end

 def rule
  rule = IceCube::Rule.from_hash(self.recurring)
  rule
 end

 def schedule
  schedule = IceCube::Schedule.new(self.created_at)
  schedule.add_recurrence_rule(rule)
  schedule
 end

end

这是我的转发控制器

class RepostsController < ApplicationController
  before_action :find_repost, only: [:show, :edit, :update, :destroy, :get_likes]

  def index
    @repost = Repost.new
    if params[:filter_by]
      @reposts = Repost.filter(params[:filter_by], params[:min], params[:max], params[:start_at], params[:end_at])
    else
      @reposts = Repost.all.order("created_at DESC")
    end
  end

  def show
  end

  def new
    @repost = Repost.new
  end

  def create
    @repost = Repost.create_repost(repost_params)
    redirect_to reposts_path
  end

  def destroy
    @repost.destroy
    redirect_to reposts_path
  end


  private

  def find_repost
    @repost = Repost.find(params[:id])
  end

  def repost_params
    params.require(:repost).permit(:twit_id, recurrences_attributes: [:recurring])
  end

end

这是我的视图

<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-10 col-sm-offset-1">
      <h1 class="twit-h1">My best Tweets</h1>
      <p><%= render "filter_form" %></p>
      <p><% @twits.each do |twit| %></p>
        <p><%= form_for @repost do |f| %></p>
        <%= hidden_field_tag :twit_id , twit.id %>
        <div class="twit">
          <%= image_tag "#{twit.image_url}", class: "twit-image" %>
          <div class="twit-body">
            <% if twit.content.present? %>
              <h3><%= twit.content %></h3>
            <% else %>
              <%= "This tweet has no text" %>
            <% end %>
            <% if twit.content_url.present? %>
              <%= link_to "#{twit.content_url}".truncate(40), twit.content_url, class: "twit-link" %>
            <% else %>
              <%= "This tweet has no link" %>
            <% end %>
            <%= f.fields_for :recurrences do |r| %>
            <h6 style="float: right;"><%= r.select_recurring :recurring %></h6>
            <% end %>
            <h6>
              <i class="fa fa-bullhorn fa" aria-hidden="true"></i> <%= twit.engagement %>
              <i class="fa fa-retweet fa" aria-hidden="true"></i> <%= twit.retweet %>
              <i class="fa fa-heart fa" aria-hidden="true"></i> <%= twit.like %>
            </h6>
            <p>Tweeted on <%= (twit.first_date).strftime("%A, %B %d, %Y at %I:%M%p %Z") %></p>
            <%= link_to "Delete", twit_path(twit), name: nil, class: "btn btn-danger btn-sm", method: :delete, data: {:confirm => 'Are you sure?'} %>
            <%= f.submit "Add to your Reposts library", class: "btn btn-primary btn-sm" %>
            <% end %>
          </div>
        </div>
      <% end %>
    </div>
  </div>
</div>

更新,这是我的日志

开始POST&#34; / __ better_errors / 923b59f8da1318ef / variables&#34; for :: 1 at 2017-02-21 15:52:56 +0100 开始POST&#34; / reposts&#34; for :: 1 at 2017-02-21 15:54:14 +0100 由RepostsController处理#create as HTML   参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; authenticity_token&#34; =&gt;&#34; 62pE3le5m99HF10RgQcd70wFHqWQWiRmQQO2ndNHT3gnceRu94Y1ttyBuSExBjr4 / cxeVUtgY60GRThxdpFGGg ==&#34;,& #34; twit_id&#34; =&gt;&#34; 1844&#34;,&#34;重复&#34; =&gt;&#34; {\&#34; interval \&#34;:1,\ &#34;直到\&#34;:空,\&#34;计数\&#34;:空,\&#34;验证\&#34;:{\&#34; \天&#34 ;:[1],\&#34; HOUR_OF_DAY \&#34;:1,\&#34; MINUTE_OF_HOUR \&#34;:0},\&#34; rule_type \&#34;:\&# 34; IceCube :: WeeklyRule \&#34;,\&#34; week_start \&#34;:0}&#34;,&#34; commit&#34; =&gt;&#34;添加到您的转发帖子文库&#34;} 在2ms完成400 Bad Request(ActiveRecord:0.0ms)

ActionController :: ParameterMissing - 缺少param或值为空:repost:

似乎我的错误来自我的Repost控制器中的这个私有方法,但我不明白为什么?

def repost_params
 params.require(:repost).permit(:twit_id, recurrences_attributes: [:recurring])
end

谢谢!

0 个答案:

没有答案