我正在尝试将表单参数从自动搜索字段传递到另一个表单,而不是参数,#<Restaurant:0x4708a10>
正在显示。
背景资料:
我有两个型号:餐厅和用户。餐厅有许多用户和用户属于餐馆。餐馆预先填充在数据库中,当用户想要注册帐户时,他们必须首先使用自动完成字段找到餐馆。这样做的url是users / find_restaurant。单击提交后,表单将发布到用户控制器的结果操作,以检查用户是否已存在用户。如果用户已经退出,则该人员将被重定向到邀请操作,否则,他们将被重定向到新操作。当用户进入新操作时,restaurant_id列显示#<Restaurant:0x4708a10>
代码是:
class Restaurant < ActiveRecord::Base
has_many :users
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
class UsersController < ApplicationController
before_filter :allow_user, :except => ['new', 'find_restaurant', 'results']
autocomplete :restaurant, :name, :display_value => :funky_method, :extra_data => [:id, :address, :city, :state, :zip_code]
def find_restaurant
@restaurants = Restaurant.search(params[:search])
@users = @resturants.map(&:users).flatten.compact.uniq
end
def results
@restaurants = Restaurant.search(params[:search])
@users = @restaurants.map(&:users).flatten.compact.uniq
if @users.blank?
redirect_to new_user_path
else
redirect_to restaurants_path(@school)
end
end
def new
@user = User.new
@restaurants = Resturant.search(params[:search])
@user.restaurant_id = @schools
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to(:action => 'index')
else
render('new')
end
end
寻找学校的表格
<%= form_tag results_users_path, :method => 'get' do %>
<p>
<%= autocomplete_field_tag :search, params[:search], autocomplete_restaurant_name_users_path, :value_method => :id %>
<%= submit_tag "Next", :name => nil, :id => "submit" %>
</p>
<% end %>
新表格是
<%= simple_form_for @user, :validate => true do |f| %>
<%= f.association :restaurant %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :title %>
<%= f.input :email %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.input :role %>
<%= f.button :submit %>
<% end %>
感谢任何帮助。