Rails 3 - Syntax error - Accessing member of object
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 =>开发者_如何学Python @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.
精彩评论