Rails,编辑链接将重定向但更新按钮不会起作用

时间:2016-04-10 11:09:25

标签: ruby-on-rails

在我的rails应用程序中,我有一个似乎无法正常工作的编辑表单。如果我通过任何链接进入编辑网站,一切都会好起来,但表单提交按钮将无法正常工作。如果我刷新窗口它工作正常。

Edit.html.erb

    <div class="alien-choice">
        <h2 class="player_name"><%= current_user.username %></h2>
        <p> Choose <span>0</span>/2 Power</p>

    <%= form_for [@gameround, @currentplayer] do |f| %>

        <% alleflares = @currentplayer.flares %>

        <% alleflares.each { |val| 
            printAlien = Alien.find_by_title(val)
        %>
        <div class="alien_wrap">
            <h3><%= printAlien.title  %> <span>[<%= printAlien.expansion.abbr   %>]</span></h3>

            <label for="<%= printAlien.title %>">
                <div class="alien" style="background-image: url(<%= printAlien.avatar.url %>)">
            </label>

            <%= f.check_box(:aliens, { :multiple => true, :id => printAlien.title, :class => 'choosealiens'}, printAlien.title, nil) %>

            </div>
        </div>


        <% } %>

        </div>
          <div class="actions">
            <%= f.submit %>
        </div>
    <% end %>

<%= link_to 'Back', gameround_currentplayers_path %>

控制器

class CurrentplayersController < ApplicationController
  before_action :set_currentplayer, only: [:show, :edit, :update]

  # GET /currentplayers
  # GET /currentplayers.json
  def index
    @currentplayers = Currentplayer.all
  end

  # GET /currentplayers/1
  # GET /currentplayers/1.json
  def show
    @gameround = Gameround.find(params[:gameround_id])
    @currentplayer = @gameround.currentplayers.find(params[:id])

    current_user
  end

  # GET /currentplayers/new
  def new
    @gameround = Gameround.find(params[:gameround_id])
    @currentplayer = Currentplayer.new
  end

  # GET /currentplayers/1/edit
  def edit
      @gameround = Gameround.find(params[:gameround_id])
      @currentplayer = @gameround.currentplayers.find(params[:id])

  end

  # POST /currentplayers
  # POST /currentplayers.json
  def create
    @gameround = Gameround.find(params[:gameround_id])

    @currentplayer = @gameround.currentplayers.create(currentplayer_params);

    respond_to do |format|
      if @currentplayer.save
        format.html { redirect_to @gameround, notice: 'Currentplayer was successfully created.' }
        format.json { render :show, status: :created, location: @currentplayer }
      else
        format.html { render :new }
        format.json { render json: @currentplayer.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /currentplayers/1
  # PATCH/PUT /currentplayers/1.json
  def update
      @gameround = Gameround.find(params[:gameround_id])
      @currentplayer = @gameround.currentplayers.find(params[:id])

      if @currentplayer.update(currentplayer_params)
        redirect_to gameround_currentplayer_path

      else
        render 'edit'
      end


  end

  # DELETE /currentplayers/1
  # DELETE /currentplayers/1.json
  def destroy
     @gameround = Gameround.find(params[:gameround_id])
      @currentplayer = @gameround.currentplayers.find(params[:id])
    @currentplayer.destroy
    respond_to do |format|
      format.html { redirect_to '/play', notice: 'Currentplayer was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def currentplayer_params
      params.require(:currentplayer).permit(:log_id, :flares, :winner, :bases, :gameround_id, aliens:[])
    end
end

config.routes

Rails.application.routes.draw do


  resources :gamerounds do 
    resources :currentplayers
  end 

2 个答案:

答案 0 :(得分:0)

我明白了。这只是因为提交按钮在主div之外。对不起,对不起!

答案 1 :(得分:-1)

您想要在config / routes.rb中创建路由,如下所示:

resources :current_players