# Campo nascosto in Rails 3 non salva campo

**URL:** <https://www.ruby-forum.com/t/campo-nascosto-in-rails-3-non-salva-campo/219581>\
**Category:** IT\
**Created:** [May 8, 2012, 4:44am UTC](https://www.ruby-forum.com/t/campo-nascosto-in-rails-3-non-salva-campo/219581 "2012-05-08T04:44:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 8, 2012, 4:44am UTC](https://www.ruby-forum.com/t/campo-nascosto-in-rails-3-non-salva-campo/219581/1 "2012-05-08T04:44:26Z")

</div>

Salve a tutti,

solito problema di campo nascosto che non mi viene salvato nel db ma in  
Rails si tratta di questi dettagli per far funzionare le cose [lo so  
esiste già’ un post su questo ma avendolo letto sono arrivato qua oltre  
non riesco ad andare]

allora ho l’oggetto post che nella vista app/views/posts/show.htlm.erb  
ha questo form

\<%= form\_for @comment do |f| %\>  
\<%= render ‘shared/error\_messages’, :object =\> f.object %\>

\<%= f.hidden\_field :post\_id, :value =\> @post.id %\>

```
<%= f.text_area :text %>

```

 \<%= f.submit "Submit" %\> 
\<% end %\>

mentre nel controller posts\_controller.rb ho

def show

@post = Post.find(params[:id])  
@comment = Comment.new

end

e per gli oggetti post e comment ho quanto segue

post

title:string, text:text

comment

post\_id:integer, text:text

quindi anche se nella pagina web ho il value 2 per il post questo non mi  
viene memorizzato e precisamente ho:

grazie in anticipio per il tempo che dedicherete nella risposta

cordialmente,

C

---

<div class="post-metadata">

**Author:** ![cluter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@cluter](https://www.ruby-forum.com/u/cluter)\
**Post date:** [May 8, 2012, 10:25am UTC](https://www.ruby-forum.com/t/campo-nascosto-in-rails-3-non-salva-campo/219581/2 "2012-05-08T10:25:55Z")

</div>

Puoi postare il metodo che salva il commento?

Il 08/05/12 04:44, Cluter V. ha scritto:

---

<div class="post-metadata">

**Author:** ![cluter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@cluter](https://www.ruby-forum.com/u/cluter)\
**Post date:** [May 8, 2012, 4:04pm UTC](https://www.ruby-forum.com/t/campo-nascosto-in-rails-3-non-salva-campo/219581/3 "2012-05-08T16:04:11Z")

</div>

eccolo

/////////////  
class CommentsController \< ApplicationController

before\_filter :authenticate, :only =\> [:create, :destroy]

def create  
@comment = current\_user.comments.build(params[:comment])  
if @comment.save  
flash[:success] = “Comment created!”  
redirect\_to root\_path  
else  
render ‘pages/home’  
end  
end

def destroy  
end

end  
///////////

---

<div class="post-metadata">

**Author:** ![cluter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@cluter](https://www.ruby-forum.com/u/cluter)\
**Post date:** [May 10, 2012, 8:57pm UTC](https://www.ruby-forum.com/t/campo-nascosto-in-rails-3-non-salva-campo/219581/4 "2012-05-10T20:57:35Z")

</div>

Ciao a tutti (ricordo che sono un autodidatta),

ho trovato la soluzione guardando questo TUTORIAL:

[![](https://img.youtube.com/vi/JaL9ul17kx0/maxresdefault.jpg "Blog in less than 10 minutes using Ruby on Rails") ](https://www.youtube.com/watch?v=JaL9ul17kx0)

nel controller app/controller/comments\_controller.rb ho modificato il  
metodo create cosi’

def create

@post = Post.find(params[:post\_id])  
@comment = @question.comments.build(params[:comment])

@comment.save

redirect\_to @post

end

nella view app/views/posts/show.htlm.erb invece ho inserito

\<%= form\_for [@post, @post.comments.build] do |f| %\>

\<%= f.text\_field :text %\>

\<%= f.submit ‘insert comment’ %\>

nella routes.rb ho messo come nel tutorial su indicato

resources :posts do

```
resources :comments

```

end

e nei modelli post e comment

class Comment \< ActiveRecord::Base

belongs\_to :post

end

class Post \< ActiveRecord::Base

has\_many :posts

end

a presto,

C
