Can anyone shed light on this for me?
undefined method `first_name' for #
Here is the show.html
<p id="notice"><%= notice %></p>
<div id="container">
<p>
<b>First name:</b>
<%= #artist.firstname %>
</p>
<p>
<b>Second name:</b>
<%= #artist.surname %>
</p>
<p>
<b>About:</b>
<%= #artist.about %>
</p>
<div id="comments">
<h2>Comments</h2>
<%= render :partial => "shared/comment", :collection => #artist.comments%>
</div
</div>
<%= render :partial => "image", :collection => #artist.images %>
<%= link_to 'Edit', edit_artist_path(#artist) %> |
<%= link_to 'Back', artists_path %>
<%= link_to 'show', images_path %>
Here is the partial
<div class="comment">
<p>
<span class="commentator"><%= comment.commentator.display_name %>
say's</span>
<%= comment.comment %>
</p>
</div
Here is the friend view
class Friends < ActiveRecord::Base
attr_accessible :firstname, :surname
has_many :comments, :as => :commentator, :class_name =>"Commentable"
def display_name
"#{self.firstname} #{self.surname}"
end
end
This is the friends controller
class FriendsController < ApplicationController
# GET /friends
# GET /friends.xml
def index
#friends = Friend.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #friends }
end
end
# GET /friends/1
# GET /friends/1.xml
def show
#friend = Friend.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #friend }
end
end
# GET /friends/new
# GET /friends/new.xml
def new
#friend = Friend.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #friend }
end
end
# GET /friends/1/edit
def edit
#friend = Friend.find(params[:id])
end
# POST /friends
# POST /friends.xml
def create
#friend = Friend.new(params[:friend])
respond_to do |format|
if #friend.save
format.html { redirect_to(#friend, :notice => 'Friend was successfully created.') }
format.xml { render :xml => #friend, :status => :created, :location => #friend }
else
format.html { render :action => "new" }
format.xml { render :xml => #friend.errors, :status => :unprocessable_entity }
end
end
end
# PUT /friends/1
# PUT /friends/1.xml
def update
#friend = Friend.find(params[:id])
respond_to do |format|
if #friend.update_attributes(params[:friend])
format.html { redirect_to(#friend, :notice => 'Friend was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #friend.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /friends/1
# DELETE /friends/1.xml
def destroy
#friend = Friend.find(params[:id])
#friend.destroy
respond_to do |format|
format.html { redirect_to(friends_url) }
format.xml { head :ok }
end
end
end
I am trying to make it so a friend can leave a comment on an artists page but I keep getting the above error.
I am very new to Ruby so I apologise if I have left anything out.
Basically, rails will look at the database to figure out what fields are on a model. So make sure your migrations have been run, and that first_name exists on the db table.
Also, Friends is plural. In rails, your table is plural (friends), your model is singular (Friend), and your controller is plural (FriendsController). It is best not to go against this convention. Try renaming the model and see what happens
This error related to database that first_name don't exist in your db.Run migration carefully.
You need line 2 of the Friend class to be attr_accessible :firstname, :surname so your views have access to the those variables.
Related
In my Rails app, I have a controller tickets_controller.rb and model ticket.rb. For creating a ticket I have the following form,
<%= form_for(#ticket) do |f| %>
<% if #ticket.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#ticket.errors.count, "error") %> prohibited this ticket from being saved:</h2>
<ul>
<% #ticket.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :ref_no, "Reference Number"%><br/>
<%= f.text_field :ref_no%><br />
<%= f.label :category, "Type of Request"%><br/>
<%= f.text_field :category_id %><br />
<%= f.label :issue, "Issue"%><br/>
<%= f.text_area :issue%><br />
<%= f.label :ticket_priority, "Priority level"%><br/>
<%= f.text_field :ticket_priority_id %><br />
<%= f.label :ticket_status, "Current Status"%><br/>
<%= f.text_field :ticket_status_id %><br />
<%= f.label :project, "Project"%><br/>
<%= f.text_field :project_id %><br />
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I want to create a unique random reference number on the form_load (ticket/new), and it should be appended to Reference Number text field. While creating a new reference number, it should check the tickets table for duplication. So I have the following model,
ticket.rb
class Ticket < ActiveRecord::Base
attr_accessible :issue, :ticket_status_id, :ticket_priority_id, :ref_no, :category_id, :project_id
has_many :ticket_statuses , :through => :ticket_histories
has_one :ticket_priority
belongs_to :user
before_create :generate_token
protected
def generate_num
self.token = loop do
random_token = random(1000000000)
break random_token unless Ticket.exists?(:ref_no => random_token)
end
end
end
and
tickets_controller.rb
class TicketsController < ApplicationController
before_filter :authenticate_user!
#load_and_authorize_resource
def index
#tickets = Ticket.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #tickets }
end
end
def show
#ticket = Ticket.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #ticket }
end
end
def new
#ticket = Ticket.new
#ref_no = Ticket.generate_num
#categories = Category.all
#status = TicketStatus.first
#priorities = TicketPriority.all
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #ticket }
end
end
def edit
#ticket = Ticket.find(params[:id])
end
def create
#ticket = Ticket.new(params[:ticket])
respond_to do |format|
if #ticket.save
format.html { redirect_to #ticket, :notice => 'Ticket was successfully created.' }
format.json { render :json => #ticket, :status => :created, :location => #ticket }
else
format.html { render :action => "new" }
format.json { render :json => #ticket.errors, :status => :unprocessable_entity }
end
end
end
def update
#ticket = Ticket.find(params[:id])
respond_to do |format|
if #ticket.update_attributes(params[:ticket])
format.html { redirect_to #ticket, :notice => 'Ticket was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #ticket.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#ticket = Ticket.find(params[:id])
#ticket.destroy
respond_to do |format|
format.html { redirect_to tickets_url }
format.json { head :no_content }
end
end
end
When I run my app, I am getting the following error. Can anyone help?
NoMethodError in TicketsController#new
undefined method `generate_num' for #<Class:0x7f5cdc1f21c0>
Rails.root: /home/local/Rajesh/ticket_system
Application Trace | Framework Trace | Full Trace
app/controllers/tickets_controller.rb:27:in `new'
Change your model method generate_num to self.generate_num.
def self.generate_num
token = loop do
random_token = random(1000000000)
break random_token unless Ticket.exists?(:ref_no => random_token)
end
end
You have defined and instance method and you are calling it using Class
Call method using object
#ticket.generate_num
I am using twitter Boostraps tabbable feature found here:
http://twitter.github.com/bootstrap/components.html#navs
And within this navigation content window, I am trying to render a view that displays a "course". This view found in views/courses/_show.html.erb looks like this:
<div class="center hero-unit">
<h1><%= #course.course_name %></h1>
<%= link_to 'New Question Set',new_answer_path(:course_ID => #course.id), :class => "btn btn-large btn-primary" %>
<%= link_to 'Launch Session!', edit_course_path, :class => "btn btn-large btn-primary" %>
</div>
I am trying to render it and failing with the following code in views/instructor/show.html.erb
<% courses.each do |c| %>
<div class="tab-pane" id="<%=c.course_name%>">
<%= render :partial => 'courses/show', :locals => {#course=>c} %>
</div>
I get the following error:
/app/views/courses/_show.html.erb:1: syntax error, unexpected '=',
expecting keyword_end ...tput_buffer = #output_buffer; =
local_assigns[:];show = loca... ... ^
/app/views/courses/_show.html.erb:1: syntax error, unexpected ']',
expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or
tSTRING_END ...tput_buffer; = local_assigns[:];show =
local_assigns[:show];... ...
saying it's failing at line 1 of my courses/_show.html.erb
My Course Controller looks like this:
class CoursesController < ApplicationController
# GET /courses
# GET /courses.json
def index
#courses = Course.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #courses }
end
end
# GET /courses/1
# GET /courses/1.json
def show
#course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #course }
end
end
# GET /courses/new
# GET /courses/new.json
def new
#course = Course.new(instructor_ID: params[:instructor_ID])
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #course }
end
end
# GET /courses/1/edit
def edit
#course = Course.find(params[:id])
end
# POST /courses
# POST /courses.json
def create
#course = Course.new(params[:course])
respond_to do |format|
if #course.save
format.html { redirect_to #course, :notice => 'Course was successfully created.' }
format.json { render :json => #course, :status => :created, :location => #course }
else
format.html { render :action => "new" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
# PUT /courses/1
# PUT /courses/1.json
def update
#course = Course.find(params[:id])
respond_to do |format|
if #course.update_attributes(params[:course])
format.html { redirect_to #course, :notice => 'Course was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #course.errors, :status => :unprocessable_entity }
end
end
end
Note: I have ommited some of the methods like delete in my controller to save space.
Any Ideas?!
In #course=>c change the # to a colon.
I would like to display a category name instead of a number (cat_id) from a belongs to relationship, I have cars and makes, basically here's the code -
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Make:</b>
<%= #car.make_id %>
</p>
<h2>
<em><%= #car.model %> <%= #car.body_typw %> <%= #car.engine_size %> <%= #car.trim %></em>
</h2>
<p>
<%= image_tag #car.image(:large) %>
</p>
<% #carimages.each do |carimage| %>
<%= image_tag carimage.image(:thumb), :class => "imgsmall" %>
<% end %>
<p>
<b>Transmission:</b>
<%= #car.transmission %>
</p>
<p>
<b>Fuel type:</b>
<%= #car.fuel_type %>
</p>
<p>
<b>Millage:</b>
<%= #car.millage %>
</p>
<p>
<b>Price:</b>
<%= number_to_currency(#car.price) %>
</p>
<p>
<%= raw #car.content %>
</p>
So basically i want the Make name here:-
<p>
<b>Make:</b>
<%= #car.make_id %>
</p>
cars_controller.rb
class CarsController < ApplicationController
# GET /cars
# GET /cars.json
def index
#cars = Car.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #cars }
end
end
# GET /cars/1
# GET /cars/1.json
def show
#car = Car.find(params[:id])
#pages = Page.all
#carimages = Carimage.all
#carimages = Carimage.find(:all, :limit => 10, :order => "id DESC")
respond_to do |format|
format.html # show.html.erb
format.json { render json: #car }
end
end
# GET /cars/new
# GET /cars/new.json
def new
#car = Car.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #car }
end
end
# GET /cars/1/edit
def edit
#car = Car.find(params[:id])
end
# POST /cars
# POST /cars.json
def create
#car = Car.new(params[:car])
respond_to do |format|
if #car.save
format.html { redirect_to #car, notice: 'Car was successfully created.' }
format.json { render json: #car, status: :created, location: #car }
else
format.html { render action: "new" }
format.json { render json: #car.errors, status: :unprocessable_entity }
end
end
end
# PUT /cars/1
# PUT /cars/1.json
def update
#car = Car.find(params[:id])
respond_to do |format|
if #car.update_attributes(params[:car])
format.html { redirect_to #car, notice: 'Car was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: #car.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cars/1
# DELETE /cars/1.json
def destroy
#car = Car.find(params[:id])
#car.destroy
respond_to do |format|
format.html { redirect_to cars_url }
format.json { head :ok }
end
end
end
it is related through make table - id and car table - make_id
Thanks
Robbie
Sure - the belongs to relationship gives you an object (a Make in your case), which you can call methods on - including getting field names!
So, If you set up your models like so:
class Car < ActiveRecord::Base
belongs_to :make
end
class Make < ActiveRecord::Base
end
And Make has a field named name, you could in your view:
<p>
<b>Make:</b>
<%= #car.make.name %>
</p>
I had hard time to figure out why I've been getting "unknown action" error message when I was do some editing:
Unknown action
No action responded to 11. Actions: bin, create, destroy, edit, index, new, observe_new, show, tag, update, and vote
you can see that Rails did mention each action in the above list - update. And in my form, I did specify action = "update".
I wonder if some friends could kindly help me with the missing links...
here is the code:
edit.rhtml
<h1>Editing tip</h1>
<% form_tag :action => 'update', :id => #tip do %>
<%= render :partial => 'form' %>
<p>
<%= submit_tag_or_cancel 'Save Changes' %>
</p>
<% end %>
_form.rhtml
<%= error_messages_for :tip %>
<p><label>Title<br/>
<%= text_field :tip, :title %></label></p>
<p><label>Categories<br/>
<%= select_tag('categories[]', options_for_select(Category.find(:all).collect {|c| [c.name, c.id] }, #tip.category_ids), :multiple => true ) %></label></p>
<p><label>Abstract:<br/>
<%= text_field_with_auto_complete :tip, :abstract %></label></p>
<p><label>Name: <br/>
<%= text_field :tip, :name %></label></p>
<p><label>Link: <br/>
<%= text_field :tip, :link %></label></p>
<p><label>Content<br/>
<%= text_area :tip, :content, :rows => 5 %></label></p>
<p><label>Tags <span>(space separated)</span><br/>
<%= text_field_tag 'tags', #tip.tag_list, :size => 40 %></label></p>
class TipsController < ApplicationController
before_filter :authenticate, :except => %w(index show)
# GET /tips
# GET /tips.xml
def index
#tips = Tip.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #tips }
end
end
# GET /tips/1
# GET /tips/1.xml
def show
#tip = Tip.find_by_permalink(params[:permalink])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #tip }
end
end
# GET /tips/new
# GET /tips/new.xml
def new
#tip = session[:tip_draft] || current_user.tips.build
end
def create
#tip = current_user.tips.build(params[:tip])
#tipMail=params[:email]
#if tipMail
# TipMailer.deliver_email_friend(params[:email], params[:name], tip)
# flash[:notice] = 'Your friend has been notified about this tip'
#end
#tip = current_user.tips.build(params[:tip])
#tip.categories << Category.find(params[:categories]) unless params[:categories].blank?
#tip.tag_with(params[:tags]) if params[:tags]
if #tip.save
flash[:notice] = 'Tip was successfully created.'
session[:tip_draft] = nil
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def edit
#tip = Tip.find(params[:id])
end
def update
#tip = Tip.find(params[:id])
respond_to do |format|
if #tip.update_attributes(params[:tip])
flash[:notice] = 'Tip was successfully updated.'
format.html { redirect_to(#tip) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #tip.errors, :status => :unprocessable_entity }
end
end
end
def destroy
#tip = Tip.find(params[:id])
#tip.destroy
respond_to do |format|
format.html { redirect_to(tips_url) }
format.xml { head :ok }
end
end
def observe_new
session[:tip_draft] = current_user.tips.build(params[:tip])
render :nothing => true
end
end
the quick answer is that form_tag doesn't support :action as an option, you want to be passing a string as a path in. A slightly longer answer is you shouldn't be using form_tag anyways for a model edit form, you should be using form_for.
what rails are you using? .rhtml is pretty old, rails generators should be giving you .html.erb files. if it is something even remotely recent, you should be able to use
<% form_for #tip do |f| %>
<%= f.label :title, 'Title' %><br />
<%= f.text_field %>
... etc
<% end %>
I'm trying to get my head around collection_selects in Rails. I can populate the dropdown from a database table, submit the selected option, and show the result. However I can't figure out how to show the selected option in the dropdown when the user chooses to edit the entry.
Here's an extract from my view code:
<p>
<%= f.label :Status %><br />
<%= f.text_field :status %>
</p>
<p>
<%= f.label :Manager %><br />
<%= f.collection_select(:manager, #managers, :id, :name, {:include_blank => true}) %>
</p>
Here's my controller code:
class ProjectsController < ApplicationController
# GET /projects
# GET /projects.xml
before_filter :require_user
def index
#projects = Project.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #projects }
end
end
# GET /projects/1
# GET /projects/1.xml
def show
#project = Project.find(params[:id])
projectid = params[:id]
#evidence = Evidence.find(:all, :conditions => ["projectid = ?", projectid])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #project }
end
end
# GET /projects/new
# GET /projects/new.xml
def new
#project = Project.new
#managers = Manager.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #project }
end
end
# GET /projects/1/edit
def edit
#project = Project.find(params[:id])
#managers = Manager.find(:all)
end
# POST /projects
# POST /projects.xml
def create
#project = Project.new(params[:project])
respond_to do |format|
if #project.save
flash[:notice] = 'Project was successfully created.'
format.html { redirect_to(#project) }
format.xml { render :xml => #project, :status => :created, :location => #project }
else
format.html { render :action => "new" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end
end
# PUT /projects/1
# PUT /projects/1.xml
def update
#project = Project.find(params[:id])
respond_to do |format|
if #project.update_attributes(params[:project])
flash[:notice] = 'Project was successfully updated.'
format.html { redirect_to(#project) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #project.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.xml
def destroy
#project = Project.find(params[:id])
#project.destroy
respond_to do |format|
format.html { redirect_to(projects_url) }
format.xml { head :ok }
end
end
end
And here's the code for my models:
class Manager < ActiveRecord::Base
has_many :projects
end
class Project < ActiveRecord::Base
belongs_to :manager
validates_presence_of(:name, :reference, :client, :status)
validates_uniqueness_of (:reference)
end
Any help is greatly appreciated.
Thanks!
According to the documentation:
collection_select(object, method, collection, value_method, text_method,
options = {}, html_options = {})
...The value returned from calling method
on the instance object will be
selected.
This means that your :manager method must return a value that matches one of the managers in your #managers instance variable in order for that to be the selected option.
I think you need to change :manager to :manager_id:
<%= f.collection_select(:manager_id, #managers, :id, :name, {:include_blank => true}) %>