为Ruby表单选择了设置选项

时间:2016-07-29 16:51:30

标签: ruby-on-rails ruby forms

我有一个用Ruby构建的表单。基本表格如下:

<%= form_for(@user) do |f| %>
  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control', placeholder: 'Name' %>
  </div>

  <div class="field">
    <%= f.label :email %>
    <%= f.text_area :email, class: 'form-control', placeholder: "E-mail" %>
  </div>

  <%= fields_for :user_category do |user_category| %>
    <%= user_category.label 'Category' %>
    <% categories = Array.new(0) %>
    <% category_table = Category.all %>
    <% category_table.each do |i| %>
      <% categories.push([i.category, i.id]) %>
    <% end %>
    <%= user_category.select(:categories_id, categories, {}, {class: 'form-control'}) %>
    <%= user_category.hidden_field :user_id, value: @user.id %>
  <% end %>
  <div class="actions">
    <%= link_to 'Back', user_path, class: 'btn btn-default' %>
    <%= f.submit class: 'btn btn-default' %>
  </div>
<% end %>

表/模型的结构如下:

User - has columns id, name, email
UserCategory - has columns id, user_id, categories_id
Category - has columns id, name

当我编辑用户时,类别的下拉列表从不抓取用户的类别,因此默认值始终是第一个类别。当它在另一个表中时,如何将它链接到正确的categories_id?

换句话说,HTML看起来像:

<select>
  <option value="1">admin</option>
  <option value="2" selected>customer</option>
  <option value="3">supplier</option>
<select>

控制器:

class UsersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  before_action :set_parameters, only: [:new, :create, :edit, :update]

  require 'erb'
  include ERB::Util
  require 'open-uri'

  def index
    @users = User.all
  end

  def show
  end

  def new
    @user = User.new
  end

  def edit
  end

  def create
    @user = User.new(user_params)
    respond_to do |format|
      if @user.save
        UserCategory.create(categories_id: params[:user_category][:categories_id], users_id: @user.id)
        format.html { redirect_to users_path}
        format.json { render :index, status: :created, location: users_path }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @user.update(user_params)
        UserCategory.find_by_users_id(params[:user_category][:users_id]).destroy
        UserCategory.create(categories_id: params[:user_category][:categories_id], users_id: @user.id)
        format.html { redirect_to users_path }
        format.json { render :index, status: :ok, location: users_path }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @user.destroy
    @user_id = UserCategory.where(users_id: params[:id])[0].id
    @user_category = UserCategory.find(@user_id)
    @user_category.destroy
    respond_to do |format|
      format.html { redirect_to users_path }
      format.json { head :no_content }
    end
  end
end

谢谢!

0 个答案:

没有答案