对于我认为是一个非常简单的问题,我很难找到一个简单的答案。
我要做的就是使用任何类型的文本字段搜索“主题”,然后将用户重定向到正确的页面以输入有关该主题的一些信息。
因此,如果用户输入“1001”,他将被带到一个表单,该表单将由已为主题“1001”输入的任何数据填充。
所以我想这就是我想要弄清楚的:
假设只有一个变量会被搜索到,@subject.subject_id。
如何存储文本字段中的变量,以便能够将该变量传递到 link_to 字段,然后该字段将使用该主题信息填充整个页面?
我添加了一个新的控制器元素来玩(populate_values):
subject_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
这是我的一些路线代码,请原谅我是菜鸟 - 我仍在弄清楚是什么触发了什么。
路线.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.
这是我的观点,用户将从哪里输入值。我觉得无论我实际上做错了什么,很可能都源于这种形式。此外,此表单位于用户视图中,而不是主题视图中,如果这很重要的话。
意见/用户/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>
任何帮助将不胜感激。
编辑 1。
我目前正在玩弄
<%= link_to "Screening", screening_path, :subject_id => subject.subject_id, :class => 'btn btn-mini' %>
但是它不识别subject.subject_id。“未定义的局部变量或方法主题”。