2016-07-05 17 views
2

データを入力(作成/新規/更新)した直後にインデックスビューに移動したいと考えています。データを入力した直後に、表示ビューが表示されます。レンダリング:show in render:indexを変更しようとしましたが、これは機能しませんでした。データの作成または保存後にshowの代わりにインデックスにリダイレクト

これは私のコントローラです:

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 
+0

あなたはいつも混乱を招く足場を使用したようです。 redirect_to electricity_generators_path – MageeWorld

+0

はい、私は足場を使用しました。私は "render:show"を "redirect_to electricity_generators_path"に置き換えるべきですか? – Frank

+0

あなたはそうしなければいけません...私は骨格を使用することを避けています。 – MageeWorld

答えて

3

ルートパスのみにリダイレクトされます。

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

希望します。

0

ただredirect_to %url_helper_to_your_index%の代わりrender :showを使用しています。

関連する問題