I've having issues finding a simple answer for what I would think is a really simple question.
All I want to do is search for a 'subject' using a text field of any kind, and then redirect the user to the the correct page to enter some information about that subject.
So if the user entered '1001', he would be taken to a form that would be populated by any data already entered for subject '1001'.
So I guess here's what I'm trying to figure out:
Assume that only one variable will ever be searched for, @subject.subject_id.
How do I store a variable from a text field so that I am able to pass that variable into a link_to field, which would then populate the whole page with that subjects info?
I've added a new controller element to play with(populate_values):
subjects_controller.rb
def update
@subject = Subject.find(params[:id])
respond_to do |format|
if @subject.update_attributes(params[:subject])
format.html { redirect_to @subject, notice: 'Subject was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @subject.errors, status: :unprocessable_entity }
end
end
end
...
def populate_values
@subject_id = params[:subject_id]
redirect_to screening_path(@subject_id)
end
end
Here is some of my route code, please excuse me being a noob - I'm still figuring out what triggers what.
routes.rb
#resources :subjects, only: [:new, :create, :destroy] # Copied th
resources :subjects
...
match '/subjects', to: 'subjects#show'
match '/newsubject', to: 'subjects#new'
match '/subjects', to: 'subjects#index'
match '/showsubject', to: 'subjects#show'
match '/editsubject', to: 'subjects#edit'
match '/screening', to: 'subjects#screening'
#match '/populate_values', to: 'subjects#screening' #Just trying to figure out things.
And here is my view from where the value will be entered by the user. I feel that whatever I'm actually doing wrong most likely stems from this form. Also, this form is located within the user views rather than the subject views, if that matters.
views/users/show.html.erb
<% provide(:title, @user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
<hr>
</section>
<div class="user_profile">
<h2>Data Entry</h2>
<body>
<p><%= link_to "Add a new Patient", newsubject_path %></p>
<p><%= link_to "View Patients", subjects_path %></p>
<%= form_tag('/subjects_controller/populate_values') do %>
<%= text_field_tag 'subject_id' %>
<div class="btn btn-link">
<%= submit_tag "Screening Log" %>
</div>
<% end %>
</body>
</div>
</aside>
</div>
Any help would be greatly appreciated.
Edit 1.
I'm currently messing around with
<%= link_to "Screening", screening_path, :subject_id => subject.subject_id, :class => 'btn btn-mini' %>
however it doesn't recognize subject.subject_id. "Undefined local variable or method subject".