编辑:添加了更新操作,以及错误发生在哪一行
模型:
class Match < ActiveRecord::Base
has_and_belongs_to_many :teams
has_many :match_teams
has_many :teams, :through => :match_teams
accepts_nested_attributes_for :match_teams, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)
控制器:
def new
@match = Match.new
@match_teams = 2.times do
@match.match_teams.build
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: @match }
end
end
def update
@match = Match.find(params[:id])
respond_to do |format|
if @match.update_attributes(params[:match])
format.html { redirect_to @match, notice: 'Match was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" } …Run Code Online (Sandbox Code Playgroud) 我有以下型号:user,role,user_role(user_role是一个连接模型)
我正在尝试使用用户#edit页面上的复选框编辑用户的角色.这是我的尝试,我觉得我错过了一些重要的事情,或采取了错误的方法.
has_many :user_roles, dependent: :destroy
has_many :roles, through: :user_roles
attr_accessible :user_roles_attributes
accepts_nested_attributes_for :user_roles, reject_if: lambda { |a| a[:role_id] == 0 }, allow_destroy: true
def has_role?(role_sym)
roles.any? { |r| r.name.underscore.to_sym == role_sym.downcase }
end
def setup_roles!
Role.all.each { |role|
user_roles.build(user_id: id, role_id: role.id) unless has_role?(role.name.to_sym)
}
end
Run Code Online (Sandbox Code Playgroud)
belongs_to :user
belongs_to :role
delegate :name, to: :role
Run Code Online (Sandbox Code Playgroud)
has_many :user_roles
has_many :users, through: :user_role
Run Code Online (Sandbox Code Playgroud)
def edit
@user = User.find(params[:id])
@user.setup_roles!
end …Run Code Online (Sandbox Code Playgroud) 我有类似的东西:
class Profile < ActiveRecord::Base
belongs_to :user
delegate :full_name, :to => :user
accepts_nested_attributes_for :user
.......
Run Code Online (Sandbox Code Playgroud)
这工作正常,因为我希望配置文件能够在用户中设置first_name和last_name.但是,如果用户在表单中注入其他参数,这会带来安全威胁.
如何使accepts_nested_attributes_for只接受first_name和last_name并删除其他参数?
我明白了
ActiveRecord :: RecordNotFound:对于ID =的订单,找不到ID = 3的客户端
在尝试为现有客户提交订单时.这通过键入以下内容通过表单或控制台进行:
Order.new(:client_attributes => { :id => 3 })
Run Code Online (Sandbox Code Playgroud)
payment_form.html.erb:
<%= semantic_form_for @order, :url => checkout_purchase_url(:secure => true) do |f| %>
<%= f.inputs "Personal Information" do %>
<%= f.semantic_fields_for :client do |ff| %>
<%= ff.input :first_name %>
<%= ff.input :last_name %>
<!-- looks like semantic_fields_for auto-inserts a hidden field for client ID -->
<% end %>
<% end %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
Order.rb:
class Order < ActiveRecord::Base
belongs_to :client
accepts_nested_attributes_for :client, :reject_if …Run Code Online (Sandbox Code Playgroud) 鉴于以下内容:
class Location < ActiveRecord::Base
has_many :games
end
class Game < ActiveRecord::Base
validates_presence_of :sport_type
has_one :location
accepts_nested_attributes_for :location
end
Run Code Online (Sandbox Code Playgroud)
def new
@game = Game.new
end
Run Code Online (Sandbox Code Playgroud)
<%= simple_form_for @game do |f| %>
<%= f.input :sport_type %>
<%= f.input :description %>
<%= f.simple_fields_for :location do |location_form| %>
<%= location_form.input :city %>
<% end %>
<%= f.button :submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
为什么位置字段(城市)没有出现在表单中?我没有收到任何错误.我错过了什么?
我尝试在rails4中创建一对多连接.但是,虽然我没有收到错误,但不存储嵌套属性.
我究竟做错了什么?
站的模型
class Station < ActiveRecord::Base
has_many :adresses
accepts_nested_attributes_for :adresses
end
Run Code Online (Sandbox Code Playgroud)
ADRESS-型号
class Adress < ActiveRecord::Base
belongs_to :station
end
Run Code Online (Sandbox Code Playgroud)
Station-Controller 类StationsController <ApplicationController
def new
@station = Station.new
@station.adresses.build
end
def create
@station = Station.new(station_params)
@station.save
redirect_to @station
end
def index
@stations = Station.all
end
private
def station_params
params.require(:station).permit(:name, adresses_attributes: [ :url ])
end
end
Run Code Online (Sandbox Code Playgroud)
站:new.html.erb
<%= form_for :station, url: stations_path do |station| %>
<p>
<%= station.label :name %><br />
<%= station.text_field :name %>
</p>
<%= station.fields_for :adresses do |adress| …Run Code Online (Sandbox Code Playgroud) ruby-on-rails one-to-many nested-forms nested-attributes ruby-on-rails-4
在使用Rails 4强参数时,我正在努力寻找处理嵌套表单的正确和干燥方式.
我有一些模型,我在我的应用程序中以不同的方式重新使用,因此我有一些嵌套的表单.
问题是当我使用嵌套表单时,我不觉得我正在做DRYest方法.
考虑以下是AccountsController:
params.require(:account).permit(
person_attributes: [
:name,
address: [
:city
]
]
)
Run Code Online (Sandbox Code Playgroud)
从我的角度来看person_attributes,不应该进入AccountsController.我的意思是,AccountsController不应该对Person类有太多了解.
我想添加这样的允许属性,PeopleController但它是一个不能直接访问的私有方法.
现在我正在考虑将类允许的属性存储在类方法中,Person.permitted_attributes但我不确定这是否是一个聪明的设计决策.这样做我可以在几个地方使用它.我显然会为每个控制器编写单独的测试.
我理解参数过滤应该在控制器中完成,而不是在模型中完成,但我不确定添加代码重复是否是一个很好的权衡.
你们怎么看待这件事?
这是我的3个型号.
User
has_many :memberships
has_many :teams, through: :memberships, dependent: :destroy
accepts_nested_attributes_for :memberships
Team
has_many :memberships
has_many :users, through: :memberships, dependent: :destroy
accepts_nested_attributes_for :memberships
Membership
belongs_to :team
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
以下是我的Team控制器的一些部分.我的目标是向某个团队添加/更新成员.请注意,添加成员的源已作为一组用户存在.
TeamsController
def create
@team = Team.new(team_params)
@team.users << User.find(member_ids) #add leader and members to team
if @team.save
#redirect to created team
else
#show errors
end
end
def update
#TO DO: update team roster here
if @team.update(team_params)
#redirect to updated team
else
#show errors
end
end
Run Code Online (Sandbox Code Playgroud)
Team控制器的强参数
#parameters for team details
def …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails has-many-through nested-attributes strong-parameters
我的模特:
class Foo < ActiveRecord::Base
has_many :bars, inverse_of: :foo
accepts_nested_attributes_for :bars
...
end
class Bar < ActiveRecord::Base
belongs_to :foo, inverse_of: :bars
...
end
Run Code Online (Sandbox Code Playgroud)
当我尝试创建这样的记录时:
Foo.create(foo_attribute: value, bars_attirbutes: [{bar_attribute: value}])
Run Code Online (Sandbox Code Playgroud)
我明白了:
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "bars" violates foreign key constraint "bars_foo_id_fkey"
DETAIL: Key (foo_id)=(14) is not present in table "foos".
Run Code Online (Sandbox Code Playgroud)
所以我想ActiveRecord试图在保存父模型之前保存嵌套模型,从而导致错误.但为什么这样做呢?我怎么能阻止它这样做呢?
我尝试使用CarrierWave上传文件,Image模型是多态的.但我得到了标题中的错误,所以这是我的模型:
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
mount_uploader :image, ImageUploader
enum image_type: {icon: 1, title: 2, regular: 3, slider: 4}
end
class Service < ActiveRecord::Base
has_many :images, as: :imageable, :dependent => :destroy
accepts_nested_attributes_for :images
end
Run Code Online (Sandbox Code Playgroud)
和_form.html.erb:
<div class="control-group">
<%= simple_fields_for :images do |image| %>
<div class="controls">
<label class="control-label">Hizmet Büyük Resimi</label>
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-new thumbnail" style="width: 200px; height: 150px;"><img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt=""/></div>
<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
<div>
<span class="btn btn-file"><span class="fileupload-new">Resim …Run Code Online (Sandbox Code Playgroud) ruby-on-rails image-uploading nested-attributes carrierwave ruby-on-rails-4