Ruby on rails Rails 5-表单中的嵌套属性,参数缺失或为空
我正在尝试从两个不同的模型创建两个关联对象,但仍然得到相同的错误“参数丢失或值为空:repost”我试图找出它失败的原因,但没有成功。有什么提示吗 我有模型转售Ruby on rails Rails 5-表单中的嵌套属性,参数缺失或为空,ruby-on-rails,nested-forms,nested-attributes,Ruby On Rails,Nested Forms,Nested Attributes,我正在尝试从两个不同的模型创建两个关联对象,但仍然得到相同的错误“参数丢失或值为空:repost”我试图找出它失败的原因,但没有成功。有什么提示吗 我有模型转售 class Repost < ApplicationRecord has_many :recurrences, inverse_of: :repost accepts_nested_attributes_for :recurrences def self.create_repost (twit_id) twit =
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
谢谢大家! 您是否在控制器的“新建”操作中调用该视图?是否可以发布发布到控制器的参数?它们将在运行rails服务器的终端。@RichardAE我刚刚用我的日志更新了我的帖子@KcUS_unico我正在调用“创建”操作您应该使用f。隐藏字段标记:twit_id,当前您缺少f
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>
def repost_params
params.require(:repost).permit(:twit_id, recurrences_attributes: [:recurring])
end