我正在尝试运行此代码并返回给我
nil的未定义方法`image':NilClass
但语法和代码似乎很好。
索引:
- @recipes.each_slice(4) do |recipes|
.row
- recipes.each do |recipe|
.col-md-3
%h4.modal-title= recipe.title
.modal-body
= render :partial =>'show', :locals => {:recipe => @recipe}
_show:
.main_content
#recipe_top.row
.col-md-4
= image_tag @recipe.image.url(:medium), class:"recipe_image"
答案 0 :(得分:2)
将您的代码更改为:
<div class="row text-center">
由于您没有实例变量= render :partial =>'show', :locals => { :recipe => recipe }
,但在行@recipe
中定义了本地变量recipe
。
答案 1 :(得分:1)
试试这个, 只需在传入区域设置时替换实例变量@recipe,因为您已经对其进行了处理并获取了本地变量配方,因此请在您的语言环境中传递它,然后在您的显示页面中调用此方法。
function [J, grad] = costFunctionReg(theta, X, y, lambda)
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
n = length(theta); %number of parameters (features)
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
% ----------------------1. Compute the cost-------------------
%hypothesis
h = sigmoid(X * theta);
for i = 1 : m
% The cost for the ith term before regularization
J = J - ( y(i) * log(h(i)) ) - ( (1 - y(i)) * log(1 - h(i)) );
% Adding regularization term
for j = 2 : n
J = J + (lambda / (2*m) ) * ( theta(j) )^2;
end
end
J = J/m;
% ----------------------2. Compute the gradients-------------------
%not regularizing theta[0] i.e. theta(1) in matlab
j = 1;
for i = 1 : m
grad(j) = grad(j) + ( h(i) - y(i) ) * X(i,j);
end
for j = 2 : n
for i = 1 : m
grad(j) = grad(j) + ( h(i) - y(i) ) * X(i,j) + lambda * theta(j);
end
end
grad = (1/m) * grad;
% =============================================================
end
答案 2 :(得分:1)
您在代码中有错误的含义:
而不是:recipe => @recipe
使用:recipe => recipe
,然后在show partial中使用recipe
代替@recipe
或
索引中的定义@recipe = recipe
并在show中使用@recipe
。