【发布时间】:2015-12-15 22:12:44
【问题描述】:
我有一个简单的脚手架项目来测试此功能。
它有两个模型,User 模型和Fruit 模型。
我正在尝试实现一个功能,用户可以使用 collection_select 从数据库中选择现有水果,然后将其与他的名字一起列出,还可以根据水果查询用户(例如:列出所有有苹果的用户)
注意:我用水果样本填满了水果表来测试它。不确定这是否是正确的方法,但我让 collection_select 工作。
我的问题
问题有两个方面。一个水果没有保存给特定用户(即使参数具有必要的属性),第二个如果我尝试调用 User.fruits.name 我得到“水果”和 User.fruits 给我一个“#”的意外结果。
我一直在阅读并尝试我的主要项目的解决方案,该项目具有精确的结构,但它似乎不起作用。如果有更高效的版本,我也在寻找替代方案。
这里是sn-ps的代码:
参数哈希
class User < ActiveRecord::Base
has_many :fruits
end
users/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<<td><%= user.fruits%></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
users/_form.html.erb
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :fruits %>
<%= f.collection_select(:fruits, Fruit.all, :id, :name, :include_blank => "Please select") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
users_controller
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@fruit = Fruit.new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
debugger
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :fruit_ids)
end
end
fruit.rb
class Fruit < ActiveRecord::Base
belongs_to :user
end
user.rb
class User < ActiveRecord::Base
has_many :fruits
end
schema.rb
ActiveRecord::Schema.define(version: 20151215214130) do
create_table "fruits", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
老实说,我已经不知所措了。我觉得我错过了一些非常基本和明显的东西。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord associations