我试图保存表单,而我却迷失了为什么它拒绝保存。 anybod有什么可能出错的线索吗?
这是表单代码:
<%= form_for @service, url: services_path do |f| %>
<% @profiles.each do |profile| %>
<%= f.text_field :service_id, value: "#{profile.service_id}" %>
<div class="media">
<a class="media-left" href="#">
<%= image_tag profile.avatar, height: '45', width: '45', class: 'img-circle' %>
</a>
<div class="media-body">
<h4 class="media-heading"><%= profile.service_username %></h4>
<%= profile.service %>
</div>
</div>
<% end %>
<%= f.submit %>
<% end %>
和
@service = current_user.services.new
他们之间的联系是:
user has_many :services
service belongs_to :user
并且服务控制器创建如下所示:
def create
@service = current_user.services.new(service_params)
if @service.save
flash[:notice] = "success"
redirect_to root_url
else
flash[:alert] = "Unable to add"
redirect_to :back
end
end
我的日志说:
Started POST "/services" for 127.0.0.1 at 2015-01-17 18:09:44 -0800
Processing by ServicesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lsW5aVuVUCQrHsaCo+uxbR11sF3mph3lTnM8O/Dtxn8=", "service"=> {"service_id"=>"2967861508"}, "commit"=>"Create Service"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 30 ORDER BY "users"."id" ASC LIMIT 1
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Redirected to http://localhost:3000/schedules
Completed 302 Found in 9ms (ActiveRecord: 1.1ms)
答案 0 :(得分:0)
糟糕,下面的小编辑(@ service.errors而不是@ validation.errors):
从日志中,您可以看到您进入控制器创建方法并且保存失败。这通常是验证问题,但我无法告知您发布的内容。我会将@ service.errors放入flash而不仅仅是&#34;无法添加&#34;。这可以帮助您和将来的用户了解创建失败时的情况。