Rails use helper method to search for id - ruby-on-rails

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 %>

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 %>

Related

Pre select form input based on params

I have a button on an index page that links to a new_assignment_path
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= link_to 'Assign to Class', new_assignment_path, :class => 'btn btn-mini' %></td>
</tr>
<% end %>
And I want it so that when you click on it, it takes you to the new_assignment_path, and takes the dropdown select on that pages form.
<%= simple_form_for(#assignment) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :user_id, collection: User.all.collect, as: :select %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
And have the drop down automatically set to the user.id of whatever user the button was inside of.
<%= link_to 'Assign to Project', new_assignment_path(#assignment, :user_id => user.id), :class => 'btn btn-mini' %>
and
<%= link_to 'Assign to Project', new_assignment_path(:user_id => user.id), :class => 'btn btn-mini' %>
And neither of them worked.
What is the best way of doing this?
With your second option try setting the instance variable in the AssignmentsController.
#user_id = params[:user_id]
Then you need to specify the default in the form
<%= f.input :user_id, collection: User.all.collect, selected: #user_id %>
You don't need as: :select with Simple Form.

Nested attributes has_many passing and collecting ids to build model

I have this form:
<% form_tag add_expt_details_samples_path, :method => :post do %>
<% for sample in #samples %>
<% if sample.study_id == #study.id %>
<% fields_for "samples[]", sample do |form| %>
<fieldset>
<legend>Sample Name: <%= sample.name %></legend>
<p><center><%= form.label :sample_title %>
<%= form.text_field :sample_title, :size => 25 %></center></p>
<table>
<tr>
<td>
<%= form.label :taxon_id %>
<%= form.text_field :taxon_id, :size => 15 %>
</td>
<td>
<%= form.label :scientific_name %>
<%= form.text_field :scientific_name, :size => 20 %>
</td>
<td>
<%= form.label :common_name %>
<%= form.text_field :common_name, :size => 15 %>
</td>
</tr>
</table>
<p>
<center>
<%= form.label :sample_descripition %><br \>
<%= form.text_area :description %>
</center>
</p>
</fieldset>
<% end %>
<% end %>
<% end %>
<p><center><%= submit_tag "Next" %></center></p>
<% end %>
I would like to collect the samples[] in add_expt_details action so that I could render a post method view for adding new model records for the samples[] ids.
my action is as follows
def add_expt_details
# Collect the samples that were sent
#expt_samples = Sample.find(params[:samples])
# #expt_samples.each do |samp|
# samp.expts.build
# end
end
and then send them to a create action where the sample details are updated and the expt values can be created.
def create_study
#samples = Sample.update(params[:samples].keys, params[:samples].values).reject { |p| p.errors.empty? }
if #samples.empty?
flash[:notice] = "Samples updated"
redirect_to :action => "add_expt_details", :anchor => "ok"
else
render :action => 'edit_individual'
end
end
is there a way to achieve this, I have used accepted_nested_attributes_for :expts, :dependent => :destroy. But I seem to be wrong some where and I do not want to have a form with many fields.
The error message I get is "Unknown key(s): 3, 6, 12"
All suggestions are appreciated.

Find ID does not find the id and create

I have a 2 views that I want to use before the create action.
1st view is add_sample_details.html.erb
<% form_tag add_expt_details_samples_path :method => :post do %>
<% for sample in #samples %>
<% fields_for "samples[]", sample do |form| %>
<fieldset>
<legend>Sample Name: <%= sample.name %></legend>
<p><center><%= form.label :sample_title %>
<%= form.text_field :sample_title, :size => 25 %></center></p>
<table>
<tr>
<td>
<%= form.label :taxon_id %>
<%= form.text_field :taxon_id, :size => 15 %>
</td>
<td>
<%= form.label :scientific_name %>
<%= form.text_field :scientific_name, :size => 20 %>
</td>
<td>
<%= form.label :common_name %>
<%= form.text_field :common_name, :size => 15 %>
</td>
</tr>
</table>
<p>
<center>
<%= form.label :sample_descripition %><br \>
<%= form.text_area :description %>
</center>
</p>
</fieldset>
<% end %>
<% end %>
<p><center><%= submit_tag "Next" %></center></p>
<% for samp in #samples %>
<%= hidden_field_tag("sample_ids[]", samp.id) %>
<% end %>
<% end %>
in the SamplesController, the add_expt_details looks like this
def add_expt_details
# Collect the samples that were sent
#expt_samples = Sample.find(params[:sample_ids])
end
and the add_expt_details.html.erb looks like this
<% form_for #expt_samples, :url => create_study_samples_path, :html => { :method => :put } do |f| %>
<% #expt_samples.each do |samp|%>
<% f.fields_for :expts do |form| %>
<%= form.label :experiment_title %>
<%= form.text_field :expt_title, :size => 15 %><br \>
<% end %>
<% end %>
<p><center><%= submit_tag "Next" %></center></p>
<% end %>
and my create_study action is like this
def create_study
#study = Study.new(params[:study])
# this method collects all the samples from the add_sra_details view from the hidden_field_tag
if current_user.id?
# Put the current user_id in the study.user_id
#study.user_id = current_user.id
if #study.save # Save the values for the study
# Update the Sample attributes
#samples = Sample.update(params[:samples].keys, params[:samples].values).reject { |p| p.errors.empty? }
# For all those sample_ids selected by the user, put the study ids in the sample_study_id
#samples.each do |samp|
#study.samples << samp
end
# get the ids_sent from the add_expt_details
#expt_samples = Sample.find(params[:id])
# A Flash message stating the details would be prefereable..! (Means you have to do it)
flash[:success] = "Your Study has been Created"
redirect_to add_expt_details_samples_path
else
flash[:error] = "Study Faulty"
render :action => "add_sra_details"
end
end
end
The error message I get when I keep the #expt_samples = Sample.find(params[:id]) is "undefined method `keys' for nil:NilClass"
and If I dont have the #expt_samples, it gives me an error "Couldn't Find id in Sample"
Can some one suggest how can I update set of sample ids from one form and also create new Expt model records that are associated to the sample_ids in another form.
All suggestions are appreciated.
Cheers
You need to refer to Rails' accepts_nested_attributes_for
To see how to link existing objects with forms, you can see examples here:
http://josemota.net/2010/11/rails-3-has_many-through-checkboxes/
rails 3 has_many :through Form with checkboxes
has_many through checkboxes

Model validation errors not appearing for nested model in simple_form

So I have a simple form for entering "brand" and "model" through a single submit button, as below:
<%= simple_form_for #brand, :html => { :class => 'form-horizontal' } do |m| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> /Brand</legend>
<%= m.input :name %>
<%= m.simple_fields_for :models, Model.new do |p| %>
<%= p.input :name %>
<% end %>
<div class="form-actions">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', brands_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
I have name:string in my schema for both brand and model.. and validates_presence_of :name in both models.. The form DOES work for creating the brand and model simultaneously but my error "can't be blank" only shows up for the brand field.
Thanks for any help with this issue.
Realized what I was doing wrong. Needed to ALSO put #model = Model.new inside of the controller and put <%= m.simple_fields_for :models, #model do |p| %>

remember form tag input values

I'm trying to create a filter form that user selects dates and sources. The problem is after clicking submit button, in the new page i see that input values are empty. Is there a way to make form remember its values ? Thanks.
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from %>
<%= text_field_tag :to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>
controller
def index
#from = params[:from] ? params[:from].to_datetime : (Time.now-3.day)
#to = params[:to] ? params[:to].to_datetime : (Time.now)
#sources = params[:sources] ? params[:sources] : 1..6
#products = Product.where(:source_id => #sources, :created_at => #from.beginning_of_day..#to.end_of_day)
end
Can't you use the value and checked options from these tags? Here is an example :
<%= form_tag products_path, :method => 'get' do %>
<%= text_field_tag :from, #from %>
<%= text_field_tag :to, #to %>
<% Source.all.each do |source| %>
<%= check_box_tag "sources[]", source.id, #sources.include?( source.id ) %>
<%= source.name %><br />
<% end %>
<%= submit_tag "Submit", :name => nil %>
<% end %>

Resources