我的问题是我正在检查form_for的验证,其中包含一个嵌套的fields_for,以访问用户输入基本信息的Address表。如果任何信息不正确,则无法加载回页面。我收到的错误是......
托运人中的NoMethodError #create
undefined method 'addcheck' for #<Address:0x9fb9664>
我觉得这是因为复选框不是地址表中的变量,就像fields_for中的其他a.variables一样。它用于简单地将地址添加到Contacts表中(如果已选中)。 我将添加 - - - 以指示错误的位置
发生错误的Shippers.new.html的视图是
<%= fields_for :address do |a| %>
<div class="row">
<div class="form-group col-xs-12 col-sm-12 col-md-9 col-lg-9">
<label>Street Address <p class="help-block" style= "display:inline;" > You can drag the marker to property position</p></label>
<div>
<span id="latitude" class="label label-default"></span> <span id="longitude" class="label label-default"></span>
</div>
<%= a.text_field :street_address, :class=> "form-control", :id=> "address", :placeholder=> "Enter Starting Location", :autocomplete=> "on" %>
</div>
<div class="col-xs-12 col-sm-12 col-md-3 col-lg-3">
<div class="form-group">
<label>Add Contact?</label>
-----------------------------------------------------
<div class="checkbox custom-checkbox">
<label><%= a.check_box :addcheck %>
<span class="fa fa-check"></span></label>
</div>
------------------------------------------------------
</div>
</div>
我正在验证模型中的所有信息......
class CurrentPackage < ActiveRecord::Base
self.primary_key = :shipment_id
belongs_to :address, foreign_key: "address_start"
validates :title, :presence => true
validates :total_cost, :presence => true
validates :weight, :presence => true
validates :dimensions, :presence => true
validates :user_id, :presence => true
我的托运人控制器中的创建操作是......
def create
@address = Address.where({street_address: params[:address][:street_address], city: params[:address][:city], state: params[:address][:state], zip_code: params[:address][:zip_code]}).first
@shippers = CurrentPackage.new( user_params )
@shippers.user_id = current_user
@shippers.time_created = Time.now
@address2 = Address.where({street_address: params[:address][:street_address2], city: params[:address][:city2], state: params[:address][:state2], zip_code: params[:address][:zip_code2]}).first
if (!!@address) == false
@address = Address.new(street_address: params[:address][:street_address], city: params[:address][:city], state: params[:address][:state], zip_code: params[:address][:zip_code])
@address.save!
end
if (!!@address2) == false
@address2 = Address.new(street_address: params[:address][:street_address2], city: params[:address][:city2], state: params[:address][:state2], zip_code: params[:address][:zip_code2])
@address2.save!
end
@temp = @address.address_id
@shippers.address_start = @temp
@temp2 = @address2.address_id
@shippers.address_end = @temp2
#@shippers.save! #replacing
------------------------------------------------------
if @shippers.save
#redirect_to @shippers
redirect_to shippers_index_path
else
render shippers_new_path #addcheck fails
#render @shippers
#redirect_to shippers_new_path #works but loses data
#render 'new' #addcheck fails
end
--------------------------------------------------
if (params[:address][:addcheck] == "1")
if (!!Contact.where({user_id: current_user, contact: @temp}).first) == false
hmm = Contact.new(user_id: current_user,contact: @temp)
hmm.save!
end
end
if (params[:address][:addcheck2] == "1")
if (!!Contact.where({user_id: current_user, contact: @temp2}).first) == false
hmm2 = Contact.new(user_id: current_user, contact: @temp2)
hmm2.save!
end
end
end
我的 routes.rb 文件看起来像
get "shippers/isdriver"
get "shippers/index"
get "shippers/show"
get "shippers/update"
post "shippers/create"
get "shippers/new"
get "shippers/destroy"
devise_for :users
get "home/index"
resources :addcheck
我添加了资源:addcheck因为我看到另一个stackoverflow帖子说它适用于他们,但不适合我。
重申一下,只有在表格不正确的情况下才会发生这种情况,进入if @ shippers.save的else条件,然后再次尝试加载新页面并中断。
任何帮助都会非常感激:D
*****附加信息******* 我拿出了addcheck rails line,然后再次运行,但出于同样的原因打破了street_address2的下一个字段,因为它实际上不是任何模型中的变量,只是占位符。
答案 0 :(得分:1)
我认为您应该将支票与地址模型<%= a.check_box :addcheck %>
相关联,而不是将支票与地址模型<%= check_box_tag(:addcheck) %>
联系起来namespace Ws.Models
{
public class samplemodelDTO
{
[Key]
public int id { set; get; }
public string Productname { get; set; }
public int Productquantity { get; set; }
public string ProductReview { get; set; }
}
}
答案 1 :(得分:0)
在将属性传递给模型之前删除该属性
m <- matrix(c(1,1,1,0,2,2,13,0,3,3,3,0,4,4,4,1),4,byrow=T);
m;
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 0
## [2,] 2 2 13 0
## [3,] 3 3 3 0
## [4,] 4 4 4 1
m[apply(m[,-ncol(m)]==m[,1],1,all)&m[,ncol(m)]==0,];
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 0
## [2,] 3 3 3 0