Ruby on rails 是否有方法将同一组ID发送到两个不同的操作,如new和show?
所以我有搜索控制器来搜索传感器。如果用户搜索特定属性,他们将获得与搜索属性匹配的传感器列表,并且搜索的传感器数量存储在名为@sensors的变量中 搜索控制器模型Ruby on rails 是否有方法将同一组ID发送到两个不同的操作,如new和show?,ruby-on-rails,ruby,Ruby On Rails,Ruby,所以我有搜索控制器来搜索传感器。如果用户搜索特定属性,他们将获得与搜索属性匹配的传感器列表,并且搜索的传感器数量存储在名为@sensors的变量中 搜索控制器模型 class Search < ApplicationRecord belongs_to :user, optional: true def sensors @sensors ||= find_sensors end def find_sensors sensor
class Search < ApplicationRecord
belongs_to :user, optional: true
def sensors
@sensors ||= find_sensors
end
def find_sensors
sensors = Sensor.where(rcomplete: true, lcomplete: true).order(:name)
# search sensor info
sensors = sensors.where("lower(name) like lower(?)", "%#{keywords}%") if keywords.present?
sensors = sensors.where("concentrations.value >= ?","#{conc_min}")
sensors = sensors.where("concentrations.value <= ?","#{conc_max}")
# search publication info
sensors = sensors.joins(:publication).where("lower(publications.title) LIKE lower(?)","%#{publication}%") if publication.present?
if year_min.present?
sensors = sensors.joins(:publication).where("publications.year >= ?","#{year_min}")
end
if year_max.present?
sensors = sensors.joins(:publication).where("publications.year <= ?","#{year_max}")
end
sensors = sensors.joins(:publication).where("lower(publications.author) LIKE lower(?)","%#{author}%") if author.present?
sensors = sensors.joins(:publication).where("lower(publications.cauthor) LIKE lower(?)","%#{co_author}%") if co_author.present?
sensors = sensors.joins(:publication).where("lower(publications.institution) LIKE lower(?)","%#{institution}%") if institution.present?
sensors = sensors.joins(:publication).where("lower(publications.journal) LIKE lower(?)","%#{journal}%") if journal.present?
sensors = sensors.joins(:publication).where("lower(publications.link) LIKE lower(?)","%#{doi_link}%") if doi_link.present?
sensors.uniq
end
end
在图表索引页面中,控制器中的传感器ID如下所示,这允许视图页面使用传感器ID
图表控制器
class SearchesController < ApplicationController
skip_before_action :authenticate_user!, :only => [:new,:create, :show]
def show
@sensors = @search.sensors
respond_to do |format|
format.html
end
end
class ChartsController < ApplicationController
before_action :set_chart, only: [:show, :edit, :update, :destroy]
def index
@charts = Chart.all
@sensors = Sensor.where(id: params[:sensors_ids])
@sensors.map(&:id)
end
class ChartsController
在图表索引页面中,上面的版本是正确的,但这不是我想要的。在底部,它不起作用,我知道这没有意义,但我想用这个想法
<div class="col-md-12">
<%= link_to 'Function Chart', new_function_chart_path(sensors_ids: @sensors.map(&:id)) %>
<div class="col-md-2"></div>
</div>
不正确的版本
<%= link_to 'Function Chart', new_function_chart_path(sensors_ids: @sensors.map(&:id)), function_chart_path(sensors_ids: @sensors.map(&:id)) %>
我想使用传感器的浓度来绘制图表,但问题是,我需要传感器的数据才能进入功能显示页面,而不是功能新建页面。但是,我无法将ID发送到两个不同的操作,因此有办法吗?重定向该操作如何? 使用,
在视图中:
<%= link_to 'Function Chart', function_chart_path(sensors_ids: @sensors.map(&:id), is_redirect: true) %>
※代码未经测试
<%= link_to 'Function Chart', function_chart_path(sensors_ids: @sensors.map(&:id), is_redirect: true) %>
def index
@charts = Chart.all
@sensors = Sensor.where(id: params[:sensors_ids])
@sensors.map(&:id)
return redirect_to action: :show, sensors_ids: params[:sensors_ids] if params[:is_redirect]
end