<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ゆーすけぶろぐ &#187; view</title>
	<atom:link href="http://yusukezzz.net/blog/archives/tag/view/feed" rel="self" type="application/rss+xml" />
	<link>http://yusukezzz.net/blog</link>
	<description>yusukezzz&#039;s weblog... ぷろぐらむとか ruby, android, java, thinkpad, milestone2</description>
	<lastBuildDate>Wed, 28 Dec 2011 13:21:54 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Railsのform_forでの注意</title>
		<link>http://yusukezzz.net/blog/archives/1494</link>
		<comments>http://yusukezzz.net/blog/archives/1494#comments</comments>
		<pubDate>Tue, 04 Aug 2009 09:56:38 +0000</pubDate>
		<dc:creator>yusukezzz</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://yusukezzz.net/blog/archives/1494</guid>
		<description><![CDATA[のような書き方をするとうまく保存されません WARNING: Can&#8217;t mass-assign these protected attributes: id のような警告をlogに吐きます どうやらidは上 [...]]]></description>
			<content:encoded><![CDATA[<pre class="brush: ruby; title: ; notranslate">
# view
&lt;% form_for :hoge, :url =&gt; {:action =&gt; :update} do |f| %&gt;
  &lt;% f.hidden_field :id, {:value =&gt; @hoge.id} %&gt;
  &lt;% f.text_field :name, {:value =&gt; @hoge.name} %&gt;
  &lt;% f.submit %&gt;
&lt;% end %&gt;
</pre>
<pre class="brush: ruby; title: ; notranslate">
# controller
def update
  if params[:hoge]
    hoge = Hoge.find_by_id(params[:hoge][:id])
    hoge.attributes = params[:hoge]
    hoge.save!
  end
end
</pre>
<p>のような書き方をするとうまく保存されません<br />
WARNING: Can&#8217;t mass-assign these protected attributes: id<br />
のような警告をlogに吐きます<br />
どうやらidは上書きも認められてないようです<br />
hidden_fieldではなく、form_forの引数として渡すとうまく行きます</p>
<pre class="brush: ruby; title: ; notranslate">
# view
&lt;% form_for :hoge, :url =&gt; {:action =&gt; :update, :id =&gt; @hoge.id} do |f| %&gt;
  &lt;% # f.hidden_field :id, {:value =&gt; @hoge.id} %&gt;
  &lt;% f.text_field :name, {:value =&gt; @hoge.name} %&gt;
  &lt;% f.submit %&gt;
&lt;% end %&gt;
</pre>
<pre class="brush: ruby; title: ; notranslate">
# controller
def update
  if params[:hoge]
    # hoge = Hoge.find_by_id(params[:hoge][:id])
    hoge = Hoge.find_by_id(params[:id])
    hoge.attributes = params[:hoge]
    hoge.save!
  end
end
</pre>
<p>これでうまいこと更新出来ます</p>
]]></content:encoded>
			<wfw:commentRss>http://yusukezzz.net/blog/archives/1494/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

