Rails 3 - Syntax error - Accessing member of object - ruby-on-rails

My app is something like Kijiji's email reply system.
For each post, user can choose to reply to the post.
I get this error when I submit.
Undefined method `contact_email' for nil:NilClass
I denoted the line that is causing the error below with **
emailinterests_controller.rb
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
respond_to do |format|
if #emailinterest.save
**Notifier.emailinterest_notification(self, #submission).deliver**
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
"self" refers to the emailinterest object that's being emailed.
#submission should refer to the current object that emailinterest object is interacting with.
notifier.rb
**def emailinterest_notification(emailinterest, submission)**
#emailinterest = emailinterest
#submission = submission
**mail :to => submission.contact_email,**
:from => emailinterest.sender_email,
:subject => 'actuirl.com - RE:' + submission.title
end
FIX
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\Rails\actuirl5\app\views\submissions_form_new_emailinterest.html.erb
<%= form_for(emailinterest) do |f| %>
<%= hidden_field_tag :submission_id, value = #submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
C:\Rails\actuirl5\app\views\submissions\show.html.erb
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new} %>

I didn't see you define #submission at anywhere. so, It is null that is correct error. Check where #submission has been create to solve :-)
Good luck

I think the problem is that you reference to
**Notifier.emailinterest_notification(self, #submission).deliver**
instead of
**Notifier.emailinterest_notification(#emailinterest, #submission).deliver**
"self" would be the controller but I think you want the #emailinterest.

Related

Not saving, no error message

On form submission, it's telling me that it was successfully created but it's not showing any data that was submitted. The database is empty. It's showing "null" values and the same on the actual screen where I should be able to edit the data. Here's a screenshot
Update: I think the problem is that it's making a GET request but I don't know how to fix it. Here's a screen shot of my server doing a get when I clicked the submit
Here's the set up
In the index action of results_controller.rb, I have
def index
#results = Result.all
#blob = Sex.new //==#blob = Sex.new is the one I'm focussing on...
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #results }
end
end
In views/results/index, I have the form
<`%= form_for(#blob) do |f| %>`
<div class="field">
<b>1. solicitor exam was fixed?:</b><br/>
<%= f.label(:solicitorcurve, "it was cooked") %>
<%= f.radio_button(:solicitorcurve, "t") %> </br>
</div>
<div class="field">
<%= f.label(:solicitorcurve, "no it was ok") %>
<%= f.radio_button(:solicitorcurve, "f") %>
</div>
<div class="field">
<%= f.label(:draftingteach, "i give the teaching a grade of _ on a scale of 1 to 6") %>
<%= f.select:draftingteach, 1..6 %> </br>
</div>
In the create action of sexes_controller.rb i have
def create
#sex = Sex.new(params[:blob])
respond_to do |format|
if #sex.save
format.html { redirect_to(#sex, :notice => 'Sex was successfully created.') }
format.xml { render :xml => #sex, :status => :created, :location => #sex }
else
format.html { render :action => "new" }
format.xml { render :xml => #sex.errors, :status => :unprocessable_entity }
end
end
end
In models/sex.rb, there is nothing...
class Sex < ActiveRecord::Base
end
And this is the set up of the database
It looks like the issue is that you're retrieving params[:blob] when you should be looking at params[:sex]. form_for will create fields named after the class of the object. The instance variable name #blob you're using is arbitrary.
...
#sex = Sex.new(params[:sex])
...
This is a good argument for why you probably want to name instance variables for what they are. Less confusion.

Rails 3 - Does ID of an object have to be an integer value?

C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
submission_id actually a member of emailinterest object. submission_id is suppose to contain the ID value of submission object.
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
I keep getting stuck at this line
#submission = Submission.find(params[:submission_id])
with this error.
Couldn't find Submission without an ID
Submission and Emailinterest are both objects.
Emailinterest need some information about the members of the Submission object to be created.
submission_id actually a member of Emailinterest object. submission_id is suppose to contain the ID value of Submission object.
Now I have submission_id as string value. Does it have to be an integer value?
FIX
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\Rails\actuirl5\app\views\submissions_form_new_emailinterest.html.erb
<%= form_for(emailinterest) do |f| %>
<%= hidden_field_tag :submission_id, value = #submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
C:\Rails\actuirl5\app\views\submissions\show.html.erb
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new} %>
This error:
Couldn't find Submission without an ID
is probably caused by
#submission = Submission.find(nil)
and nil is what a Hash will give when you ask it for a key it doesn't have (unless someone has supplied a different default of course but params and its contained Hashes should give you nil). So you don't have a :submission_id inside params[:emailinterest]. Based on your comments, I would guess that you do have params[:submission_id] so try this:
#submission = Submission.find(params[:submission_id])
and if that doesn't work, do a logger.debug params.inspect and look at your logs to see where :submission_id is (if anywhere at all).
It can be a string or anything, as long as it can be converted to an integer.
You can verify this using your rails console:
ruby-1.9.2-p0 > Product.find("1")
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", "1"]]
=> #<Product id: 1, collection_id: nil, name: "test", description: nil, price: 1000, picture_file_name: nil, picture_content_type: nil, picture_file_size: nil, picture_updated_at: nil, created_at: "2011-07-14 10:10:45", updated_at: "2011-07-14 10:10:45">
If you want to know what's really in submission_id, you can log the value by putting this at the beginning of your method:
logger.debug params[:emailinterest][:submission_id]

save two models from on form does not work ruby radrails

I'm a newbie with rad rails. I wanted to write at same time in two table from one form.
I have a table machine (with nom and role as column) and a table ipvfour (with machine_id and ip as column).
So I created in models the relation has-and-belongs-to-many.
But when I'm trying to add a new machine if failed with
unknown attribute: ip
I don't really understand why, can someone help me please ?
machine.controllers:
def create
#machine = Machine.new(params[:machine])
ipvfour = #machine.ip.create(params[:ip])
respond_to do |format|
if #machine.save && ipvfour.save
flash[:notice] = 'Machine was successfully created.'
format.html { redirect_to(#machine) }
format.xml { render :xml => #machine, :status => :created, :location => #machine }
else
format.html { render :action => "new" }
format.xml { render :xml => #machine.errors, :status => :unprocessable_entity }
end
end
end
new.html.erb (machine)
New machine
'form', :locals => { :f_machine => f_machine } %>
_form.html.erb (machine)
<% f_machine.fields_for :ip do |f_ip| %>
<%= render :partial => 'ipvfours/form', :locals => { :f_ip => f_ip } %>
<% end %>
_form.html.erb (ipvfours)
<%= f_ip.label :ip %><br />
<%= f_ip.text_field :ip %>
The page to add a machine is correclty displayed with all fields but it seems that write in db failed due to .... I hope that someone will be able ti help me.
Thanks in advance.
EDIT:
You can edit any model in any controller if you want. There's a magic trick called
accepts_nested_attributes_for (google it!)
Your code should look like:
In your Controller:
def new
# .... your code ....
# create empty machine
#machine = Machine.new
# add one empty ip
#machine.ipvfours.build
# .... your code ....
end
def create
# fill machine and ipvfours directly
#machine = Machine.new(params[:machine])
respond_to do |format|
if #machine.save
flash[:notice] = 'Machine was successfully created.'
format.html { redirect_to(#machine) }
format.xml { render :xml => #machine, :status => :created, :location => #machine }
else
format.html { render :action => "new" }
format.xml { render :xml => #machine.errors, :status => :unprocessable_entity }
end
end
end
In your view:
new.html.erb
<% form_for(#machine) do |f_machine| %>
<%= render :partial => 'form', :locals => { :f_machine => f_machine } %>
<%= f_machine.submit 'Create' %>
<% end %>
<%= link_to 'Back', machines_path %>
_form.html.erb (machine)
<%= f_machine.error_messages %>
<% f_machine.fields_for :ipvfours do |f_ip| %>
<%= render :partial => 'ipvfours/form', :locals => { :f_ip => f_ip } %>
<% end %>
_form.html.erb (ipvfours)
<%= f_ip.label :ip %>
<br />
<%= f_ip.text_field :ip %>
In your Model:
Machine model
class Machine < ActiveRecord::Base
has_many :ipvfours, :dependent => :destroy
accepts_nested_attributes_for :ipvfours
end
best regards
simon

rails: "unknown action" message when action is clearly specified

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 %>

NameError (uninitialized constant *nameofcontroller* error in Rails

I am new to RoR and trying simple CRUD operations without scafolding. I have a model called ccword, with controller named WordsController.
Problem is I am creating simple 'create' and leading me to this error:
NameError in C cwordsController#create
uninitialized constant CCwordsController
This is how my create method in my controller looks like:
def create
#ccword = CCword.new(params[:ccword])
respond_to do |format|
if #ccword.save
flash[:notice] = 'Word was successfully added.'
format.html { redirect_to(#ccword) }
format.xml { render :xml => #ccword, :status => :created, :location => #ccword }
else
flash[:notice] = 'Error.'
format.html { render :action => "show" }
format.xml { render :xml => #ccword.errors, :status => :unprocessable_entity }
end
end
end
end
my view new.html.erb looks like :
<h1>Words new</h1>
<% form_for (#ccword) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :word %><br />
<%= f.text_field :word %>
</p>
<p>
<%= f.label :meaning %><br />
<%= f.text_field :meaning %>
</p>
<p>
<%= f.submit 'create' %>
</p>
<% end %>
When I click create I get above NameError.
In console following log gets displayed:
Processing ApplicationController#create (for 127.0.0.1 at 2009-11-19 09:33:06) [POST]
Parameters: {
"commit"=>"Create",
"authenticity_token"=>"hqf54HkXSaTPGejGSbEPcw3ZpOy1Ayp3gvti29gc7Eg=",
"c_cword"=>{"meaning"=>"2", "word"=>"2"}
}
NameError (uninitialized constant CCwordsController):
Any idea?
The problem is here:
format.html { redirect_to(#ccword) }
try changing it to
format.html { redirect_to word_path(#ccword) }

Resources