开发者

Rails: How to receive non-model form input, convert it to a model attribute, and then save that model

Background:

  1. I have a form_for mapped to a model called List.
  2. List has attributes: name, id, receiver_id, assigner_id.
  3. I want the user( or list assigner) to be able to choose a list receiver.
  4. I want the assigner to input an e-mail, rather than the receiver's id.

Problem:

  1. I am not sure how to use a form to receive an e-mail address, run a "User.find_by_email(xx).id" query using that e-mail a开发者_运维技巧ddress, and then assign the returned id to the List's receiver_id attribute.

Current Code:

lists_conroller.rb

class ListsController < ApplicationController

   before_filter :current_user

   def new
      @list = List.new
   end

   def create
      @list = List.new(params[:list])
      @list.assigner = @current_user
      #@list.receiver = User.find_by_id(:receiver_id)
      @list.save
      redirect_to @list
   end

   def show
      @list = List.find(params[:id])

   end

   def update

@list = List.find(params[:id])
   end

end

lists\new.html.erb

<%= form_for @list do |f| %>

    <%= f.label :name, 'Name'%>
    <%= f.text_field :name %>

    <%= f.label :receiver_id, 'Receiver ID'%>  
    **I want this to be the e-mail input, rather than the integer id.**
    <%= f.text_field :receiver_id %><br />

    <%= f.submit :submit %>

<% end %>


User creates new list, with him as the assigner. In that creation process there must be a receiver too. Did I get this right?

I think the receiver should be selected from a list of possible receivers (maybe a select box? this will depend on the number of possible receivers though, wouldn't want to list 1000+ users in there - if there are many users you could do an ajax search when the user types a few letters)

The assigner then selects a user (with the corresponding id as the value) and everything should be ok.


The answer to my question is "Virtual Attributes..."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜