我陷入了非常奇怪的问题。我有2个模型User
和Supplier
。用户可以添加许多供应商,所以这里是我的模型的样子。
这是我的用户模型
class User < ActiveRecord::Base
.
.
.
has_many :suppliers, dependent: :destroy
def Supplier_feed
Supplier.where("user_id = ?", id)
end
end
我的供应商模型就像
class Supplier < ActiveRecord::Base
set_primary_key :id
belongs_to :user
default_scope -> { order('created_at DESC') }
validates :supplier, presence: true, length: { maximum: 140 }
validates :user_id, presence: true
end
我的控制器看起来像
class SuppliersController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
def index
@user=current_user
@supplier = current_user.suppliers.paginate(page: params[:page])
end
def new
@supplier=Supplier.new
end
def create
@supplier = current_user.suppliers.build(supplier_params)
if @supplier.save
flash[:success] = "Supplier created!"
redirect_to root_url
else
@supplier_feed_items = []
render 'users/home'
end
end
def destroy
end
private
def supplier_params
params.require(:suppliers).permit(:supplier)
end
end
当我尝试添加某个供应商时,我收到了错误
TypeError in SuppliersController#create
can't convert Symbol into String
以下是我的表单的样子。
<%= form_for :suppliers, :url => {:action => "create"} do |f| %>
<div class="field">
<%= f.text_area :supplier, placeholder: "Add new Supplier..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
因为我是RoR的新手,我无法弄清楚我做了什么错。 请指导我正确的方向。提前致谢
修改 我的应用程序跟踪就像
app/controllers/suppliers_controller.rb:32:in `supplier_params'
app/controllers/suppliers_controller.rb:16:in `create'
This error occurred while loading the following files:
supplier
我的请求参数是
{"utf8"=>"✓",
"authenticity_token"=>"1op5Kkh6EXO+wsR9TmAY1SW3ZIUNYcrodbJ9lfpJjck=",
"suppliers"=>{"supplier"=>"abc"},
"commit"=>"Post"}
这是我的添加供应商表格。
<%= form_for :suppliers, :url => {:action => "create"} do |f| %>
<div class="field">
<%= f.text_area :supplier, placeholder: "Add new Supplier..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
我正在使用偏见。所以这是我的观点供应商
<div class="span8">
<% if @user.suppliers.any? %>
<h3>Suppliers (<%= @user.suppliers.count %>)</h3>
<ol class="supplierss">
<%= render @supplier %>
</ol>
<%= will_paginate @supplier %>
<% end %>
</div>
这是我的_Suppliers.html.erb
<li>
<span class="content"><%= supplier.supplier %></span>
</span>
</li>
答案 0 :(得分:1)
这是视图中的拼写错误
您已使用
def new
@supplier=Supplier.new
end
在视图中你已经使用了
<%= form_for :suppliers, :url => {:action => "create"} do |f| %>
视图:suppliers
中的必须supplier
从s
删除suppliers
请使用
def new
@supplier=Supplier.new
end
<%= form_for :supplier, :url => {:action => "create"} do |f| %>
<div class="field">
<%= f.text_area :supplier, placeholder: "Add new Supplier..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
答案 1 :(得分:0)
您可以创建一个这样的表单
<%= form_for @supplier do |f| %>
并在控制器中
def create
@supplier = current_user.suppliers.build(params[:supplier])