I am trying to implement simple form in home#index page using:
<%= render "forms/form"%>
Form look like this:
<%= form_for(#form) do |f| %>
<% if #form.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#form.errors.count, "error") %> prohibited this form from being saved:</h2>
<ul>
<% #form.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :defect %><br />
<%= f.text_field :defect %>
</div>
<div class="field">
<%= f.label :region %><br />
<%= f.text_field :region %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
But when I access that page, I got this error message:
undefined method `model_name' for NilClass:Class
First of all I though it is because my model name is also Form, but then I checked Rails 3 reserved words, but there wasn't "form" !
Thanks in advance!
In home controller, set the instance variable #form in the index action as:-
def index
#form = Form.new
end
Your #form instance variable's value is nil. You should set it in controller.
Related
i'm new to rails and ruby, sorry if it's too noobish to ask but need help cause i'm stuck.
i have simple 'app', using rails forms. I need to change ( . ) into ( : ) whenever user will fill the text_field on form.
can anyone be so kind and help me create method from scratch, to access text_field : appearance and use gsub method on it to change ( . ) into ( : ) ?
here is my view: (model is empty, controller is generated using scaffold, super simple stuff)
<%= form_for(#sample) do |f| %>
<% if #sample.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sample.errors.count, "error") %> prohibited this sample from being saved:</h2>
<ul>
<% #sample.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :url %><br>
<%= f.text_field :url %>
</div>
<div class="field">
<%= f.label :appearance %><br>
<%= f.text_field :appearance %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In your model, try this:
class Sample < ActiveRecord::Base
def appearance=(value)
write_attribute(:appearance, value.gsub(/\./, ':'))
end
end
I have the following view page in my test_app:
<p id="notice"><%= notice %></p>
<p>
<strong>Box count:</strong>
<%= #job.box_count %>
</p>
<p>
<strong>Install date:</strong>
<%= #job.install_date %>
</p>
<%= link_to "Back", :back %>
I have the following ruby code in my jobs_controller show method:
def show
#job = Job.find(params[:job_id])
end
The parameters being passed through to the view are ("customer_id" and "id" for the job) as follows:
{"customer_id"=>"1", "id"=>"23"}
When I load the view for any job, I get the following error:
Couldn't find Job without an ID
I must have an error in my show method, but I am unsure exactly what I am doing wrong.
Any ideas?
It looks as though the issue is that my form is not saving the data to the table, here is my form:
<%= form_for([#customer, #job]) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :box_count %><br>
<%= f.number_field :box_count %>
</div>
<div class="field">
<%= f.label :install_date %><br>
<%= f.text_field :install_date %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any idea why the data is not being saved?
You should be using :id, not :job_id:
def show
#job = Job.find(params[:id])
end
I'm trying to figure out how to change the standard rails scaffolding form so that it works for a nested model.
So here's the deal: checklists have many checks. But when I do the standard scaffolded form:
<%= form_for(#check) do |f| %>
<% if #check.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#check.errors.count, "error") %> prohibited this check from being saved:</h2>
<ul>
<% #check.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="actions">
<%= f.submit %>
</div>
<% end %>
I get this error (accessing /checklists/1/checks/new):
undefined method `checks_path' for #<#<Class:0x00000101795070>:0x000001017913a8>
Extracted source (around line #1):
1: <%= form_for(#check) do |f| %>
2: <% if #check.errors.any? %>
3: <div id="error_explanation">
What should I change in this form?
Railscasts' Nested Model Form would be a good tutorial for you.
When I click on this link in my index view:
<%= link_to "Edit Password", edit_user_path(current_user) %>
I get this error:
NoMethodError in Users#edit
Showing /rubyprograms/dreamstill/app/views/videos/_modal.html.erb where line #3 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <div id="boxes">
2: <div id="dialog" class="window">
3: <%= form_for(#video) do |f| %>
This has to do with a partial called _modal that I render into the indexview. It has a form in it.
I also have this in my Videos controller:
def index
#video = Video.new
#videos = Video.paginate(:page => params[:page], :per_page => 20)
end
Why am I getting this error, and how can I fix it?
UPDATE:
Here's my edit action in the Users controller:
def edit
#user = current_user
end
Here's the _modal partial:
<div id="boxes">
<div id="dialog" class="window">
<%= form_for(#video) do |f| %>
<% if #video.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% #video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :video_url %><br />
<%= f.text_field :video_url %>
</div>
<div class="field">
<%= f.label :title, 'Song Title' %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Cancel', '#', :class => 'close' %>
</div>
<div id="mask"></div>
</div>
I believe you are basically having the same problem as last time.
Since you are effectively rendering the edit action—whether it is in a modal or not—you need #video defined again.
I have been getting this error and have been unable to fix it
(the post at StackOverflow Undefined Method Index Path didn't fix my problem).
This exact error message I am getting is (I am using Rails 3.0.5 and Ruby 1.9.2):
NoMethodError in Students#new
Showing C:/rails/ww/app/views/students/_form.html.erb where line #1 raised:
undefined method `students_index_path' for #<#:0x4991c10>
Here are the files:
students_controller.rb
class StudentsController < ApplicationController
def new
#students = Students.new
end
end
new.html.erb
<h1>Enroll New Student</h1>
<%= render 'form' %>
<%= link_to 'Back', students_path %>
_form.html.erb
<%= form_for(#students) do |f| %>
<% if #students .errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#students .errors.count, "error") %> prohibited this course from being saved:</h2>
<ul>
<% #students .errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :pen %><br />
<%= f.text_field :pen %>
</div>
<div class="field">
<%= f.label :fName %><br />
<%= f.text_field :fName %>
</div>
<div class="field">
<%= f.label :lName %><br />
<%= f.text_field :lName %>
</div>
<div class="field">
<%= f.label :pass %><br />
<%= f.text_field :pass %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Thanks for the answer in advanced.
It should be <%= form_for(#student) do |f| %> not #students
Why your model name is plural ? It should be singular.
Just rename or drop/create your model with name Student.
Add resources :students to config/routes.rb
In controller, #student = Student.new
Thats it.. should work with this...
This is because you are passing in the variable #students into form_for, so rails interprets that as students_index_path. If you were to pass in a variable named #student, you'd fine. (assuming you created a variable #student = Student.new)
I'd read up on how form_for interprets it's arguments here.
Just add string in routes.rb
resources :students
instead
match "/students/:id" => "students#new"
or whatever you have (: