Fenec
Fenec

Reputation: 1222

Rails3: not render view if no record found

Here's my controller method

def check    
 @added_word = Word.where(:word => params[:word][:word]).first

 respond_to do |format|  
  format.js     
 end
end

In the view I have the following code:

    $("#added_words").append("<%= @added_word.word %>, ");    

How should i change the controller in order not to render the view if no record found ( false returned)?

Upvotes: 0

Views: 470

Answers (1)

clyfe
clyfe

Reputation: 23770

Not very idiomatic, but it answers your question.

def check    
  @added_word = Word.where(:word => params[:word][:word]).first
  if @added_word.present?
    respond_to {|f| f.js}
  else
    render :text => '' # or whatever
  end
end

Upvotes: 1

Related Questions