0

I'm building a simple trivia app and am using bootstrap generators for my forms. In the new and update forms for my models, the model names aren't showing up in my views. Here's the code for app/views/questions/new.html.erb:

<div class="page-header">
  <h1>
    <%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => @model_name %>
  </h1>
</div>
<%= render :partial => 'form' %>

Here's my questions_controller.rb:

class QuestionsController < ApplicationController

  ...

  def new
    @model_name = Question.model_name.human
    @question = Question.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @question }
    end
  end

  ...

  private
    def question_params
      params.require(:question).permit(:body)
    end
end

The word New shows up in the view, but not the model name... Anyone know what's going on here?

FWIW I tried this with 2.0 and 1.9.3 with the same results.

Ryan Clark
  • 764
  • 1
  • 8
  • 29

2 Answers2

2

Defining things in the view is a very bad idea. If you have to do this then move it up into the controller.

# app/controllers/questions_controller.rb

class QuestionsController < ActionController::Base
  def new
    @question = Question.new
  end
end

# config/locales/questions/en.yml

helpers:
  titles:
    new: new %{model}

# app/views/questions/new.html.erb

<div class="page-header">
  <h1><%= I18n.t('helpers.titles.new', model: @question.class.human) %></h1>
</div>
<%= render :partial => 'form' %>
ChuckJHardy
  • 6,956
  • 6
  • 35
  • 39
  • Thanks, I agree with you about defining things in the view and have moved it into the controller, but I still don't get the model name in the view and there are no errors. – Ryan Clark Jun 06 '13 at 16:55
  • Am i missing something here? shouldn't it be `

    <%=t '.title', :default => [:'helpers.titles.new', "New %{@model_name}"], :model => @model_name %>

    `
    – usha Jun 06 '13 at 17:22
  • It's `[..., "New %{foo}], :foo => @bar ...` Doesn't really matter what goes in there. The issue seems to be with the `helpers.title.new`. I took that out and it works fine now. – Ryan Clark Jun 06 '13 at 17:30
0

I got rid of the helper and it works. Not sure why, but here is the final code:

<div class="page-header">
   <h1><%=t '.title', :default => 'New %{model_name}', :model_name => @model_name %></h1>
</div>
<%= render :partial => 'form' %>
Ryan Clark
  • 764
  • 1
  • 8
  • 29