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 %>
<div class="field">
<%= f.hidden_field :post_id, :value => @post.id %>
<%= f.text_area :text %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% 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:
<table class="comments" summary="User comment">
<form accept-charset="UTF-8" action="/comments"
class="new_comment" id="new_comment" method="post"><div
style="margin:0;padding:0;display:inline"><input name="utf8"
type="hidden" value="✓" /><input name="authenticity_token"
type="hidden" value="VFKCNKZZqcBDbWe0MZg7xxbzgcfo3+K9V5RG52cCCT8="
/></div>
<div class="field">
<input id="comment_post_id" name="comment[post_id]" type="hidden"
value="2" />
<textarea cols="40" id="comment_text" name="comment[text]"
rows="20"></textarea>
</div>
<div class="actions">
<input id="comment_submit" name="commit" type="submit"
value="Submit" />
</div>
</form>
grazie in anticipio per il tempo che dedicherete nella risposta
cordialmente,
C
on 2012-05-08 04:44
on 2012-05-08 10:25
Puoi postare il metodo che salva il commento? Il 08/05/12 04:44, Cluter Vipic ha scritto:
on 2012-05-08 16:04
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
///////////
on 2012-05-10 20:57
Ciao a tutti (ricordo che sono un autodidatta), ho trovato la soluzione guardando questo TUTORIAL: http://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| %> <p> <%= f.text_field :text %> </p> <%= 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.