【问题标题】:Rails 4.0 Record not savingRails 4.0记录不保存
【发布时间】: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


【解决方案1】:

嗯,你需要调试它。我将首先将 gem pry 添加到您的项目中。 然后您应该暂时将您的代码更改为:

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
      binding.pry
      redirect_to @opportunity, alert: 'Unable to add link'
    end
end

为了测试它,您照常执行添加链接操作。代码处理将在调用binding.pry 的行停止。我在这里假设记录的保存失败,我们输入else 部分条件。

如果我假设正确,link 变量将被errors 丰富。在binding.pry 的控制台提示符下尝试显示它们,如果有的话(@link.errors

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多