【发布时间】:2014-06-03 03:38:15
【问题描述】:
我有一个包含许多链接的机会模型。 Links 是 Opportunity 的嵌套资源。在我的views/opportunities/show.html.erb 页面上,我显示了属于该机会的所有链接,并且还呈现了一个“新链接”表单。这种形式直到最近都很好用,我不知道为什么。当我填写“新链接”表格并单击“添加”时,记录不保存。有人可以帮我吗?
这是我的观点/机会/show.html.erb 页面:
<%= render @opportunity %>
<form>
<fieldset>
<legend>Links</legend>
<div id="links">
<%= render @opportunity.links %>
</div>
<%= render :file => 'links/new' %>
</fieldset>
</form>
这是我的观点/链接/新页面:
<%= form_for ([@opportunity, @opportunity.links.new]) do |f| %>
<div class="field">
<%= f.label "Description:" %> <br />
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label "URL:" %> <br />
<%= f.text_field :link_url %>
</div>
<div class="actions">
<%= f.submit 'Add' %>
</div>
<% end %>
这是我的创建链接控制器:
def create
@opportunity = Opportunity.find(params[:opportunity_id])
@link = @opportunity.links.new(link_params)
if @link.save
redirect_to @opportunity, notice: 'link has been added'
else
redirect_to @opportunity, alert: 'Unable to add link'
end
end
这是我的链接模型:
class Link < ActiveRecord::Base
belongs_to :opportunity
end
这是我的机会模型:
class Opportunity < ActiveRecord::Base
has_many :links
end
这是来自我的控制台的代码:
Started GET "/opportunities/7?utf8=%E2%9C%93&authenticity_token=ZLgPz98w2MjTChzzDXJ8EcqNmYNtBUG5DSYcp1CXReU%3D&link%5Bdescription%5D=testlink&link%5Blink_url%5D=testlink&commit=Add" for 127.0.0.1 at 2014-06-02 15:19:06 -0400
Processing by OpportunitiesController#show as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZLgPz98w2MjTChzzDXJ8EcqNmYNtBUG5DSYcp1CXReU=", "link"=>{"description"=>"testlink", "link_url"=>"testlink"}, "commit"=>"Add", "id"=>"7"}
Opportunity Load (0.2ms) SELECT "opportunities".* FROM "opportunities" WHERE "opportunities"."id" = ? LIMIT 1 [["id", 7]]
Rendered opportunities/_opportunity.html.erb (2.0ms)
Link Load (0.1ms) SELECT "links".* FROM "links" WHERE "links"."opportunity_id" = ? [["opportunity_id", 7]]
Rendered links/_link.html.erb (0.2ms)
Rendered links/new.html.erb (2.0ms)
【问题讨论】:
-
你的模型是什么样子的? Rails 在展示机会方面存在问题。你能把你的模型代码和你的数据库迁移好吗>
-
我编辑添加了我的模型。我不久前删除了所有迁移文件@HristoGeorgiev
-
你的
form_for在哪里?看起来您正在对表单提交执行GET请求,而您应该执行POST。 -
@DamienRoche 我编辑了它现在就在那里。以前没有,因为我没有输入足够的标签...请指教。
标签: ruby-on-rails nested