नमस्कार लोगोंमेरे परीक्षण गुजर रहे हैं। लेकिन, अपरिभाषित विधि `की समीक्षा करता है 'शून्य के लिए: NilClass जब पेज
बुला मैं एक तंबाकू समीक्षा आवेदन का निर्माण करने की कोशिश कर रहा हूँ की तुलना में कुछ अधिक सामान जानने के लिए कोई अन्य कारण से। मैं रेल ट्यूटोरियल पुस्तक में जो कुछ सीखा है, उस पर निर्माण करने की कोशिश कर रहा हूं, हालांकि मैं ईंट की दीवारों और गठिया के खिलाफ आ रहा हूं जो मुझे समझ में नहीं आता है। मैं आमतौर पर शून्य के लिए अपरिभाषित विधि 'कुछ' प्राप्त कर सकता हूं: नील क्लास और मेरे पास अन्य समस्याएं हैं, लेकिन यह वास्तव में मुझे स्टंप कर देता है क्योंकि मुझे अपने परीक्षण पास हो गए हैं।
मेरे टेस्ट
review_test.rb
require 'test_helper'
class ReviewTest < ActiveSupport::TestCase
# Define a user for the test
def setup
@user = users(:shaun)
@review = @user.reviews.build(rating: 5,
comment:"This is a super snus",
tobacco_id: 1)
end
# Test should be valid
test "should be valid" do
assert @review.valid?
end
# Test the user id is present
test "user id should be present" do
@review.user_id = nil
assert_not @review.valid?
end
# Test the tobacco id is present
test "tobacco id should be present" do
@review.tobacco_id = nil
assert_not @review.valid?
end
# Rating should be present
test "rating should be present" do
@review.rating = " "
assert_not @review.valid?
end
# Comment should be present
test "comment should be present" do
@review.comment = " "
assert_not @review.valid?
end
# Verify that the first review in the DB is the same as fixture review
test "order should be most recent first" do
assert_equal reviews(:most_recent), Review.first
end
end
user_test.rb
# Test that a review is destroyed when a user is destroyed
test "associated reviews should be destroyed" do
@user.save
@user.reviews.create!(rating: 5,
comment: "I love this suns, it's so good!",
tobacco_id: 1)
assert_difference 'Review.count', -1 do
@user.destroy
end
end
फिक्स्चर
+०१२३५१६४१०६reviews.yml
one:
rating: 5
tobacco_id: 1
comment: "This is a super snus"
created_at: <%= 3.hours.ago %>
two:
rating: 5
tobacco_id: 1
comment: "This is the best snus in the world"
created_at: <%= 2.hours.ago %>
three:
rating: 5
tobacco_id: 1
comment: "I will always buy this snus"
created_at: <%= 1.hours.ago %>
most_recent:
rating: 5
tobacco_id: 1
comment: "I have a shed load of this snus, it's great!"
created_at: <%= Time.zone.now %>
मॉडल
review.rb
belongs_to :tobacco
tobacco.rb
has_many :reviews
user.rb
has_many :tobaccos, dependent: :destroy
has_many :reviews, dependent: :destroy
नियंत्रक reviews_controller.rb
def new
@review = Review.new
end
यह वही मैं नियंत्रक में बनाने कार्रवाई के लिए है, लेकिन मैं नहीं कर सकता बताएं कि मेरे पास यह सही है क्योंकि मैं अभी तक पृष्ठ लोड नहीं कर सकता हूं। अगर मेरे पास यह गलत है, तो मेरे पास शायद एक और सवाल पूछने के लिए निर्माण कार्रवाई पर किसी भी सुझाव की सराहना की जाएगी!
def create
@tobacco = Tobacco.find(params[:tobacco_id])
@review = @tobacco.reviews.new(params[:review].permit(:tobacco_id, :comment))
@review.user = User.find(current_user.id)
@review.save
redirect_to tobacco_path(@tobacco)
end
मार्गों
resources :tobaccos do
resources :reviews, except: [:show, :index]
end
show.html.erb
<%= link_to "Write a Review", new_tobacco_review_path(@tobacco) %>
new.html।ERB
<%= form_for([@tobacco, @tobacco.reviews.build]) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Save Review", class: 'btn btn-primary' %>
<% end %>
जब मैं एक समीक्षा लिखने के पृष्ठ पर आते हैं, मैं त्रुटि
Showing C:/Sites/apps/review/app/views/reviews/new.html.erb where line #11 raised:
undefined method `reviews' for nil:NilClass
यह मुझे यह 11
<%= form_for([@tobacco, @tobacco.reviews.build]) do |f| %>
लाइन लेकिन जो कुछ भी मैं कोशिश मैं बता रही है मिल एक अलग त्रुटि। मुझे पता है कि यह एक गड़बड़ गलती है लेकिन मैं इसे नहीं देख सकता।
अग्रिम धन्यवाद
शॉन
अद्यतन
मैंने यह काम कर रहा है और अब डेटाबेस के लिए बचत, मैं इसे इस reviews_controller.rb में नए और बनाने के देखो पसंद आया मिल गया इस
def new
@tobacco = Tobacco.find(params[:tobacco_id])
end
def create
@tobacco = Tobacco.find(params[:tobacco_id])
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.tobacco_id = @tobacco.id
if @review.save
redirect_to @tobacco
else
render 'new'
end
end
तरह review_params
हैंdef review_params
params.require(:review).permit(:rating, :comment, :tobacco_id)
end
यह सब मेरे लिए सही लग रहा है, और यह काम करता है इसलिए मुझे आशा है कि यह ठीक है।
जो मैं अभी अटक गया हूं वह एक तंबाकू से संबंधित एक समीक्षा संपादित कर रहा है। यह पहली बार है जब मैंने नेस्टेड मार्गों पर काम किया है। मैंने बस कुछ ट्यूटोरियल्स को देखा लेकिन कुछ भी वास्तव में इसे ठीक करने में मेरी मदद नहीं की है। मुझे यकीन है कि कैसे मैं इन कुंजियों पारित कर सकते हैं नहीं कर रहा हूँ ...
शो पृष्ठ पर मैं इस
<%= link_to "Edit", edit_tobacco_review_path(@tobacco, @review) %>
शो विधि tobaccos_controller.rb इस
def show
@tobacco = Tobacco.find(params[:id])
@reviews = Review.where(tobacco_id: @tobacco.id)
end
में की तरह लग रहा है reviews_controller.rb मैं इस
def edit
@tobacco = Tobacco.find(params[:tobacco_id])
@review = Review.find(params[:id])
end
किया है और त्रुटि मैं हो रही है इस
हैActionController::UrlGenerationError in Tobaccos#show
No route matches {:action=>"edit", :controller=>"reviews", :id=>nil, :tobacco_id=>"8"} missing required keys: [:id]
इस पर ध्यान देना मुझे Tobaccos # शो एक्शन में कुछ करना चाहिए, लेकिन मैं नहीं सोच सकता।
मैं लगभग घर और सूखा हूं, क्या आप देख सकते हैं कि मैंने क्या गलत किया है? धन्यवाद :)
अद्यतन संख्या दो show.html.erb यह नहीं पूरे शो पेज है, लेकिन यह महत्वपूर्ण हिस्सा है
<% if @reviews.blank? %>
<h3>No reviews yet! Would you like to be the first?</h3>
<%= link_to "Write Review", new_tobacco_review_path(@tobacco), class: "btn btn-danger" %>
<% else %>
<% @reviews.each do |review| %>
<div class="reviews">
<p><%= review.comment %></p>
<p><%= review.user.name %></p>
<%= link_to "Edit", edit_tobacco_review_path(@tobacco, @review) %>
</div>
<% end %>
<%= link_to "Write Another Review", new_tobacco_review_path(@tobacco), class: "btn btn-danger" %>
<% end %>
अद्यतन संख्या 3 edit.html.erb
<%= form_for([@tobacco, @tobacco.reviews.build]) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Update Review", class: 'btn btn-primary' %>
<% end %>
<%= link_to "Back", tobacco_path(@tobacco),
class: "btn btn-default btn-block margin-top-25" %>
_fields.html।ERB
<%= f.label :rating %>
<%= f.select :rating, [['1', 1],
['2', 2],
['3', 3],
['4', 4],
['5', 5]
] ,class: 'form-control' %>
<%= f.label :comment %>
<%= f.text_field :comment, class: 'form-control' %>
अपने 'new' कार्रवाई में होना चाहिए, आप' @tobacco = Tobacco.find जोड़ने की जरूरत है ([: tobacco_id] पैरामीटर) ' – AbM
धन्यवाद @Abm काम कर thats लेकिन इसकी नहीं डेटाबेस में सहेजना, मैं अपनी निर्माण कार्रवाई की अपनी लाइन 2 अनुमान लगा रहा हूं। क्या आप कृपया मेरे लिए कोई प्रकाश डाल सकते हैं? – Shaun
क्या आपके पास strong_params विधि है? कृपया इसे पोस्ट करें। – Pavan