Related
I wanted to create a three dropdown selects category>subcategory>susubcategory
I have followed this method here https://www.youtube.com/watch?v=ap551f2a_d0 with some changes to suit my needs. everything seemed fine and each dropdown gets populated based on the selection of the dropdown before but when I select any value in the dropdowns it does not keep it selected it just goes back to the default -Please select-! I am not sure where I went wrong.
note: I only have one module (categories for the categories and using the parant_id for the subcategories)
I would really appreciate it if someone can point me in the right direction. Thank you
my categories table
create_table :categories do |t|
t.string "name", limit: 255
t.text "description", limit: 65535
t.references "parent_id", limit: 4
t.boolean "important", default: false
t.integer "position", limit: 4, default: 0
t.timestamps
end
in modules category.rb
class Category < ApplicationRecord
validates :name, presence: true, uniqueness: true
has_many :jobs
belongs_to :parent_category, foreign_key: :parent_id, class_name: 'Category'
has_many :subcategories, foreign_key: :parent_id, class_name: 'Category'
has_many :subsubcategories, foreign_key: :parent_id, class_name: 'Category'
end
in modules job.rb
class Job < ApplicationRecord
validates :title,:category_id, :description, presence: true
validates :category, :presence => true
belongs_to :user
belongs_to :category , -> { order("name") }
end
dropdown_controller.js
import { Controller } from "#hotwired/stimulus";
// connect to data-controller="dropdown"
export default class extends Controller {
submit () {
this.element.requestSubmit();
}
}
in my jobs_controller.rb
before_action :authenticate_user!, except: [:index, :show]
before_action :set_categories
def index
#jobs = Job.all
end
def new
#categories = Category.where(parent_id: nil)
#subcategory = #subcategory&.category || []
#subcategories = #subcategories&.subcategories || []
#subsubcategories = #subsubcategories&.subsubcategories || []
#job = Job.new
end
def create
#job = Job.new(job_params.merge({ user: current_user }))
if #job.save
format.html { redirect_to root_path, notice: "Job was successfully created." }
else
render :new, status: :unprocessable_entity
end
end
private
def job_params
params.require(:job).permit(:title, :description,:category_id).merge(user: current_user)
end
def set_categories
#category = Category.find_by(id: params[:category].presence)
#categories = Category.find_by(id: params[:category].presence)
#subcategories = Category.find_by(id: params[:category].presence)
#subsubcategories = Category.find_by(id: params[:subcategories].presence)
end
end
views/jobs/new.html.erb
<%= turbo_frame_tag "form" do %>
<%= form_tag new_job_path, method: :get, data: { controller: "dropdown", action: "change->dropdown#submit" } do %>
<%= select_tag :category, options_from_collection_for_select(#categories, "id", "name", #category&.id ), prompt: "Select a category" %>
<%= select_tag :subcategories, options_from_collection_for_select(#subcategories, "id" , "name", #category&.id), prompt: "Select a subcategories category" %>
<%= select_tag :subsubcategories, options_from_collection_for_select(#subsubcategories, "id" , "name", #category&.id), prompt: "Select a subsubcategories category" %>
<% end %>
<% end %>
I think that you miss update select_tag after request successfully.
you create a new file views/jobs/new.turbo_stream.erb
Ex: The select_tag has category_id
<%= turbo_stream.update "category_id" do %>
<%= select_tag :category, options_from_collection_for_select(#categories, "id", "name", #category&.id ), prompt: "Select a category" %>
<% end %>
Make sure select_tag selected value with #category&.id (ex: #category&.id = 1)
I done it but i have 2 select_tags.
https://drive.google.com/file/d/1MBY3T0_O2TAE6zFUywDsEoIOjnxr1hMy/view?usp=share_link
P/S: Contact me if you wanna know anymore.
This was the solution to the problem:
my categories table
create_table :categories do |t|
t.string "name", limit: 255
t.text "description", limit: 65535
t.integer "parent_id", limit: 4
t.boolean "important", default: false
t.integer "position", limit: 4, default: 0
t.timestamps
end
in modules category.rb
class Category < ApplicationRecord
validates :name, presence: true
has_many :jobs
belongs_to :parent, foreign_key: :parent_id, class_name: 'Category' , :optional => true
has_many :subcategories, foreign_key: :parent_id, class_name: 'Category'
has_many :subsubcategories, foreign_key: :parent_id, class_name: 'Category'
end
in modules job.rb
class Job < ApplicationRecord
validates :title,:category_id, :description, presence: true
validates :category, :presence => true
belongs_to :user
belongs_to :category , -> { order("name") }
belongs_to :subcategories, class_name: "Category"
belongs_to :subsubcategories, class_name: "Category"
end
dropdown_controller.js
import { Controller } from "#hotwired/stimulus";
// connect to data-controller="dropdown"
export default class extends Controller {
submit () {
this.element.requestSubmit();
}
}
in my jobs_controller.rb
class JobsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
before_action :set_categories
def index
#jobs = Job.all
end
def new
#category = Category.find_by(id: params[:category])
#categories = Category.where(parent_id: nil)
#subcategories = #category.subcategories if #category
#subsubcategories = Category.where(:parent_id => params[:subcategories])
#job = Job.new
end
def create
#job = Job.new(job_params.merge({ user: current_user }))
if #job.save
format.html { redirect_to root_path, notice: "Job was successfully created." }
else
render :new, status: :unprocessable_entity
end
end
private
def job_params
params.require(:job).permit(:title, :description,:category_id).merge(user: current_user)
end
def set_categories
#category = Category.find_by(id: params[:category].presence)
#subcategory = Category.find_by(id: params[:subcategories])
#subsubcategory = Category.find_by(id: params[:subsubcategories])
end
end
views/jobs/new.html.erb
<%= turbo_frame_tag "form" do %>
<%= form_tag new_job_path, method: :get, data: { controller: "dropdown", action: "change->dropdown#submit" } do %>
<%= select_tag :category, options_from_collection_for_select(#categories, "id", "name", #category&.id ), prompt: "Select a category" %>
<%= select_tag :subcategories, options_from_collection_for_select(#subcategories || [], "id" , "name", #subcategory&.id), prompt: "Select a subcategories category" %>
<%= select_tag :subsubcategories, options_from_collection_for_select(#subsubcategories || [], "id" , "name", #subsubcategory&.id), prompt: "Select a subsubcategories category" %>
<% end %>
<% end %>
Im a bit new to Ruby on Rails so I'm hoping this isn't a stupid question.
I'm trying to make an application to store played fifa games.
This requires 2 users linked to one game, but offcourse one user can also have many games, therefore I used a many-to-many relation.
Controller:
class GamesController < ApplicationController
before_action :authenticate_user!, exept: [:index, :show]
def index
#games = Game.all
end
def new
#game = current_user.games.build
#user_options = User.all.map{|u| [ u.email, u.id ] }
end
def create
#user_options = User.all.map{|u| [ u.email, u.id ] }
#game = Game.new(game_params)
#game.home_team_user_id = current_user.id
#game.home_team_user_name = current_user.email
if #game.save
redirect_to games_path, :notice => "Successfully added game!"
else
render 'index'
end
end
def show
#games = Game.all
end
def destroy
#game = Game.find(params[:id])
#game.destroy
redirect_to games_path
end
private
def find_game
#game = Game.find(params[:id])
end
def game_params
params.require(:game).permit(:home_team_user_name, :home_score, :away_team_user_name, :away_score, :home_team_user_id, :away_team_user_id)
end
end
View File:
<div class="panel panel-default" style="margin-left: 10px; margin-right:10px">
<!-- Default panel contents -->
<div class="panel-heading">Played games</div>
<!-- Table -->
<table class="table">
<thead>
<th>Home Team</th>
<th>Home Team Score</th>
<th>Away Team Score</th>
<th>Away Team</th>
<th>Added by</th>
<th>Actions</th>
</thead>
<% #games.each do |game|%>
<tbody>
<td>
<%= game.home_team_user_id %>
</td>
<td>
<%= game.home_score %>
</td>
<td>
<%= game.away_score %>
</td>
<td>
<%= game.away_team_user_id %>
</td>
<td>
</td>
<td>
<%= link_to "Delete", game, method: :delete, data: { confrim: "Are you sure?"} %>
</td>
<% end %>
</tbody>
</table>
</div>
New Game form:
<div class="col-md-12" style="text-align:center">
<div class="panel panel-default" style="margin-right:10px">
<div class="panel-heading">
<h3 class="panel-title">Submit New Match</h3>
</div>
<div class="panel-body">
<%= simple_form_for(#game) do |f| %>
<%= f.text_field :home_score, :placeholder => "Your score" %>
<%= f.text_field :away_score, :placeholder => "Your Opponents score" %>
<%= f.select(:away_team_user_id, #user_options) %>
<%= f.submit "Submit Match", class: "btn-submit" %>
<% end %>
<!-- <%= form_for #game do |f| %>
<%= collection_select(:game, :away_team_user_id, User.all, :id, :email, prompt: true ) %>
<%= f.text_field :home_score, :placeholder => "Your score" %>
<%= f.text_field :away_score, :placeholder => "Your Opponents score" %>
<%= f.submit "Submit Match", class: "btn-submit" %>
<% end %> -->
</div>
</div>
Now I'm able to add games with the
home_team_user_id = current_user
away_team_user_id = user selected from dropdown menu
and offcourse the score.
However in the view the email adresses should be shown in stead of the id's.
I've tried many solutions like <%= game.home_team_user_id.user.email %>
This gives the error : undefined method `user' for 1:Fixnum
Can anybody help me on this mather? What am I doing wrong?
Thx in advance!
ADDITIONAL QUESTION:
Hi there so I tried the last answer but I'm getting an error.
undefined local variable or method `game' for
Controller:
class GamesController < ApplicationController
before_action :authenticate_user!, exept: [:index, :show]
def index
#games = Game.all
#home_user = User.select(:id, :user_name).find(game.home_team_user_id)
end
def new
#game = current_user.games.build
#user_options = User.all.map{|u| [ u.user_name, u.id ] }
end
def create
#user_options = User.all.map{|u| [ u.user_name, u.id ] }
#game = Game.new(game_params)
#game.home_team_user_id = current_user.id
if #game.home_score > #game.away_score
#game.winner_id = #game.home_team_user_id
#game.loser_id = #game.away_team_user_id
else
#game.winner_id = #game.away_team_user_id
#game.loser_id = #game.home_team_user_id
end
if #game.save
redirect_to games_path, :notice => "Successfully added game!"
else
render 'index'
end
end
def show
#games = Game.all
end
def destroy
#game = Game.find(params[:id])
#game.destroy
redirect_to games_path
end
private
def find_game
#game = Game.find(params[:id])
end
def game_params
params.require(:game).permit(:home_team_user_name, :home_score, :away_team_user_name, :away_score, :home_team_user_id, :away_team_user_id, :winner_id, :loser_id)
end
end
Models:
Game:
class Game < ActiveRecord::Base
has_many :user_games
has_many :users, through: :user_games
belongs_to :winner, class_name: 'User' # or Team or whatever
belongs_to :loser, class_name: 'User'
before_validation :evaluate_score!, if: -> { home_score.present? }
private
def evaluate_score!
self.winner = home_score > away_score ? home_team_user : away_team_user
self.loser = home_score < away_score ? home_team_user : away_team_user
end
end
User:
class User < ActiveRecord::Base
has_many :user_games
has_many :games, through: :user_games
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
user_game:
class UserGame < ActiveRecord::Base
belongs_to :game
belongs_to :user
end
Schema:
ActiveRecord::Schema.define(version: 20160331142037) do
create_table "game_users", force: :cascade do |t|
t.integer "user_id_id"
t.integer "game_id_id"
end
create_table "games", force: :cascade do |t|
t.string "home_team_user_name"
t.string "home_score"
t.string "away_team_user_name"
t.string "away_score"
t.integer "home_team_user_id"
t.integer "away_team_user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "winner_id"
t.integer "loser_id"
end
create_table "user_games", force: :cascade do |t|
t.integer "user_id"
t.integer "game_id"
t.integer "user_id_id"
t.integer "game_id_id"
end
add_index "user_games", ["game_id_id"], name: "index_user_games_on_game_id_id"
add_index "user_games", ["user_id_id"], name: "index_user_games_on_user_id_id"
create_table "users", force: :cascade do |t|
t.string "user_name"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "win"
t.integer "los"
t.integer "draw"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
In your controller, try:
#home_user = User.select(:id, :email).find(game.home_team_user_id)
In your view:
<%= #home_user.email %>
Thanks for the comment, this worked for me:
<%= User.find(game.home_team_user_id).email %>
I am guessing you have a has_many relation in the model, then in your view you should use:
<% game.home_team_users.each do |home_team| %>
<%= home_team.email %>
<% end %>
Added:
I am going to ignore that you are using #games = Game.all in def show, which is the same as in def index. :)
The show should display view only related to one record, and index should be used for all records
I have a new Match form:
EDIT:
= form_for(#match) do |f|
= f.label :match_date
= f.date_select :match_date, :order => [:day, :month, :year]
= f.label :player_ids, 'Select players'
= f.collection_select :player_ids, #players, :id, :lastname, {}, { multiple: true }
= f.fields_for :match_edits do |ff|
= ff.label :result
= ff.number_field :result, in: 0..10
%div
= f.button :submit
When I choose two players I want set for each one match result like this:
id: 1, match_id: 1, player_id: 1, result: 4
id: 2, match_id: 1, player_id: 2, result: 10
I'm new in rails and I don't know how to fix that
MatchController
class MatchesController < ApplicationController
respond_to :html
def index
#matches = Match.all
end
def show
#match = Match.find(params[:id])
#results = #match.match_edits
end
def new
#match = Match.new
#players = Player.all
2.times {#match.match_edits.build}
end
def create
#match = Match.new(match_params)
respond_to do |format|
if #match.save
format.html { redirect_to #match, notice: 'Match was successfully created.' }
format.json { render :show, status: :created, location: #match }
else
format.html { render :new }
format.json { render json: #match.errors, status: :unprocessable_entity }
end
end
end
private
def match_params
params[:match].permit :match_date, player_ids: [], :match_edits_attributes => [:id, :result]
end
end
MatchEdit model
class MatchEdit < ActiveRecord::Base
belongs_to :match
belongs_to :player
end
Match model
class Match < ActiveRecord::Base
has_many :match_edits
has_many :players, through: :match_edits
accepts_nested_attributes_for :match_edits, allow_destroy: true, reject_if: proc { |attrs| attrs['result'].blank? }
end
Schema.rb
ActiveRecord::Schema.define(version: 20150629144534) do
create_table "match_edits", force: :cascade do |t|
t.integer "match_id"
t.integer "player_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "result"
end
add_index "match_edits", ["match_id"], name: "index_match_edits_on_match_id"
add_index "match_edits", ["player_id"], name: "index_match_edits_on_player_id"
create_table "matches", force: :cascade do |t|
t.date "match_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "players", force: :cascade do |t|
t.string "firstname"
t.string "lastname"
t.string "picture"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
You need to update your form to remove redundant code i.e.:
= ff.number_field :result, in: 0..10
Your form will look like:
= form_for(#match) do |f|
= f.label :match_date
= f.date_select :match_date, :order => [:day, :month, :year]
= f.label :player_ids, 'Select players'
= f.collection_select :player_ids, #players, :id, :lastname, {}, { multiple: true }
= f.fields_for :match_edits do |ff|
= ff.label :result
= ff.number_field :result, in: 0..10
%div
= f.button :submit
Your controller's new method is responsible to to provide multiple fields for result:
class MatchsController << ApplicationContoller
...
def new
...
2.times { #match.match_edits.build }
...
end
...
end
Your model should allow to accept nested attributes as following:
class Match
...
has_many :match_edits
accepts_nested_attributes_for :match_edits, allow_destroy: true,
reject_if: proc { |attrs| attrs['result'].blank? }
...
end
class MatchEdit
...
belongs_to :match
...
end
I found solution. Form should look like this:
= form_for(#match) do |f|
= f.label :match_date
= f.date_select :match_date, :order => [:day, :month, :year]
= f.fields_for :match_edits do |ff|
= ff.label :player_id, 'Select player'
= ff.collection_select :player_id, #players, :id, :lastname, {}, { multiple: false }
= ff.label :result
= ff.number_field :result, in: 0..10
%div
= f.button :submit
and in matches_controller:
def match_params
params[:match].permit :match_date, :match_edits_attributes => [:id, :result, :player_id]
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do I make it automatically pull up the current users name when I go to the user profile page. Like I click on the "message me" button and instead of pulling all the users on the site, I want it to pull automatically the user I am looking at.
Right now when I press the "message me" button it pulls up all the users and I have to choose which user to send the message. What can I do? I can use <%= user.name %> to pull up the user but that isn't going to help me.
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#exampleModal" data-whatever="<%= #user.name%>">Message Me</button>
<%= f.collection_select (:recipients, User.all.collect {|p| [ p.name, p.id ] }, {}, { multiple: true , class: "chosen-select form-control" })%>
Full code:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %>
<div class="panel2-signup">
<div class="panel-body-signup">
<div class="modal-body">
<form>
<div class="form-group">
To <%= f.collection_select (:recipients, User.all.collect {|p| [ p.name, p.id ] }, {}, { multiple: true , class: "chosen-select form-control" })%>
Subject:
<%= f.text_field :subject, class: "form-control" %>
Message:
<%= f.text_area :body, class: 'form-control', placeholder: "Type your message here", rows: 4 %>
</div>
</div>
Here is the db info
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :mailboxer_conversations do |t|
t.column :subject, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Receipts
create_table :mailboxer_receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :is_read, :boolean, :default => false
t.column :trashed, :boolean, :default => false
t.column :deleted, :boolean, :default => false
t.column :mailbox_type, :string, :limit => 25
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Notifications and Messages
create_table :mailboxer_notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.references :sender, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.string :notification_code, :default => nil
t.references :notified_object, :polymorphic => true
t.column :attachment, :string
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
t.boolean :global, default: false
t.datetime :expires
end
#Indexes
#Conversations
#Receipts
add_index "mailboxer_receipts","notification_id"
#Messages
add_index "mailboxer_notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "mailboxer_receipts", "mailboxer_notifications", :name => "receipts_on_notification_id", :column => "notification_id"
#Messages
add_foreign_key "mailboxer_notifications", "mailboxer_conversations", :name => "notifications_on_conversation_id", :column => "conversation_id"
end
def self.down
#Tables
remove_foreign_key "mailboxer_receipts", :name => "receipts_on_notification_id"
remove_foreign_key "mailboxer_notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :mailboxer_receipts
drop_table :mailboxer_conversations
drop_table :mailboxer_notifications
end
end
The ConversationsController.rb
class ConversationsController < ApplicationController
before_action :authenticate_user!
before_action :get_mailbox
def new
end
def index
end
def create
recipients = User.where(id: conversation_params[:recipients])
if
conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
flash[:notice] = "Your message was successfully sent!"
redirect_to conversation_path(conversation)
else
flash[:alert] = "Error. Message was not created"
redirect_to new_conversation_path
end
end
def show
#receipts = conversation.receipts_for(current_user).order("created_at ASC")
# mark conversation as read
conversation.mark_as_read(current_user)
end
def reply
current_user.reply_to_conversation(conversation, message_params[:body])
flash[:notice] = "Your reply message was successfully sent!"
redirect_to conversation_path(conversation)
end
def trash
conversation.move_to_trash(current_user)
redirect_to mailbox_inbox_path
end
def untrash
conversation.untrash(current_user)
redirect_to mailbox_inbox_path
end
def delete
#mailbox.trash.each do |conversation|
conversation.receipts_for(current_user).update_all(deleted: true)
end
flash[:notice] = 'Your trash was cleaned!'
redirect_to mailbox_inbox_path
end
private
def conversation_params
params.require(:conversation).permit(:subject, :body,recipients:[])
end
def get_mailbox
#mailbox ||= current_user.mailbox
if #user = current_user
#post = current_user.posts.build
else
end
if #user = current_user
#post = current_user.posts.build
#purchased = Sale.where(buyer_email: current_user.email).order("created_at DESC").order("created_at DESC").paginate(page:params[:page], per_page: 1 )
#sales = Sale.where(seller_email: current_user.email).order("created_at DESC").order("created_at DESC").paginate(page:params[:page], per_page: 1 )
else
end
end
def message_params
params.require(:message).permit(:body, :subject)
end
end
Looking at your action filters it looks like you are using the devise gem for user management.
To get the current authenticated user in devise you can use the helper method current_user.
I have an area model that belongs to a report model. I have built a form partial using SimpleForm. When I go to new_report_area_path(#report), I get a New Area form that works just fine. Enter the details and hit submit and it creates an area and takes you to area#show. But the button on the new area form says "Update Area" not "Create Area". Why?
config/routes.rb:
Testivate::Application.routes.draw do
resources :reports do
resources :areas
end
end
db/schema.rb:
ActiveRecord::Schema.define(:version => 20121205045544) do
create_table "areas", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "report_id"
end
create_table "reports", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
app/models/area.rb:
class Area < ActiveRecord::Base
attr_accessible :name
has_many :heuristics
belongs_to :report
end
app/models/report.rb:
class Report < ActiveRecord::Base
attr_accessible :name
has_many :heuristics
has_many :areas
end
app/controllers/areas_controller.rb:
class AreasController < ApplicationController
filter_resource_access
def new
#report = Report.find(params[:report_id])
#area = #report.areas.create
respond_to do |format|
format.html # new.html.erb
end
end
def create
#report = Report.find(params[:report_id])
#area = #report.areas.create(params[:area])
respond_to do |format|
if #area.save
format.html { redirect_to report_area_path(#report, #area), notice: 'Area was successfully created.' }
else
format.html { render action: "new" }
end
end
end
end
app/views/areas/news.html.haml:
%h1 New Area
= render 'form'
app/views/areas/_form.html.haml:
= simple_form_for [#report, #area] do |f|
= f.error_notification
= f.input :name
= f.button :submit
Instead of creating an area you should building it as it's a new action:
def new
#report = Report.find(params[:report_id])
#area = #report.areas.build # build instead of create
respond_to do |format|
format.html # new.html.erb
end
end