【问题标题】:ROR3: Why is this not saving?ROR3:为什么这不保存?
【发布时间】:2012-11-06 17:59:32
【问题描述】:

这个功能曾经可以工作,但突然停止了,我不知道为什么。在运行测试之前我已经进行了更改,但是在追溯我的步骤后,我不知道发生了什么变化。一双新鲜的眼睛能告诉我为什么这是保存帖子而不是位置吗?

Model Post.rb

class Post < ActiveRecord::Base
 attr_accessible :title, :body, :tag_list, :locations_attributes
 has_and_belongs_to_many :locations
 accepts_nested_attributes_for :locations, :reject_if => :all_blank
end

*控制器 Posts_controller.rb *

class PostsController < ::Blogit::ApplicationController

...     

def new
  @post = current_blogger.blog_posts.new(params[:post])
  @location = @post.locations.build
end

def update
  @post = current_blogger.blog_posts.find(params[:id])
  if @post.update_attributes(params[:post])
    redirect_to @post, notice: 'Blog post was successfully updated.'
  else
    render action: "edit"
  end
end

def create
  @post = current_blogger.blog_posts.new(params[:post])
  if @post.save
    redirect_to @post, notice: 'Blog post was successfully created.'
  else
    render action: "new"
  end
end

end

*查看_form.html.erb*

<%= nested_form_for(@post, :html=> {:multipart => true, :class=> "new_blog_post", :id=> "new_blog_post"}) do |f| %>

 ...

<%= field do %>
  <%= f.text_field :title, placeholder: "Give your post a title", :class=>"span12" %>
<% end %>
<%= field do %>
  <%= f.text_area :body, placeholder: "Write something here...", :id=>"blog-text", :class=>"span12" %>
 <% end %>

<%= field id: "new_blog_post_tag_field" do %>
  <%= f.label :tag_list, "Tags" %>
  <%= f.text_field :tag_list, placeholder: "tag one, tag two, etc..." %>
<% end %>

<%= f.label :search_locations, "Add locations to your post" %>
   <%= render :partial => 'search_locations', :locals => { :f => f }  %>

 <p><%= f.link_to_add "Add a location", :locations %></p>

<%= actions do %>
<%= f.submit "Submit", :class=>"btn", :disable_with => 'Uploading Image...' %> or 
<%= link_to("cancel", @post.new_record? ? root_path : post_path(@post)) %>

<% end %>

<% end %>

*表格部分 _search_locations.html.erb*

 <%= f.fields_for :locations do |m| %>


 <%= m.text_field :name,:class=>"localename", :id=>"appendedInput", :placeholder=> "Name of the location", :autocomplete => "off" %>


 <span class="add-on"><%= m.link_to_remove do %><i class='icon-trash'></i></span><%end%>


 <% end %>

日志中的实际提交是这样的:

Started PUT "/blog/posts/51-hong-kong" for 127.0.0.1 at 2012-11-06 17:41:00 +0000
Processing by Blogit::PostsController#update as HTML
Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"s+2117Qi/HzO/D/ORINhqHChMJR3S5XK7y/s3rq3dnc=", "post"=>{"title"=>"Hong 
Kong ", "body"=>"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In orci elit,  
rhoncus quis interdum vel, interdum non nulla. Phasellus nec massa lorem. Duis malesuada   
pellentesque orci, accumsan mollis augue aliquam mollis. Pellentesque luctus orci in enim 
hendrerit adipiscing. Mauris tempor tortor in leo posuere tristique.", "tag_list"=>"", 
"locations_attributes"=>{"0"=>{"_destroy"=>"false",    
"name"=>"London","longitude"=>"-0.1276831", "latitude"=>"51.5073346"}}},
"_wysihtml5_mode"=>"1", "name"=>"London", "legname"=>"London", "longitude"=>"-0.1276831",
"latitude"=>"51.5073346", "commit"=>"Submit", "id"=>"51-hong-kong"}

为什么保存的是帖子而不是位置信息?

【问题讨论】:

  • 你应该从你的控制器发布更新方法而不是创建方法,因为你已经为更新方法提供了日志。
  • 您好 Prem,感谢您的评论。我已经添加了更新方法。我认为这不是问题,但是无论我是更新还是创建新位置,位置都不会保存在数据库中。还有其他建议吗?

标签: ruby-on-rails ruby-on-rails-3 forms nested-forms save


【解决方案1】:

您没有fields_for,请尝试替换:

<%= render :partial => 'search_locations', :locals => { :f => f }  %>

<p><%= f.link_to_add "Add a location", :locations %></p>

<%= actions do %>
<%= f.submit "Submit", :class=>"btn", :disable_with => 'Uploading Image...' %> or 
<%= link_to("cancel", @post.new_record? ? root_path : post_path(@post)) %>

与:

<% f.fields_for :locations do |location| %>
  <%= render :partial => 'search_locations', :locals => { :f => location }  %>
<% end %>

<%= f.submit "Submit", :class=>"btn", :disable_with => 'Uploading Image...' %> or 
<%= link_to("cancel", @post.new_record? ? root_path : post_path(@post)) %>

【讨论】:

  • 嗨,Kien,我在部分中有一个 fields_for。我按照你的方法试了一下,报错了。还有其他建议吗?
  • 你遇到了什么错误?这是什么nested_form_for?只是form_for
【解决方案2】:

解决了,这是模型中的一小段代码。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2014-11-09
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多