0

フォームから送信される以下のパラメーターのセットがあります

Parameters: {"utf8"=>"✓", "authenticity_token"=>"pyMkh1eJ7WxYC978XKjdsyGOeGDvi6RTIOSGb9KMqkc=", "link"=>{"category_id"=>"1", "comment"=>"", "url"=>"yahoo.com "}, "type"=>"html", "original_url"=>"http://yahoo.com", "url"=>"http://www.yahoo.com/", "title"=>"Yahoo!", "description"=>"Welcome to Yahoo!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and information.", "favicon_url"=>"http://www.yahoo.com/favicon.ico", "provider_url"=>"http://www.yahoo.com", "provider_display"=>"www.yahoo.com", "provider_name"=>"Yahoo", "safe"=>"true", "html"=>"", "thumbnail_url"=>"", "object_type"=>"link", "image_url"=>"", "category_id"=>"1"}

カテゴリ モデルに属するリンク モデルに新しい「リンク」レコードを作成したいと考えています。リンクコントローラーの「作成」アクションは次のようになります

  def create
    @category = Category.find_by_id(params[:category_id])
    @link = @category.links.build(params[:link])
    @link.user_id = current_user.id
    respond_to do |format|
        if @link.save
            links_attributes = params.slice(:original_url, :title, :description, :favicon_url, :provider_url, :provider_display, :thumbnail_url, :object_type)
            @link.update_attributes(links_attributes)
        else 

        end
    end     
  end

update 属性を指定せずに @link.save を実行すると、コメント、URL、およびカテゴリ ID のみが保存されます。ただし、上記は 2 つのレコードを作成します。1 つはコメント、url、category_id で、もう 1 つはすべてのデータを含みます。

これにより、すべての情報を含む 1 つのレコードのみが作成されるようにするにはどうすればよいですか?

更新 3 つのパラメーターを使用してレコードを作成し、残りのパラメーター (links_attributes) を使用して更新できれば、それで問題ありません...どうすればよいかわかりません。

これは、送信時に取得する出力です。

Started POST "/categories/1/links" for 127.0.0.1 at 2013-01-11 12:43:44 -0500
Processing by LinksController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pyMkh1eJ7WxYC978XKjdsyGOeGDvi6RTIOSGb9KMqkc=", "link"=>{"category_id"=>"1", "comment"=>"", "url"=>"bloomberg.com "}, "type"=>"html", "original_url"=>"http://bloomberg.com", "url"=>"http://www.bloomberg.com/", "title"=>"Business, Financial & Economic News, Stock Quotes", "description"=>"Bloomberg is a premier site for business and financial market news. It delivers world economic news, stock futures, stock quotes, & personal finance advice.", "favicon_url"=>"http://www.bloomberg.com/favicon.ico", "provider_url"=>"http://www.bloomberg.com", "provider_display"=>"www.bloomberg.com", "provider_name"=>"Bloomberg", "safe"=>"true", "html"=>"", "thumbnail_url"=>"http://www.bloomberg.com/image/is2KySnyVWmA.jpg", "object_type"=>"link", "image_url"=>"", "category_id"=>"1"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "links" ("category_id", "comment", "created_at", "description", "favicon_url", "object_type", "original_url", "points", "profile_link", "provider_display", "provider_url", "thumbnail", "thumbnail_url", "title", "updated_at", "url", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["category_id", "1"], ["comment", ""], ["created_at", Fri, 11 Jan 2013 17:43:

Started POST "/categories/1/links" for 127.0.0.1 at 2013-01-11 12:43:44 -0500
Processing by LinksController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"pyMkh1eJ7WxYC978XKjdsyGOeGDvi6RTIOSGb9KMqkc=", "link"=>{"category_id"=>"1", "comment"=>"", "url"=>""}, "commit"=>"Post", "category_id"=>"1"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSE44 UTC +00:00], ["description", "Bloomberg is a premier site for business and financial market news. It delivers world economic news, stock futures, stock quotes, & personal finance advice."], ["favicon_url", "http://www.bloomberg.com/favicon.ico"], ["object_type", "link"], ["original_url", "http://bloomberg.com"], ["points", nil], ["profile_link", nil], ["provider_display", "www.bloomberg.com"], ["provider_url", "http://www.bloomberg.com"], ["thumbnail", nil], ["thumbnail_url", "http://www.bloomberg.com/imRT INTO "links" ("category_id", "comment", "created_at", "description", "favicon_url", "object_type", "original_url", "points", "profile_link", "provider_display", "provider_url", "thumbnail", "thumbnail_url", "title", "updated_at", "url", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["category_id", "1"], ["comment", ""], ["created_at", Fri, 11 Jan 2013 17:43:44 UTC +00:00], ["description", nil], ["favicon_url", nil], ["object_type", nil], ["original_url", nil], ["points", nil], ["profile_link", nil], ["provider_display", nil], ["provider_url", nil], ["thumbnail", nil], ["thumbnail_url", nil], ["title", nil], ["updated_at", Fri, 11 Jan 2013 17:43:44 UTC +00:00], ["url", ""], ["user_id", 1]]
   (1.1ms)  commit transaction
  Rendered links/create.js.erb (0.0ms)
Completed 200 OK in 8ms (Views: 3.9ms | ActiveRecord: 1.6ms)
age/is2KySnyVWmA.jpg"], ["title", "Business, Financial & Economic News, Stock Quotes"], ["updated_at", Fri, 11 Jan 2013 17:43:44 UTC +00:00], ["url", "bloomberg.com "], ["user_id", 1]]
   (2.5ms)  commit transaction
  Rendered links/create.js.erb (0.0ms)

これは私のフォームです:

<%= form_for([@category, @category.links.build], :remote => true, :class => "form-horizontal") do |f| %>
    <%= f.hidden_field :category_id, :value => params[:id] %>
    Comment: <%= f.text_field :comment %><BR>
    Link: <%= f.text_field :url %>
    <%= f.submit "Post", :class => "btn", :disable_with => '...', :id => "new_link_button" %>
<% end %>

<div class="selector" style="width:350px;margin:-30px 0px 0px 0px;"></div>
<!-- Placeholder that tells Preview where to put the loading icon-->
<div class="loading">
    <img src='http://embedly.github.com/jquery-preview/images/loading-rectangle.gif'>
</div>


<script>


$('#link_url').preview({ key:'60f1dcdf3258476794784148a6eb65e7', // Sign up for a key: http://embed.ly/pricing

      selector : {type:'rich'},
      preview : {
        submit : function(e, data){
          $.ajax({
            dataType: 'script',
            url: this.form.attr('action'),
            type: 'POST',
            data: data
          });
        },
      },
      autoplay : 0,
      maxwidth : 350,
      display : {display : 'rich'}
});

$('#new_link_button').click(function(e) {
    e.preventdevault();
    $('.new_link').submit();
    return false;
});

</script>

これは私のルートです:

  resources :categories, :only => [:new, :show, :create, :edit, :update] do
    resources :links, :only => [:new, :show, :create, :edit, :update]
    resources :industries, :only => [:new, :show, :create, :edit, :update]
    resources :territories, :only => [:new, :show, :create, :edit, :update]
  end
4

3 に答える 3

0

保存する前に設定してみてください。

def create
  @link = Link.new(params[:link])
  @link.category_id = params[:category_id]
  @link.original_url = params[:original_url]
  @link.title = params[:title]
  @link.description = params[:description]
  @link.user_id = current_user.id
  # etc...
  respond_to do |format|
    if @link.save
      # do things...
    else 
      # do other things...
    end
  end
end
于 2013-01-11T16:48:29.547 に答える
0

フォームの作成方法に問題があると思います。投稿されたパラメータでわかるように、params[:link]リンクに保存しようとしているデータのサブセットのみが含まれます。

"link"=>{"category_id"=>"1", "comment"=>"", "url"=>"yahoo.com "}

残りはすべてparams配列の最上位にあります。

Parameters: {... "original_url"=>"http://yahoo.com", "url"=>"http://www.yahoo.com/", "title"=>"Yahoo!", "description"=>"Welcome to Yahoo!..." ...}

form_forを使用してフォームを作成する方法を確認し、追加のパラメーターに正しく使用されていることを確認する必要があります。

編集:

フォームを編集できない場合は、コントローラーでこれを行うことができます。

@link.user_id = current_user.id
links_attributes = params.slice(:original_url, :title, :description, :favicon_url, :provider_url, :provider_display, :thumbnail_url, :object_type)
@link.attributes = links_attributes
respond_to do |format| 
   if @link.save
      ...
于 2013-01-11T16:36:51.760 に答える
0

まず、@link.savethenを呼び出す必要はありません。データベースにまだ存在しない場合は保存される@link.update_attributesためです。@link.update_attributes@link

あなたが投稿したログから、コントローラーが2つの投稿リクエストを受け取ったようです。これが2つのオブジェクトを作成する理由です。理由は、2つの送信が発生したためだと思います。あなたの$('#link_url').preview(...コード。

于 2013-01-11T18:00:56.150 に答える