小编bkn*_*les的帖子

使用嵌套资源Rails best_in_place gem

有没有人知道使用带有best_in_place gem的嵌套资源是否可能(如果是这样,语法是什么)?

我的routes.rb看起来像这样

resources :users do
  resources :goals 
end
Run Code Online (Sandbox Code Playgroud)

我想编辑:description目标的字段,但我的视图中的代码

<%= best_in_place [@user, @goal], :description %>
Run Code Online (Sandbox Code Playgroud)

给出NoMethodError说

undefined method `description' for #<Array:0x20e0d28> 
Run Code Online (Sandbox Code Playgroud)

运用

<%= best_in_place @goal, :description %>
Run Code Online (Sandbox Code Playgroud)

给我一个未定义的方法错误,因为没有 goal_path

我可以让gem为@user(非嵌套资源)字段工作而不会出现问题.

我正在运行Rails 3.1.1,Ruby 1.9.2,best_in_place 1.0.4

edit-in-place ruby-on-rails-3

15
推荐指数
2
解决办法
4772
查看次数

将局部变量传递给嵌套部分

我有一个页面,大致像这样呈现一个集合:

索引.html.haml

= render partial: 'cars_list', as: :this_car,  collection: @cars
Run Code Online (Sandbox Code Playgroud)

_cars_list.html.haml

编辑: _cars_list 有关于个别汽车的其他信息。

%h3 Look at these Cars!
%ul
  %li Something about this car
  %li car.description
%div
  = render partial: 'calendars/calendar_stuff', locals: {car: this_car}
Run Code Online (Sandbox Code Playgroud)

_calendar_stuff.html.haml

- if car.date == @date
  %div 
    = car.date
Run Code Online (Sandbox Code Playgroud)

_cars_contoller.rb

def index
  @cars = Car.all
  @date = params[:date] ? Date.parse(params[:date]) : Date.today
end
Run Code Online (Sandbox Code Playgroud)

在日历内容部分发生的事情是,this_car它总是汽车收藏中的第一辆车,即相同的日期被一遍又一遍地打印出来。

如果我将逻辑_calendar_stuff移入cars_list部分,则打印结果会按预期更改。

因此,似乎 Railsthis_car每次渲染部分时都没有将本地对象传递到嵌套部分中。

有谁知道为什么?

PS如果我用

@cars.each do |car|
  render 'cars_list', locals: {this_car: car} …
Run Code Online (Sandbox Code Playgroud)

nested ruby-on-rails partial-views

5
推荐指数
1
解决办法
3034
查看次数