で私はここで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
nilの値を取得していますか教えてください?それに私に解決策を教えてください。あなたのコメントから
'Page.find_by_seo( '接触-たち')'値がDB – Nithin
はいには存在しませんが、私のDBに存在します。私は手動でURL「http:// localhost:3000/contact-us」を入力していますが、私のデータは来ています。しかし問題は私のフォームを提出している間です。 – Chinmay235
@Nithinの問題はここです 'format.html {render:contact_form、notice: 'すべてのフィールドを入力してください。' } 'もし私が' format.html {redirect_to:back、using: 'すべてのフィールドを記入してください。' } 'これはうまくいきます。しかし、私のすべてのフォームフィールド値は空白になります。 – Chinmay235