# Problem with unsaved commenti

**URL:** <https://www.ruby-forum.com/t/problem-with-unsaved-commenti/259307>\
**Category:** Deployment\
**Created:** [April 20, 2020, 7:30pm UTC](https://www.ruby-forum.com/t/problem-with-unsaved-commenti/259307 "2020-04-20T19:30:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiGi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a587f6/32.png) [@GiGi](https://www.ruby-forum.com/u/GiGi)\
**Post date:** [April 20, 2020, 7:30pm UTC](https://www.ruby-forum.com/t/problem-with-unsaved-commenti/259307/1 "2020-04-20T19:30:14Z")

</div>

I use Device as Utente model, I have a sort of blog composed of controllers and model Messaggi and controllers model Commenti. The Messaggi work and are saved through Utente ID, while I can’t solve the problem for the saving of Commenti. Even if it doesn’t display any error.

my MessaggiController

> class MessaggiController \< ApplicationController  
> before\_action :authenticate\_utente!
> 
> def index  
> argo = params[:argo]  
> if !argo.nil?  
> messaggi = Messaggio.where(:argomento\_id =\> argo)  
> argomento = Argomento.where(:id =\> argo).first.argomento  
> else  
> messaggi = Messaggio.all  
> end  
> argomenti = Argomento.all  
> end
> 
> def show  
> messaggio = Messaggio.find(params[:id])  
> commento = Commento.new  
> commento.messaggio\_id = messaggio.id  
> end
> 
> def new  
> messaggio = current\_utente.messaggi.build  
> commento = Commento.new  
> commento.messaggio\_id = messaggio.id  
> end
> 
> def create  
> messaggio = current\_utente.messaggi.build(messaggio\_params)
> 
> ```
> respond_to do |format|
> if messaggio.save
> format.html { redirect_to messaggio, notice: 'Messaggio was successfully created.' }
> format.json { render :show, status: :created, location: messaggio }
> else
> format.html { render :new }
> format.json { render json: messaggio.errors, status: :unprocessable_entity }
> end
> end
> 
> ```
> 
> end
> 
> def edit  
> messaggio = Messaggio.find(params[:id])  
> end
> 
> def update  
> messaggio = Messaggio.find(params[:id])  
> respond\_to do |format|  
> if messaggio.update(messaggio\_params)  
> format.html { redirect\_to messaggio, notice: ‘Messaggio was successfully updated.’ }  
> format.json { render :show, status: :ok, location: messaggio }  
> else  
> format.html { render :edit }  
> format.json { render json: messaggio.errors, status: :unprocessable\_entity }  
> end  
> end  
> end
> 
> def destroy  
> messaggio = Messaggio.find(params[:id])  
> messaggio.destroy  
> respond\_to do |format|  
> format.html { redirect\_to messaggi\_url, notice: ‘Messaggio was successfully destroyed.’ }  
> format.json { head :no\_content }  
> end  
> end
> 
> ```
> private
> 
> def messaggio_params
> params.require(:messaggio).permit(:titolo, :testo, :argomento_id, :utente_id)
> end
> 
> ```
> 
> end

My CommentsController

> class CommentiController \< ApplicationController
> 
> ```
> before_action :find_commento, only: [:create, :destroy]    
> before_action :commento_auth, only: [:destroy]    
> 
> def create    
> #@post = Post.find(params[:post_id])    
> #@comment = @post.comments.create(params[:comment].permit(:name, :body))    
> #redirect_to post_path(@post)
> 
> #new format    
> @messaggio = Messaggio.find(params[:messaggio_id])    
> #post = current_user.posts.build(post_params)    
> commento = current_utente.commenti.build(commento_params)    
> redirect_to messaggio_path(messaggio)    
> end   
> 
> def destroy    
> commento = messaggio.commenti.find(params[:id]).destroy    
> redirect_to messaggi_path  
> end    
> 
> private    
> 
> def commento_params    
> params.require(:commento).permit(:testo, :commento_id, :utente_id)    
> end   
> 
> def find_commento    
> messaggio = Messaggio.find(params[:messaggio_id])    
> end   
> 
> def commento_auth    
> if current_utente != messaggio.utente    
> redirect_to(messaggi_path)   
> end    
> end     
> 
> end
> 
> ```

and my relationship  
Utente.rb  
has\_many :messaggi  
has\_many :commenti

Messaggio.rb  
belongs\_to :utente  
has\_many :commenti

Commenti.rb  
belongs\_to :utente  
belongs\_to :messaggio

Thanks a lot for helping.
