Ruby问题从新视图显示变量,但不编辑

时间:2016-05-08 00:13:11

标签: ruby-on-rails ruby if-statement

在我的应用中,我进行了部分测验,因为我需要为newedit视图进行相同(冗长)的测验。在application.html.erbusers#show页面的标题中,我使用if / else语句显示此测验的结果。出于某种原因,在用户在new视图中进行测验后,它正在注册测验已完成(您可以通过if / else语句判断)但不显示变量。但是,使用edit视图(调用相同的部分_quiz.html.erb),变量会完美显示。

我的new视图调用了这样的部分:

<%= render partial: "quiz", locals: { url: swing_books_path, method: :post } %>

虽然我的edit视图称之为:

<%= render partial: "quiz", locals: { url: edit_swing_book_path(@swing_book), method: :put } %>

部分看起来像这样:

<%= form_for @swing_book do |f| %>
  <div class="clearfix">
    <h1 class="text-center">
    <%= f.text_field :swing01, class: 'form-control input-1' %>
    <%= f.text_field :swing02, class: 'form-control input-1' %>
    <%= f.text_field :swing03, class: 'form-control input-1' %>
    -
    <%= f.text_field :swing04, class: 'form-control input-1' %>
    <%= f.text_field :swing05, class: 'form-control input-1' %>
    <%= f.text_field :swing06, class: 'form-control input-1' %>
    -
    <%= f.text_field :swing07, class: 'form-control input-1' %>
    <%= f.text_field :swing08, class: 'form-control input-1' %>
    <%= f.text_field :swing09, class: 'form-control input-1' %>
    <%= f.text_field :swing10, class: 'form-control input-1' %>
   </h1>

   <div class="form-group text-center">
      <%= f.submit "Submit Swing Code", class: 'btn btn-success' %>
   </div>
 </div>
<% end %>

但是,我在另一个没有相同问题的测验中使用这些完全相同的细节(只更改了变量名称)。 (我还有其他三个类似的测验,所有这些测验都可以从newedit视图完美展示。)

我的模型看起来像这样:

class SwingBook < ActiveRecord::Base
  before_save :set_swing_code

  def set_swing_code
    self.swing_code = "#{self.swing01}#{self.swing02}#{self.swing03}-#{self.swing04}#{self.swing05}#{self.swing06}-#{self.swing07}#{self.swing08}#{self.swing09}#{self.swing10}"
  end

  belongs_to :user
  validates :user, presence: true
end

我的控制器看起来像这样:

class SwingBooksController < ApplicationController
before_action :require_sign_in

def show
  @swing_book = SwingBook.find(params[:id])
end

def new
  @swing_book = current_user.build_swing_book
end

def create
  @swing_book = SwingBook.new

  @swing_book.swing_code = params[:swing_book][:swing01]
  @swing_book.swing_code = params[:swing_book][:swing02]
  @swing_book.swing_code = params[:swing_book][:swing03]
  @swing_book.swing_code = params[:swing_book][:swing04]
  @swing_book.swing_code = params[:swing_book][:swing05]
  @swing_book.swing_code = params[:swing_book][:swing06]
  @swing_book.swing_code = params[:swing_book][:swing07]
  @swing_book.swing_code = params[:swing_book][:swing08]
  @swing_book.swing_code = params[:swing_book][:swing09]
  @swing_book.swing_code = params[:swing_book][:swing10]
  @swing_book.swing_code = params[:swing_book][:swing_code]

  @swing_book.user = current_user

  if @swing_book.save
    flash[:notice] = "Your swing code was saved successfully."
    redirect_to user_path(current_user)
  else
    flash[:alert] = "Sorry, your results failed to save."
    redirect_to welcome_index_path
  end
end

def edit
  @swing_book = SwingBook.find(params[:id])
end

def update
  @swing_book = SwingBook.find(params[:id])

  @swing_book.assign_attributes(swing_book_params)

  if @swing_book.save
    flash[:notice] = "Results were updated successfully."
    redirect_to user_path(current_user)
  else
    flash.now[:alert] = "There was an error saving your results. Please try again."
    redirect_to welcome_index_path
  end
end

private
def swing_book_params
  params.require(:swing_book).permit(:swing01, :swing02, :swing03, :swing04, :swing05, :swing06, :swing07, :swing08, :swing09, :swing10, :swing_code)
end
end

最后,这是我用来在application.html.erb文件中显示这些变量的逻辑:

<% if current_user.swing_book != nil %>
  <p>ID Code:
    <%= current_user.swing_book.swing01 %>
    <%= current_user.swing_book.swing02 %>
    <%= current_user.swing_book.swing03 %>
    -
    <%= current_user.swing_book.swing04 %>
    <%= current_user.swing_book.swing05 %>
    <%= current_user.swing_book.swing06 %>
    -
    <%= current_user.swing_book.swing07 %>
    <%= current_user.swing_book.swing08 %>
    <%= current_user.swing_book.swing09 %>
    <%= current_user.swing_book.swing10 %></p>
<% end %>

user#show页面上:

<% if @user.swing_book == nil %>
  <h3>ID Code: ???-???-????<%= %></h3>
  <p><%= link_to "Get Your ID Code Now!", new_swing_book_path %></p>
<% else %>
  <h3>ID Code:
    <%= @user.swing_book.swing01 %>
    <%= @user.swing_book.swing02 %>
    <%= @user.swing_book.swing03 %>
    -
    <%= @user.swing_book.swing04 %>
    <%= @user.swing_book.swing05 %>
    <%= @user.swing_book.swing06 %>
    -
    <%= @user.swing_book.swing07 %>
    <%= @user.swing_book.swing08 %>
    <%= @user.swing_book.swing09 %>
    <%= @user.swing_book.swing10 %></h3>
  <p><%= link_to "Update Your Code", edit_swing_book_path(@user.swing_book) %></p>
 <% end %>

我最初尝试只显示swing_code变量(在method中建立,但是它显示了一个整数,所以我切换到上面看到的代码。理想情况下,我想显示它只是swing_code变量,但我正在使用的(有点重复)解决方法工作得很好,如果我只能解决new / edit显示问题。

关于可能出现什么问题的任何想法?我发现这个和其他可能导致问题的测验之间没有任何区别!

1 个答案:

答案 0 :(得分:1)

create方法中,您有一段显式设置@swing_book值的代码。此代码将表单中的所有值都设置为swing_code成员。由于最后一次分配也是从swing_code param到swing_code成员,因此它隐藏了错误。这是分配了正确成员字段的代码:

@swing_book.swing01 = params[:swing_book][:swing01]
@swing_book.swing02 = params[:swing_book][:swing02]
@swing_book.swing03 = params[:swing_book][:swing03]
@swing_book.swing04 = params[:swing_book][:swing04]
@swing_book.swing05 = params[:swing_book][:swing05]
@swing_book.swing06 = params[:swing_book][:swing06]
@swing_book.swing07 = params[:swing_book][:swing07]
@swing_book.swing08 = params[:swing_book][:swing08]
@swing_book.swing09 = params[:swing_book][:swing09]
@swing_book.swing10 = params[:swing_book][:swing10]
@swing_book.swing_code = params[:swing_book][:swing_code]

现在,虽然这将解决当前的问题,但您可能只想使用与update方法相同的方法。请尝试使用create方法,而不是:

def create
  @swing_book = SwingBook.new

  @swing_book.assign_attributes(swing_book_params)
  @swing_book.user = current_user

  if @swing_book.save
    flash[:notice] = "Your swing code was saved successfully."
    redirect_to user_path(current_user)
  else
    flash[:alert] = "Sorry, your results failed to save."
    redirect_to welcome_index_path
  end
end

但是,您有机会最小化整体代码以帮助消除此类错误。考虑一下这样的控制器代码:

class SwingBooksController < ApplicationController
  before_action :require_sign_in

  def show
    @swing_book = SwingBook.find(params[:id])
  end

  def new
    @swing_book = current_user.build_swing_book
  end

  def edit
    @swing_book = SwingBook.find(params[:id])
  end

  def create
    @swing_book = SwingBook.new
    apply_form_values
  end

  def update
    @swing_book = SwingBook.find(params[:id])
    apply_form_values
  end

private
  def apply_form_values
    @swing_book.assign_attributes(swing_book_params)

    if @swing_book.save
      flash[:notice] = "Results were updated successfully."
      redirect_to user_path(current_user)
    else
      flash.now[:alert] = "There was an error saving your results. Please try again."
      redirect_to welcome_index_path
    end
  end

  def swing_book_params
    params.require(:swing_book).permit(:swing01, :swing02, :swing03, :swing04, :swing05, :swing06, :swing07, :swing08, :swing09, :swing10, :swing_code)
  end
end

您可以使用这样的技术来更轻松地编写初始代码,并使其更易于维护。