Where to render comments controller in Rails on model validations failure? -
i have simple video model in rails app has_many
comments. displaying these comments on video's show page. when submit form works fine; however, if there validation errors on comment model, system blows up. if there validation errors on comment model, render video's show page again, validation error styling showing. how do inside of create action? lot!
class commentscontroller < applicationcontroller def create @video = video.find(params[:video_id]) @comment = @video.comments.build(params[:comment]) if @comment.save redirect_to @video, :notice => 'thanks posting comments.' else render # what? render in order show video page's show action validation error styling showing? please help! end end end
to you'll have render template:
class commentscontroller < applicationcontroller def create @video = video.find(params[:video_id]) @comment = @video.comments.build(params[:comment]) if @comment.save redirect_to @video, :notice => 'thanks posting comments.' else render :template => 'videos/show' end end end
keep in mind you'll have declare instance variables (like @video) inside of commentscontroller#create action though, because videoscontroller#show action not run, template rendered. instance, if have @video_name variable in videoscontroller#show action, you'll have add same @video_name instance variable commentscontroller#create action.
Comments
Post a Comment