Вопрос

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".

Это было полезно?

Решение

If I understand you correctly, you would like to have, on your "search" page, a link that would dynamically include, as a parameter, the subject id that the user has just entered.

Things do not work this way. The issue you are facing is the following : the actual links on the search page are there when the page is loaded, i.e. before the user enters anything so they cannot link to anything the user enters. There could of course be ways to do the trick (with javascript for instance) but here, I think you just want to use a form.

You would have, for instance :

  • in your users views : a search page including a form that will capture the subject_id and call an action in your subjects controller, passing it this subject_id

  • this action in your subjects controller could be, for instance, the update action which will respond with the corresponding edit view that probably exists in your subject views if you used the Rails scaffolding feature. It could also be another controller action that you create and that responds with a specific view from your subjects views.

This Stackoverflow question should help you create forms that are submitted to another controller = form_for but to post to a different action .

Just adapt the form to hook it to your subjects controller and place it in your search page in your users views.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top