-1

この機能は以前は機能していましたが、突然停止し、理由がわかりません。テストを実行する前に変更を加えましたが、手順をたどったところ、何が変更されたのかわかりません。なぜこれが投稿を保存しているのに場所を保存していないのか、新鮮な目で教えてもらえますか?

モデル 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"}

場所情報ではなく、投稿が保存されるのはなぜですか?

4

2 に答える 2

0

がありません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)) %>
于 2012-11-07T05:05:14.923 に答える
0

解決しました。これは、モデル内の迷子のコードでした。

ご助力いただきありがとうございます!

于 2012-11-08T18:38:10.107 に答える