我undefined method 'title' for nil:NilClass
这里虽然我渲染网页是我的代码:未定义的方法`标题”:NilClass在红宝石
的routes.rb
get 'contact-us' => "pages#contact_form"
post 'contact-us' => "pages#contact_us"
pages_controller.rb
#http://localhost:3000/contact-us
def contact_form
@page = Page.find_by_seo('contact-us')
@inquiry = Inquiry.new
end
def contact_us
@inquiry = Inquiry.new(inquiry_params)
respond_to do |format|
if @inquiry.save
InquiryMailer.welcome_email(@inquiry).deliver
format.html { redirect_to :back, success: 'Your request successfully sent.' }
format.json { render '/contact-us', status: :created, location: @inquiry }
else
format.html { render :contact_form, notice: 'Please fill all the fields.' }
format.json { render json: @inquiry.errors, status: :unprocessable_entity }
end
end
end
contact_form.html.erb
<div class="contact_left">
<ul class="contact-form_address">
<li><%= @page.title %></li>
<li><%= @page.description.html_safe %></li>
</ul>
<div class="map">
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d119743.53374944824!2d85.75041271337584!3d20.300870219682118!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3a1909d2d5170aa5%3A0xfc580e2b68b33fa8!2sBhubaneswar%2C+Odisha!5e0!3m2!1sen!2sin!4v1460021407530" width="100%" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>
</div>
</div>
<div class="contact_right">
#Contact Form here...
</div
请让我知道我是如何得到@page.title
零值?并给我解决方案。
'Page.find_by_seo( '接触我们')'的值不会在DB – Nithin
是存在于我的数据库存在。当我手动输入URL'http:// localhost:3000/contact-us'时,我的数据即将到来。但问题是我提交表单时。 – Chinmay235
@Nithin问题在这里'format.html {render:contact_form,notice:'请填写所有字段。' }'如果我使用'format.html {redirect_to:back,请注意:'请填写所有字段。' }'这工作正常。但是我所有的表单域都是空白的。 – Chinmay235