Rails XML undefined method `name' for "preview":String - ruby-on-rails

My XML error:
NoMethodError in Admin/xml#index
Showing C:/Rails/asdw/app/views/admin/xml/index.rhtml where line #1 raised:
undefined method `name' for "preview":String
Extracted source (around line #1):
1: <% update_xml("preview") %>
2:
3:
4: <h2>Preview/publish</h2>
My controller:
def index
#photographer = Photographer.find(:first)
#render :layout => false
end
My XML helper:
module XmlHelper
require 'builder'
def update_xml(photographer, output="preview")
xml = Builder::XmlMarkup.new
xml.photographer(:name => photographer.name) do
for group in photographer.groups
xml.group(:name => group.name) do
for project in group.projects
xml.project(:name => project.name) do
for collection in project.collections
xml.collection(:name => collection.name) do
for image in collection.images
xml.image(image.description, :url => image.image, :id => image.id)
end
end
end
end
end
end
end
end
File.open("#{rails_root}/public/xml/#{output}.xml", "w") do |f|
f.puts ("#{xml}")
end
end
end
UPDATE:
Using <% update_xml(photographer, "preview") %>:
NameError in Admin/xml#index
Showing C:/Rails/asdasd/app/views/admin/xml/index.rhtml where line #1 raised:
undefined local variable or method `photographer' for #<#<Class:0x47eb990>:0x47ea238>
NEW UPDATE Using <% update_xml(#photographer, "preview") %>:
oMethodError in Admin/xml#index
Showing C:/Rails/asdfsadf/app/views/admin/xml/index.rhtml where line #1 raised:
undefined method `groups' for #<Photographer:0x45ca2d8>
Extracted source (around line #1):
1: <% update_xml(#photographer, "preview") %>
2:
3: <h2>Preview/publish</h2>

Could you please try using "<% update_xml(photographer, "preview") %>" instead of <% update_xml("preview") %> in Admin/xml#index, let me know if you get error again!

Make sure in your Photographer model you have a groups association such as:
has_many :groups

Related

Ruby Rails: Upload File

I am trying to follow this tutorial. It has written in previous version of Rails and I am using Rails 4. I am trying to upload file but I am getting following error:
NoMethodError in UploadController#uploadfile
undefined method `[]' for nil:NilClass
Extracted source (around line #3):
class DataFile < ActiveRecord::Base
def self.save(upload)
name = upload['datafile'].original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
Rails.root: C:/Ruby193/mylibrary
Application Trace | Framework Trace | Full Trace
app/models/data_file.rb:3:in `save'
app/controllers/upload_controller.rb:6:in `uploadfile'
Here is data_file.rb
class DataFile < ActiveRecord::Base
def self.save(upload)
name = upload['datafile'].original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
end
end
Here is controller upload_controller.rb
class UploadController < ApplicationController
def index
render :file => 'app\views\upload\uploadfile.html'
end
def uploadfile
post = DataFile.save(params[:upload])
render :text => "File has been uploaded successfully"
end
end
Here is uploadfile.html
<h1>File Upload</h1>
<%= form_tag({:action => 'uploadfile'}, :multipart => true) do %>
<p><label for="upload_file">Select File</label>
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<% end %>
What should I do? Thanks in advance
It looks like params[:upload] isn't what you think it is. You forgot to set the form to be multipart. If fixing that doesn't make it work, start inspecting params to see what you're actually getting.
def uploadfile
puts params.inspect # Add this line to see what's going on
post = DataFile.save(params[:upload])
render :text => "File has been uploaded successfully"
end
Also, it's not a great "answer," but I've had good success using paperclip to handle file uploads. If you just want something that works (rather than learning how to do it yourself), check into that.

Railscast #111 uninitialized constant

I followed the episode 111 on Railscast.
http://railscasts.com/episodes/111-advanced-search-form-revised?view=asciicast
I modifed the model names but the other stuff is basically the same. I only want to show you the code where my error appears:
Controller:
def suche
#search = Search.find(params[:id])
end
Model:
class Search < ActiveRecord::Base
attr_accessible :ausdruck, :keyword, :maximum, :minimum, :rund, :zeitraum
def patients
#patients ||= find_patients
end
private
def find_patients
patients = Patients.order(:vorname)
patients = patients.where("vorname like ?", "%#{keyword}%") if keyword.present?
patients
end
end
And my view:
<%= #search.patients.each do |f| %>
<%= f %>
<% end %>
Somehow i get this error:
NameError in Patients#suche
Showing C:/geburtstag/app/app/views/patients/suche.html.erb where line #1 raised:
uninitialized constant Search::Patients
Extracted source (around line #1):
1: <%= #search.patients.each do |f| %>
PATient model:
class Patient < ActiveRecord::Base
attr_accessible :drucken, :extraanrede, :extratext, :geburtsdatum, :geschlecht,:nachname, :vorname
scope :drucken, where(:drucken => true)
end
Class name in rails are singular, so you want
Patient.order(:vorname)
in your find_patients method.

Ruby on Rails - undefined method `map' for nil:NilClass

I have spent the past few hours trying to figure out what I am doing wrong, but I cannot come to a solution. Simply put, I am trying to populate a select box with data from a table called 'semesters'. (I've seen tons of questions regarding this on SO, but I cannot get them to work with my app).
Here's what I have:
Courses Controller
class CoursesController < ApplicationController
def create
#semesters = Semester.all()
#course = Course.new(params[:course])
# Save the object
if #course.save
flash[:notice] = "Course created."
redirect_to(:action => 'list')
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
end
View
#views/courses/new.html.erb
<%= form_for(:course, :url => {:action => 'create'}) do |f| %>
<%= f.select(:semester, #semesters.map { |s| [ s.name, s.id ] }) %>
<%= submit_tag("Create Course") %>
<% end %>
I was hoping it would output:
<select>
<option id="1">Spring 2013</option>
<option id="2">Fall 2013</option>
</select>
But instead, I am getting the error:
views/courses/new.html.erb where line #32 raised:
undefined method `map' for nil:NilClass
Line #32 corresponds to my form helper select.
Any help on this would be great!
You should set your #semesters variable in controller:
def new
#semesters = Semester.all
end
The error occurs because unset instance variable is evaluated to nil, so you try to call map method on nil object.

RoR - undefined method `merge'

I'm studing Ruby on Rails with the "RoR Bible" by Thimothy Fisher. But one of the examples doesn't work. It's code - http://pastebin.com/gtjLsdt0
The error is: NoMethodError in Contact#new where line #4 raised:
undefined method `merge' for "first_name":String
that's my contact_controller. I'm just retyping example's code, and there weren't any words about merge
class ContactController < ApplicationController
def index
#contacts = Contact.find(:all);
end
def show
end
def new
#contact = Contact.new;
end
def create
end
def update
end
end
What is wrong??
Lol that example is completely wrong!
Instead of writing sth like this:
<%= f.text_field 'contact', 'first_name' %>
You should write
<%= f.text_field :first_name %>
Because by using f.field_type you assign the field to the :contact form which provides the f methods by iteration! Also you can write
<%= f.label :first_name, "description of first_name" %>
Instead of writing it manual!
// I loked up the book you refered it seems to be quit outdated. You may buy "The Rails 3 Way" or sth. that can hold up to the current rails version!

Rails variable loads first time and then is nil!

I am receiving the following error:
ActionView::TemplateError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?) on line #24 of app/views/index/index.html.erb:
21: <% #achievements.each do |achievement| %>
22: <%= achievement.name %>
23: <%= achievement.level %>
24: by <%= achievement.user.username %><br/>
25: <% end %>
The strange thing is that when the index page is loaded the first time then there is no problem whatsoever. When I refresh, I get the error above.
The controller looks like this:
class IndexController < ApplicationController
def index
#achievements = Achievement.find(:all)
end
end
Is it something to do with the caching? Or is it using too much memory? If so, can I load the username in another way perhaps? I'm confused!
Try eager loading the users by adding ":include => :user" in your find:
class IndexController < ApplicationController
def index
#achievements = Achievement.find(:all, :include => :user)
end
end

Resources