Em Sta
Em Sta

Reputation: 1706

Rails use helper method to search for id

Ok i have an simple input, where the user can typ in an user name, and the helper method finduserid should then search for the right user id:

<%= f.text_field :user_id, :value => finduserid(input), :class => "input-small" %> 

The helper method:

def finduserid(name)
  @user = User.find_by_typ(name)
  return @user.id
end

First my code dont works because input is an "undefined local variable or method". So how can i change this? And is an helper method the right tool for this task? How would you perform it?

<%= form_for User.new do |f| %>
<td> <%= f.text_field :user_id, :value => findebmsid(input), :class => "input-small" %>      </td> 
<td></td> 
<td> <%= f.text_field :extra %></td> 
<td> <%= f.hidden_field :number, :value => "1" %></td>    
<td><%= f.submit "Speichern", :class => 'btn btn-mini btn-success' %></td>
<% end %>

Upvotes: 0

Views: 258

Answers (1)

Rodrigo Zurek
Rodrigo Zurek

Reputation: 4575

i dont think a helper is the correct aproach, you should use the show action on your app something like this:

view:

 <%= form_tag(users_path,:method => :get ) do %>
      <%= text_field_tag :user_name, params[:user_name] %>               
 <%end%>

controller:

def show
  @user=User.find_by_user_name(params[:user_name])
end

show view:

<%= @user.name %>

Upvotes: 1

Related Questions