【问题标题】:redirect to index instead of show after create or save data创建或保存数据后重定向到索引而不是显示
【发布时间】:2016-07-05 13:13:35
【问题描述】:

我想在输入(创建/新建/更新)数据后直接进入索引视图。输入数据后立即显示显示视图。我试图将渲染:显示更改为渲染:索引,但这不起作用。

这是我的控制器:

class ElectricityGenerationsController < ApplicationController
  before_action :logged_in_user
  before_action :set_electricity_generation, only: [:show, :edit, :update, :destroy]`

  # GET /electricity_generations
  # GET /electricity_generations.json
  def index
    @scenario_selection = Selection.find_by_resource("scenario").name
    @selected_scenarios = Selection.find_by_resource("scenario").scenario_id
    @electricity_generations = ElectricityGeneration.where(scenario_id: @selected_scenarios)
    respond_to do |format|
      format.html #index.html.erb
      format.json {render json: @electricity_generations}
    end
  end

  # GET /electricity_generations/1
  # GET /electricity_generations/1.json
  def show
    @scenario_selections = Selection.find_by_resource("scenario").name
    @electricity_generation = ElectricityGeneration.find(params[:id])
    respond_to do |format|
      format.html #show.html.erb
      format.json {render json: @electricity_generations}
    end
  end

  # GET /electricity_generations/new
  def new
    @scenario_selections = Selection.find_by_resource("scenario").name
    @electricity_generation = ElectricityGeneration.new
    respond_to do |format|
      format.html #new.html.erb
      format.json {render json: @electricity_generations}
    end
  end

  # GET /electricity_generations/1/edit
  def edit
    @scenario_selections = Selection.find_by_resource("scenario").name
    @electricity_generation = ElectricityGeneration.find(params[:id])
  end

  # POST /electricity_generations
  # POST /electricity_generations.json
  def create
    @selected_scenarios = Selection.find_by_resource("scenario").scenario_id
    @electricity_generation = ElectricityGeneration.new(electricity_generation_params)
    @electricity_generation.id = ElectricityGeneration.last.id + 1
    @electricity_generation.scenario_id = Selection.find_by_resource("scenario").scenario_id
    @scenario_selections = Selection.find_by_resource("scenario").name
    @electricity_generation.user_id = current_user.id
    respond_to do |format|
      if @electricity_generation.save
        format.html { redirect_to @electricity_generation, notice: 'Electricity Generation Information was successfully created.' }
        format.json { render :show, status: :created, location: @electricity_generation }
      else
        format.html { render :new }
        format.json { render json: @electricity_generation.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /electricity_generations/1
  # PATCH/PUT /electricity_generations/1.json
  def update
    @scenario_selections = Selection.find_by_resource("scenario").name
    @electricity_generation.user_id = current_user.id
    respond_to do |format|
      if @electricity_generation.update(electricity_generation_params)
        format.html { redirect_to @electricity_generation, notice: 'Electricity Generation was successfully updated.' }
        format.json { render :show, status: :ok, location: @electricity_generation }
      else
        format.html { render :edit }
        format.json { render json: @electricity_generation.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /electricity_generations/1
  # DELETE /electricity_generations/1.json
  def destroy
    @scenario_selections = Selection.find_by_resource("scenario").name
    @electricity_generation.destroy
    respond_to do |format|
      format.html { redirect_to electricity_generations_url, notice: 'Electricity generation was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_electricity_generation
      @electricity_generation = ElectricityGeneration.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def electricity_generation_params
      params.require(:electricity_generation).permit(:user_id, :annual_solar_irradiation, :asi_max, :asi_min, :scenario_id)
    end
end

【问题讨论】:

  • 你好像用了脚手架,总是弄得一团糟。如果你想回到使用redirect_toelectricity_generators_path的索引
  • 是的,我使用了脚手架。我应该用“redirect_toelectricity_generators_path”替换“render :show”吗?
  • 你应该是的......虽然我自己避免使用脚手架,因为多余的代码会妨碍你。
  • 谢谢,我将format.html { redirect_to @electricity_generation, notice: 'Electricity Generation was successfully updated.' } 更改为format.html { redirect_to @electricity_generations_path, notice: 'Electricity Generation was successfully updated.' } format.json { render :show, status: :created, location: @electricity_generation } 保持不变。它有效!
  • 那也行不通……你的路径不是实例变量。重定向到电代路径。如果您不确定路径名,请在您的应用根文件夹中输入 rake routes,它会显示所有可用路径以及它们需要工作的内容。

标签: html ruby-on-rails json view


【解决方案1】:

只重定向到根路径,像这样;

    format.html { redirect_to root_path, notice: 'My Notice.' }

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    只需使用redirect_to %url_helper_to_your_index% 而不是render :show

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 2015-04-16
      • 2014-04-16
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多