Topic: status , notice and some other parameters don't work!

Hi bros.
I got a tricky problem as far as I'm lossing my temper neutral
I'm working on rails 3.2 and when I use render or redirect_to with status or notice paramteres, they don't work!
for example the error that should be shown by notice is not shown and for example unprocessble_entity doesn't work!

what to do to get rid of this fuckin' problem?

Re: status , notice and some other parameters don't work!

To be able to help you, please post the code responsible for rendering and displaying the message.

Re: status , notice and some other parameters don't work!

<% if has_answered.count == 0 %>
  
    <%= render :partial => 'contests/answer' %> 

  <% else %>
 
    <%= render :partial => 'contests/user_answer' %>
 
  <% end %>

The above code is executed when we wanna see the questions and the answeres.
If user has answered the questions before this code will be rendered :

<ol>
  <% @contest.questions.each do |question| %>
    <li> <%= question.content %></li>
    <% p current_user.answers.count %>
    <% p current_user.id %>
    <%= current_user.answers.where(question_id: question.id).last.content %>
  <% end %>  
</ol>

And if user has not answered the questions this code will be rendered:

  <%= form_for @user_answer do |user_answer| %>

      <ol>
        
        <%= fields_for :answers do |answer| %>
          <% @contest.questions.all.each do |question| %>
            <li> <%= question.content %></li>

            <%= answer.text_area :content , name: "questions[#{question.id}][answers[content]]"%>
            <%= answer.hidden_field :question_id , value: question.id, name: "questions[#{question.id}][answers[question_id]]" %>
          <% end %>  

        <% end %>
    </ol>
      

    <%= user_answer.submit %>
    <% end %>
 

In this form, when I leave the inputs empty according to the validation , the errors should appear and this is done by this code:
(create controller)

def create
    puts params
    @user_answer = UserAnswer.new(params[:user_answer])

    @user_answer.contest_id = Contest.last.id
    @user_answer.user_id = current_user.id  
 
    respond_to do |format|
      if @user_answer.save
      
        params[:questions].each_value do |q|
          @answer = Answer.new(q[:answers])
          @answer.user_id = current_user.id
          @answer.user_answer_id = @user_answer.id
          if !@answer.save
            @user_answer.delete
            format.html { redirect_to Contest.last }
            format.json { render json: @answer.errors, status: :unprocessable_entity}
          end
        end

        format.html { redirect_to questions_path, notice: 'User answer was successfully created.' }
        format.json { render json: @user_answer, status: :created, location: @user_answer }
      else
        format.html { render action: "new" }
        format.json { render json: @user_answer.errors, status: :unprocessable_entity }
      end
    end
  end

Every thing after passing empty input work right and redirection also works right but there is not error about validation! but the object is not saved because of the validator protection! seems strange!

And I found something else that is strange! In a model , When I redirect the page to the show action after updating an object the notice is shown but when I redirect the page to the index action after updating an object the notice is not shown!!!!!!!!!

Re: status , notice and some other parameters don't work!

Check your layout code (see views/layouts/application.html.erb), you should have something like that:

<% flash.each do |name, msg| %>
      <%= content_tag :div, msg, :id => "flash_#{name}" %>
    <% end %>

Re: status , notice and some other parameters don't work!

The application file didn't contain above code and so I added it and problem was solved but there is still a little problem.
By notices are shown but errors which should be shown by validator are not shown ! ( but validation doesn't allow to save the object too!)

Re: status , notice and some other parameters don't work!

In your view (it could be a partial _form.html.erb, for example or smth else), i.e. the view which is responsible for creating a new/updating existing object, you should also have the code as follows to be able to display errors:

<%= form_for(@user_answer) do |f| %>
  <% if @user_answer.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user_answer.errors.count, "error") %> prohibited this Answer from being saved:</h2>

      <ul>
      <% @user_answer.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


... #the rest of your view code
<%end%>

I'd advice you once again to read one of the official RoR tutorials, follow Rails guides to really understand how the things works.

Last edited by Javix (2012-06-08 07:16:36)

Re: status , notice and some other parameters don't work!

Yeah but what if we have nested model or fields_for like this ? like this:

<%= form_for @answer_score do |answer_score| %>
  
  <%= fields_for @answer_score do |score| %>
    
    <ol>
    <% @a = false %>
    <% Contest.last.questions.each do |question| %>
      
      <li> <%= question.content %></li>
      <%= @answers.where(question_id: question.id).last.content %>
      <% answer = @answers.where(question_id: question.id).last %>
        <% if AnswerScore.where(answer_id: answer.id).where(judger_id: current_user.id).count > 0 %>
            <%= AnswerScore.where(answer_id: answer.id).where(judger_id: current_user.id).last.score %>
        <% else %>
            <% @a = true %>
            <%= score.hidden_field :answer_id , value: answer.id , name: "answers[#{answer.id}][answer_scores[answer_id]" %>
            <%= score.hidden_field :judger_id , value: current_user.id , name: "answers[#{answer.id}][answer_scores[judger_id]"%>
            <%= score.number_field :score , name: "answers[#{answer.id}][answer_scores[score]"%>
        <% end %> 
        
        <br>
    
      <% end %>  
    
    </ol>
  
  <% end %>
<% if @a %>
  <%= answer_score.submit %>
<% end %>

<% end %>

<%= link_to "back to index", judgers_path %>
 

Here we wanna show validation errors for score object that is not a single object

Last edited by ibtkm (2012-06-08 07:40:18)