0

我的应用由四个模型组成。用户>列表>愿望<项目。我在首页上展示了所有项目,并希望能够从各个项目中建立一个愿望。

由于关联,我必须允许用户指定他想要将项目与哪个列表关联。我想通过 ajax 调用和模态来完成。item但是,partial中的实例变量_item.html.erb不能通过 a 传递link_to,它调用app/views/wishes/new.js.erb,它呈现一个表单来指定列表。

我对rails和ruby很陌生,这个问题已经困扰我好几天了!非常感谢任何帮助。

协会:

class User < ActiveRecord::Base
  has_many :lists, dependent: :destroy
  has_many :wishes, through: :lists
  has_many :items, through: :wishes
end

class List < ActiveRecord::Base
  belongs_to :user
  has_many :wishes, dependent: :destroy
  has_many :items, through: :wishes
end

class Wish < ActiveRecord::Base
  belongs_to :list, touch: true
  belongs_to :item
end

class Item < ActiveRecord::Base
  has_many :wishes
  has_many :lists, through: :wishes
end

app/views/static_pages/home.html.erb

<%= render @items %>

应用程序/视图/项目/_item.html.erb

<div class="span4">
    <div class="white-box item">
        <div class="image-container">
            <%= link_to "Wishlistt it", new_wish_path(item_id: item.id), id: "new_wish", class: "btn btn-primary btn-large", remote: true %>
            <a href="<%= item_path(item) %>" class="item-link">
                <em class="mark"></em>
                <%= image_tag item.image_url(:medium).to_s %>
            </a>
        </div>
        <h3><%= item.title %></h3>
        <%= item.wishes.count %> wishes this item

    </div>
</div>

应用程序/视图/愿望/new.js.erb

$('body').append('<%= j render "new_wish" %>');
$('#new_wish').modal('toggle')

应用程序/视图/愿望/_new_wish.html.erb

<div id="new_wish" class="modal hide fade" tabindex="1" role="dialog" aria-labelledby="make_wish" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="wishlistt_it">Wishlistt it</h3>
  </div>
  <div id="linkpreview">
    <div class="modal-body">
      <%= form_for(@item.wishes.build(item_id: @item.id)) do |f| %>
        <%= f.hidden_field :item_id %>
        <%= f.label :list_id %>
        <%= select_tag :list_id, options_for_select(current_user.lists.all.collect{ |u| [u.name, u.id] }) %>
        <%= f.submit "Add wish", class: "btn btn-large btn-primary" %>
      <% end %>
    </div>
    <div class="modal-footer">
      <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
      <%= link_to "Get info", new_item_path, id: "urlb", class: "btn btn-primary", remote: true %>
    </div>
  </div>
</div>

按来自的链接_item.html.erb会引发此错误:

undefined method `wishes&#x27; for nil:NilClass

编辑

wish_controller.rb

class WishesController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]
  before_filter :correct_user,   only: :destroy

  def new
    @wish = Wish.new
  end

  def create
    @item = Item.find_by_id(params[:item_id])
    @wish = Wish.create!(item_id: params[:wish][:item_id], list_id: params[:list_id])
    redirect_to root_url
  end

  def destroy
    @wish.destroy
    redirect_to current_user
  end

  private

    def correct_user
      @wish = current_user.wishes.find_by_id(params[:id])
      redirect_to current_user if @wish.nil?
    end
end
4

1 回答 1

0

首先,path_heplers 是如何工作的。

new_wish_path

将产生路线

/wishes/new

默认情况下,它将寻找wishes#new您实际上没有的方法。

New获取对象表单需要操作,并且create方法将输入的数据保存在数据库中 - 这是默认的 Rails 逻辑。

在这里你没有表格,这意味着你没有通过params[:wish](它是零)。

此外,在创建方法中,您需要使用save方法,它将输入的数据保存到数据库。

总之,在我看来,这段代码距离可以编辑并且可以正常工作的东西真的很远。无论如何,我建议从一开始就按照此处提供的提示重构此代码,这肯定会节省您的时间。

UPD

修复步骤:

1)new在wish_contoller 中添加方法

2)为表单定义实例@item,通过请求中的参数找到它:@item = Item.find_by_id(params[:item_id])

于 2013-06-09T10:05:24.273 回答