简体   繁体   English

在Rails中将实例变量从Model传递到View到Controller

[英]Passing instance variable from Model to View to Controller in Rails

Basic app. 基本应用。 Trying to make a button that clicks and shows the next page in this case lesson. 在这种情况下,尝试使按钮单击并显示下一页。

In my controller I have: 在我的控制器中,我有:

def show 
  @lesson = Lesson.find(params[:id])
end 

In my view (show.html.erb) I have: 在我看来(show.html.erb)我有:

...
<p><%= link_to 'Previous', Lesson.previous(@lesson) %></p>
<p><%= link_to 'Next', Lesson.next(@lesson) %></p>
...

In my model I have: 在我的模型中,我有:

def self.next(current_lesson)
  current_lesson.number + 1
end

def self.previous(current_lesson)
  current_lesson.number - 1
end 

My schema includes a number column that is an integer. 我的架构包括一个数字列,它是一个整数。

However, this errors out with 'undefined method `to_model' for 0:Fixnum' and when I run @lesson in the console, it comes up as nil. 但是,使用“未定义的方法'to_model'为0:Fixnum'会出此错误,当我在控制台中运行@lesson时,它显示为nil。

I also tried this: 我也试过这个:

def self.next
  current_lesson = Lesson.find(@lesson.id)
  next_lesson = current_lesson.number + 1 
end 

def self.previous 
  current_lesson = Lesson.find(@lesson.id)
  previous_lesson = current_lesson.number - 1
end

This however, successfully passes the model the instance variable because in the console @lesson returns the correct value but it cannot call the method. 但是,这成功地将实例变量传递给模型,因为在控制台中@lesson返回正确的值,但是它无法调用该方法。

thoughts? 有什么想法吗?

Edit: another solution attempted: 编辑:尝试了另一种解决方案:

I tried changing this to an instance method rather than a class method. 我尝试将其更改为实例方法而不是类方法。 So in the view I set @lesson.previous and @lesson.next. 因此,在视图中,我设置了@ lesson.previous和@ lesson.next。 In the model I did this: 在模型中,我这样做:

def next
  self.number + 1
end 

def previous
  self.number - 1
end

But alas, I get @instance nil error again. 但是可惜,我再次收到@instance nil错误。

self.next & self.previous return integer instead of Lesson. self.next和self.previous返回整数,而不是Lesson。 Make them return next and previous Lesson objects and it should work. 使他们返回下一个和上一个Lesson对象,它应该起作用。 Ie

    Lesson.find_by_number(self.number-1)

In the function 在功能上

def self.next
  current_lesson = Lesson.find(@lesson.id)
  next_lesson = current_lesson.number + 1
end

you will return a Fixnum instead of a Lesson object. 您将返回一个Fixnum而不是一个Lesson对象。

If you want to return a Lesson with an id 1 higher you are probably better off doing something like: 如果您想返回ID为1的课程,则最好执行以下操作:

def next
  Lesson.find(self.number + 1)
end

The reason why you're getting the error is that Lesson.next and Lesson.previous return integers and not lesson objects. 出现错误的原因是Lesson.nextLesson.previous返回整数而不是课程对象。 If you wanted to continue using the next and previous class methods, you could make the following change in your view 如果您想继续使用nextprevious类方法,则可以在视图中进行以下更改

<p><%= link_to 'Previous', lesson_path(Lesson.previous(@lesson)) %></p>
<p><%= link_to 'Next', lesson_path(Lesson.next(@lesson)) %></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM