Rails - How to embed a wistia video dynamically through a form - ruby-on-rails

I am making an e-learning platform and have a Course resource so I can add courses dynamically. I am now at the stage where I need to upload videos to each course, and I have decided to use wistia for this.
What I'm not sure about exactly is how can I Embed the videos that belong to each course directly through the Course form? I couldn't find documentation as to how to achieve this, so any help would be highly appreciated.
Example:-
I have this embed code from wistia:
<iframe src="//fast.wistia.net/embed/iframe/tk6picrt4k" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="640" height="388"></iframe>
<script src="//fast.wistia.net/assets/external/E-v1.js" async></script>
Once I make a :video_url table in the database, how do I then make the above code work as intended if I paste it in a form box in the Create Course action and submit the form? and What data type should :video_url be?
Courses Controller:-
class CoursesController < ApplicationController
before_action :set_course, only: [:show, :edit, :update, :destroy]
before_action :set_grade
respond_to :html
def index
#grades = Grade.all
#courses = Course.where grade: params[:grade_id]
respond_with(#course)
end
def show
respond_with(#course)
end
def new
#grade = Grade.find(params[:grade_id])
#course = #grade.courses.new
respond_with(#course)
end
def edit
end
def create
#grade = Grade.find(params[:grade_id])
#course = #grade.courses.build(course_params)
if #course.save
redirect_to grade_courses_path(#grade, #courses), notice: 'Successfully Created Course'
else
render action: 'new'
end
end
def update
#course.update(course_params)
redirect_to grade_courses_path(#grade, #courses), :notice => "Successfully Updated Course"
end
def destroy
#course.destroy
redirect_to grade_courses_path(#grade, #courses), :notice => "Successfully Deleted Course"
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
#course = Course.find(params[:id])
end
def set_grade
#grade = Grade.find(params[:grade_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:title, :period, :description, :semester, :grade, :subject, :grade_id, :chapters_from, :chapters_to, :instructor, :price)
end
end
_form.html.erb
<%= bootstrap_form_for [#grade, #course] do |f| %>
<% if #course.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#course.errors.count, "error") %> prohibited this course from being saved:</h2>
<ul>
<% #course.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.select :subject, [["Math"], ["Physics"], ["Chemistry"], ["Science"], ["Biology"], ["English"], ["Arabic"], ["Social Studies"]], { label: "Subject"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_field :instructor %>
</div>
<div class="field">
<%= f.select :period, [["First"], ["Second"], ["Third"], ["Final"]], { label: "Period"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.select :semester, [["First"], ["Second"]], { label: "Semester"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.number_field :chapters_from %>
</div>
<div class="field">
<%= f.number_field :chapters_to %>
</div>
<div class="field">
<%= f.number_field :price %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Inside your controller we have to permit the new attribute video_url to be set. Rails will handle updates/creates for it on its own:
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:title, :period, :description, :semester, :grade, :subject, :grade_id, :chapters_from, :chapters_to, :instructor, :price, :video_url)
end
Inside your view where you have the static iframe:
...
<iframe src="#{#course.video_url}" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="640" height="388"></iframe>
...
This will populate your view with the given video_url. Note, there's no exception handling if the video_url is NIL.
Your video_url should be of datatype :string. That's sufficient for a simple URL :)
Considering you already have a form to submit data for your course (To create, edit, update the course with information) you will just have to add another input field for the URL (video_url).
Now that you provided the form, we can just add another input:
<%= bootstrap_form_for [#grade, #course] do |f| %>
<% if #course.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#course.errors.count, "error") %> prohibited this course from being saved:</h2>
<ul>
<% #course.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.select :subject, [["Math"], ["Physics"], ["Chemistry"], ["Science"], ["Biology"], ["English"], ["Arabic"], ["Social Studies"]], { label: "Subject"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_field :instructor %>
</div>
<div class="field">
<%= f.select :period, [["First"], ["Second"], ["Third"], ["Final"]], { label: "Period"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.select :semester, [["First"], ["Second"]], { label: "Semester"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.number_field :chapters_from %>
</div>
<div class="field">
<%= f.number_field :chapters_to %>
</div>
<div class="field">
<%= f.number_field :price %>
</div>
<div class="field">
<%= f.text_field :video_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
That should be it!
If you already have a form as well as an existing controller, hit us up with more code and I will gladly edit my answer to give you more specific input.
So far, this should be it and enough for you to work with :)
--- If you still need something else, let us know.

Related

Rails - create and update action don't work

I'm a beginner in Rails, I have a Suplement controller and I can't create or edit a suplement (delete works fine). I'm not getting any errors, just nothing happens when I click and everything's working fine from the console. I tried everything I know (which is not much) and I couldn't find a question like this, similar answers didn't help. I'd appreciate any help, thanks!
class SuplementsController < ApplicationController
before_action :set_suplement, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
#suplement = Suplement.all.order("created_at DESC")
end
def new
#suplement = Suplement.new
end
def create
#suplement = Suplement.new(suplement_params)
if #suplement.save
redirect_to '/suplements'
else
render '/suplements/new'
end
end
def show
end
def edit
end
def update
if #suplement.update(suplement_params)
redirect_to '/suplements'
else
redirect_to '/suplements/new'
end
end
def destroy
#suplement.destroy
redirect_to '/suplements'
end
private
def set_suplement
#suplement = Suplement.find(params[:id])
end
def suplement_params
params.require(:suplement).permit(:name,
:number_of_units,
:daily_dosage_in_units,
:number_of_days,
:suplement_cost
)
end
end
Here's a view:
<h1>Create new suplement</h1>
<%= form_for(#suplement) do |f| %>
<%= render 'form', suplement: #suplement %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and here's a form partial:
<%= form_for(#suplement) do |f| %>
<% if #suplement.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#suplement.errors.count, "error") %> prohibited this suplement from being saved:</h2>
<ul>
<% #suplement.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :number_of_units %>
<%= f.text_field :number_of_units %>
</div>
<div class="field">
<%= f.label :daily_dosage_in_units %>
<%= f.text_area :daily_dosage_in_units %>
</div>
<div class="field">
<%= f.label :number_of_days %>
<%= f.text_area :number_of_days %>
</div>
<div class="field">
<%= f.label :suplement_cost %>
<%= f.text_area :suplement_cost %>
</div>
<% end %>
Also my models:
class Suplement < ApplicationRecord
belongs_to :user
validates :name,
:number_of_units,
:daily_dosage_in_units,
:number_of_days,
:suplement_cost,
presence: true
end
and
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :suplements
end
It looks like the problem is that you have 2 forms.
Uou have a form_for #suplement in your _form.html.erb file and also in your new.html.erb file. Try removing it from new.html.erb so your file looks like this
new.html.erb
<h1>Create new suplement</h1>
<%= render 'form', suplement: #suplement %>
_form.html.erb
<%= form_for(#suplement) do |f| %>
<% if #suplement.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#suplement.errors.count, "error") %> prohibited this suplement from being saved:</h2>
<ul>
<% #suplement.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :number_of_units %>
<%= f.text_field :number_of_units %>
</div>
<div class="field">
<%= f.label :daily_dosage_in_units %>
<%= f.text_area :daily_dosage_in_units %>
</div>
<div class="field">
<%= f.label :number_of_days %>
<%= f.text_area :number_of_days %>
</div>
<div class="field">
<%= f.label :suplement_cost %>
<%= f.text_area :suplement_cost %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
What I did is:
1) Deleted form_for and submit button inside new.html.erb
2) Added submit button in _form.html.erb, so the variable f is accessible
Also, since you are passing a variable #suplement to partial local variable suplement, you can use the variable suplement inside _form.html.erb file without the # sign
EDIT (Regarding comment):
Your getting User presence validation Error, because from Rails 5.0, belongs_to associations are automatically validated for presence.
If you do not need a user in your suplement object all the time then you should change your association to belongs_to :user, optional: true
OR
if you do need the user, and you always want it to be the current user logged in, then add this to your _form
<%=f.hidden_field :user_id, current_user.id %>
This will use Devise helper method to get the current logged in user and assign it to this hidden field. Don't forget to add this parameter in your controler suplement_params controller method
In the #edit of your controller, you need to set the value of the #suplement variable.
def edit
#suplement = Suplement.find(params[:id])
end
you should also include the above line as the first line in your #update method
def update
#suplement = Suplement.find(params[:id])
if #suplement.update_attributes(suplement_params)
# continued...
end

How does my show page need to be written in order to display data?

I am a newbie to the world of Ruby on Rails and I am developing a form that is dropdown in nature for the first time and you can see the code here in _form.html.erb:
<%= simple_form_for(#job, html: {class: 'form-horizontal'}) do |f| %>
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Category:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Landscaping','Landscaping'],
['Babysitting','Babysitting'],['Tree planting','Tree planting']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Location:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Dorchester','Dorchester'],
['Roxbury','Roxbury'],['Mattapan','Mattapan']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Status:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['New','New'],
['Pending','Pending'],['Complete','Complete']])) %>
</div>
</div>
<%= f.input :description, label: "Job Description" %>
<%= f.submit 'Add Job', class: 'btn btn-default' %>
<% end %>
When I click on 'Add Job', it takes me to a blank screen which I have not been able to resolve on my own.
I have changed the code around a view times in the views/show.html.erb. I have had this:
<h1><%= #job.poster %></h1>
<p><%= #job.category %></p>
<p><%= #job.location %></p>
and this:
<tbody>
<% #job.each do |job| %>
<tr>
<td><%= job.poster %></td>
</tr>
<% end %>
</tbody>
and I continue to get a blank screen or if I went to my jobs_controller.rb and developed it like this:
class JobsController < ApplicationController
before_action :find_job, only: [:show, :edit, :update, :destroy]
def index
# #job = Job.all
end
def show
#job = Job.find_job(jobs_params[:id])
end
def new
#job = Job.new
end
def create
#job = Job.new(jobs_params)
if #job.save
redirect_to #job, notice: 'Your job was successfully added'
else
render "New"
end
end
def edit
end
def update
end
def destroy
end
private
def jobs_params
params.require(:job).permit(:poster, :category, :location, :status, :description)
end
def find_job
#job = Job.find(params[:id])
end
end
I would get an undefined method for 'poster' for ActiveRecord object, but if I remove the code in the show action, I just get a blank page. I am out of solutions here and I have looked on SO, but nothing resembled a solution for me.
My routes.rb file looks like this:
Rails.application.routes.draw do
resources :jobs
root 'jobs#index'
end
I changed the show action to reflect this:
def show
#job = Job.find(jobs_params[:id])
end
then I got a param is missing or the value is empty: job.
I did a rails console and I have this:
Job Load (1.1ms) SELECT "jobs".* FROM "jobs" LIMIT ? [["LIMIT", 11]] => #<ActiveRecord::Relation [#<Job id: 1, poster: nil, category: nil, location: nil, status: "New", description: "Your job is to plant trees all over the Dorchester...", created_at: "2017-08-01 10:29:40", updated_at: "2017-08-01 10:29:40">]>
I don't understand why I have nil on everything except description.
It seems that every form input like this:
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
Gave me a nil in the rails console and only this one:
<%= f.input :description, label: "Job Description" %>
successfully inputted data, so something is wrong with the code for f.select, but I am not sure what.
change this
def show
#job = Job.find_job(jobs_params[:id])
end
to this
def show
end
The #job record is retrieved for you by the before_action
And use this in the show.html.erb
<h1><%= #job.poster %></h1>
<p><%= #job.category %></p>
<p><%= #job.location %></p>
Try to Replace your _form.html.erb with
<%= simple_form_for(#job, html: {class: 'form-horizontal'}) do |f| %>
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:poster, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Category:", class: 'control-label' %>
<div class="controls">
<%= f.select(:category, options_for_select([['Landscaping','Landscaping'],
['Babysitting','Babysitting'],['Tree planting','Tree planting']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Location:", class: 'control-label' %>
<div class="controls">
<%= f.select(:location, options_for_select([['Dorchester','Dorchester'],
['Roxbury','Roxbury'],['Mattapan','Mattapan']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Status:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['New','New'],
['Pending','Pending'],['Complete','Complete']])) %>
</div>
</div>
<%= f.input :description, label: "Job Description" %>
<%= f.submit 'Add Job', class: 'btn btn-default' %>
<% end %>
In controller
def show
end
show.html.erb
<h1><%= #job.poster %></h1>
<p><%= #job.category %></p>
<p><%= #job.location %></p>
You got
Job id: 1, poster: nil, category: nil, location: nil, status:
"New", description: "Your job is to plant trees all over the
Dorchester..."
because by mistek you have used :status in every select in _form.html.erb
Remove this line from the show action in your JobsController :
#job = Job.find_job(jobs_params[:id])

Validation errors not showing up - Rails 4

For some reason validation errors are not showing up.
my form
<%= form_for [#question.category, #question] do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field panel">
<%= f.label :question_type %><br>
<%= f.select :question_type, [ ["single","single"],["multiple","multiple"] ], selected: f.object.question_type %>
</div>
<div class="field panel">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field panel">
<%= f.label :image %><br>
<% if #question.image? %>
<div class="explanation-image text-center">
<%= image_tag #question.image_url(:resized) %>
<p>
<label>
<%= f.check_box :remove_image %>
Remove image
</label>
</p>
</div>
<% end %>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %>
</div>
<div class="field panel">
<%= f.label :explanation %><br>
<%= f.text_area :explanation, size: "30x10" %>
</div>
<div class="field panel">
<%= f.label :link_name %><br>
<%= f.text_field :link_name %>
</div>
<div class="field panel">
<%= f.label :link_url %><br>
<%= f.text_area :link %>
</div>
<div class="field panel">
<%= f.label :video_url %><br>
<%= f.text_area :video_url %>
</div>
<div class="field panel">
<%= f.label :category_id %><br><%= #category.title %>
<%= f.hidden_field :category_id %>
</div>
<div class="actions">
<br>
<%= f.submit 'Submit', class:"button round success" %> <%= link_to 'Back', category_questions_path, class: "button round alert" %>
</div>
<% end %>
this is the model
class Question < ActiveRecord::Base
belongs_to :category
has_many :choices
mount_uploader :image, ImageUploader
validates :description, length: {
minimum: 6
}
validates :link, presence: true
end
and parts of the controller
def edit
#category = Category.find(params[:category_id])
#question = #category.questions.find_by(id: params[:id])
end
def update
respond_to do |format|
if #question.update(question_params)
format.html { redirect_to category_question_url(#question.category, #question), notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: #question }
else
format.html {
#category = Category.find(params[:category_id])
#question = #category.questions.find_by(id: params[:id])
render action: :edit
}
format.json { render json: #question.errors, status: :unprocessable_entity }
end
end
end
the error messages code is directly from the scaffolding. i haven't touched it. if i try to edit a question and save it while lets say link field is empty it will reload the edit action correctly but no error message will pop up.
Any clues?
If it fails to save, you redefine the #question variable, before rendering out the page.
#question = #category.questions.find_by(id: params[:id])
this will delete the object which failed to save and was holding the validation errors, and replace it with the one loaded out of the database. Don't do this. I think if you just delete this line it might work ok.

Display images from DB in a form_for RoR

I'm trying to create an update form that would modify ( add or remove ) images related to a product. Each product has many images , each image belongs to a product.
When a user wants to modify a product , the idea is that all the images related to the product displayed and the user decides whether to add more or remove them. My problem is I do not know how to make the images appear. I can only make it show the link.
edit.html.erb
<%- model_class = Product -%>
<div class="page-header">
<h1><%=t '.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<%= render :partial => 'form' %>
_form.html.erb
<%= form_for #product, :html => { :class => "form-horizontal product" } do |f| %>
<% if #product.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% #product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'form-control' %>
</div>
<%= error_span(#product[:name]) %>
</div>
<div class="control-group">
<%= f.label :price, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :price, :class => 'form-control' %>
</div>
<%= error_span(#product[:price]) %>
</div>
<div class="control-group">
<%= f.label :stock, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :stock, :class => 'form-control' %>
</div>
<%= error_span(#product[:stock]) %>
</div>
<div class="control-group">
<%= f.label :tipo, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :tipo, :class => 'form-control' %>
</div>
<%= error_span(#product[:tipo]) %>
</div>
<p> <hr> </p>
<%= f.fields_for :images do |builder| %>
<%= render 'image_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add Question", f, :images %>
<p> <hr> </p>
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
products_path, :class => 'btn btn-default' %>
<% end %>
_image_fields.html.erb
<fieldset>
<%= f.label :link, "Imagen" %><br />
<div class="col-xs-6 col-md-3">
<a href="#" class="thumbnail">
<img src="<%= Here I want to introduce the link %>" alt="...">
</a>
</div>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Imagen" %>
</fieldset>
products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_action :build_image, only: [:edit]
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :price, :stock, :tipo, images_attributes: [ :link ])
end
def build_image
#images = #product.images.build
end
end

Form_for generates resource path ending with '.1'

I have a resource called :school and when I call form_for(#school) it generates the form action:
/school.1
I'm new to all this so any clues as to why it's doing that would be much appreciated. Sleep deprived and heading for a deadline in 3 hours, arrrggg!
Thanks :)
routes.rb:
resource:school
school.rb:
<%= form_for(#school, :url => school_path) do |f| %>
<% if #school.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#school.errors.count, "error") %> prohibited this school from being saved:</h2>
<ul>
<% #school.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address1 %><br />
<%= f.text_field :address1 %>
</div>
<div class="field">
<%= f.label :address2 %><br />
<%= f.text_field :address2 %>
</div>
<div class="field">
<%= f.label :address3 %><br />
<%= f.text_field :address3 %>
</div>
<div class="field">
<%= f.label :town %><br />
<%= f.text_field :town %>
</div>
<div class="field">
<%= f.label :county %><br />
<%= f.text_field :county %>
</div>
<div class="field">
<%= f.label :postcode %><br />
<%= f.text_field :postcode %>
</div>
<div class="field">
<%= f.label :local_authority_id %><br />
<%= f.collection_select :local_authority_id, LocalAuthority.all, :id, :name %>
</div>
<% if current_user.person.primary_user? %>
<div class="field">
<%= f.label 'Are you happy for us to send you regular updates about VRH?' %><br />
<%= f.check_box :allow_contact %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
by using the singular resource method you are telling rails that only one of these objects exist, in your routes file I think you need to change your resource :school to...
resources :schools
If however you do want only one school then I think you will need to add the url option to form_for...
<% form_for(#school, :url => school_path) %>
Proposed solution for follow up questions...
I think what you need will be something like this...
# routes.rb
resources :users do
resources :schools
end
resources :schools
# app/controllers/schools_controller.rb
class SchoolsController < ApplicationController
def new
#user = User.find(params[:user_id])
#school = #user.build_school
end
def create
#user = User.find(params[:user_id])
#school = #user.build_school(params[:school])
if #school.save
# success...
else
# fail...
end
end
def edit
#school = School.find(params[:id])
end
def update
#school = School.find(params[:id])
if #school.update_attributes(params[:school])
# success
else
# fail
end
end
end
# app/views/schools/new.html.erb
<%= form_for([#user, #school]) do |f| %>
<!-- fields go here -->
<% end %>
# app/views/schools/edit.html.erb
<%= form_for([#user, #school]) do |f| %>
<!-- fields go here -->
<% end %>
I just had a similar issue with a singular profile resource in my routes.rb file:
resource :profile
It took me a few hours to find a solution so I decided to share it to save someone else the trouble.
I had to remove the "(.:format)" part of the route (shown when running "rake routes"), by specifying a specific format:
constraints :format => "html" do
resource :profile
end
I also had to add the :url option to form_for tags:
<% form_for(#profile, :url => profile_path) %>
and that worked.

Resources