【问题标题】:Rails Form Submits Empty or Old ValuesRails 表单提交空值或旧值
【发布时间】:2023-09-12 09:53:01
【问题描述】:

我遇到了一个奇怪的问题,即我在 rails 中的一个表单没有收到我的任何帖子数据。

我的模型称为条目,并且路由嵌套在活动控制器中。提交新条目时,条目被保存,但所有值显示为空白,并且在编辑现有条目并单击提交时,条目不采用任何输入的新值,而是恢复为原始值。

我的代码如下

/entries/_form.html.erb

<%= form_for [@campaign, @entry] do |f| %>

 <div class="row">
  <div class="left">
    <%= f.label :name %>
  </div>
  <div class="right">
    <%= f.text_field :name, :class => "textbox" %>
  </div>

</div>
<div class="row tarow">
  <div class="left">
    <%= f.label :content %>
  </div>
  <div class="right">
    <%= f.text_area :content %>
  </div>

</div>


<div class="row tarow">
  <div class="left">
    <%= f.label :description %>
  </div>
  <div class="right">
    <%= f.text_area :description %>
  </div>

</div>


<div class="row">
  <div class="left">
    <%= f.label :provider %>
  </div>
  <div class="right">
    <%= f.text_field :provider, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :image %>
  </div>
  <div class="right">
    <%= f.text_field :image, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :url %>
  </div>
  <div class="right">
    <%= f.text_field :url, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :votes %>
  </div>
  <div class="right">
    <%= f.text_field :votes, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :hours  %>
  </div>
  <div class="right">
    <%= f.text_field :hours, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
   &nbsp;
  </div>
  <div class="right">
<%= f.submit %>
  </div>

</div>
<% end %>

entries_controller.rb

def new

@entry = Entry.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @entry }
end
end

def edit
end

# POST /entries
# POST /entries.xml
def create

  @entry = Entry.new(params[:entry])
  @entry.user_id = current_user.id
  @entry.campaign_id = @campaign.id

    respond_to do |format|
     if @entry.save
      format.html { redirect_to(campaign_entry_path(@campaign.url, @entry) , :notice => 'Entry was successfully created.') }
      format.xml  { render :xml => @entry, :status => :created, :location => @entry }
     else
      format.html { render :action => "new" }
      format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
     end
    end

end

def update


respond_to do |format|
  if @entry.update_attributes(params[:entry])
    format.html { redirect_to(campaign_entry_path(@campaign.url, @entry), :notice => 'Entry was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
  end
end
end

【问题讨论】:

    标签: ruby-on-rails ruby forms post


    【解决方案1】:

    找出问题所在,就像我想的那样愚蠢。

    基本上,我一直在弄乱我的 entry.rb 文件与其他东西有关,并且在不应该定义 attr_reader 和 attr_accessible 时定义了它们。不确定幕后究竟发生了什么,但很明显,rails 没有解析发布的信息。

    希望这可以帮助遇到类似问题的任何人,为帮助欢呼

    【讨论】:

      【解决方案2】:

      检查您的日志,params 哈希究竟是什么样的?很可能您没有名为 :entry 的根级别键,也许它是 params[:campaign][:entry] (我没有使用过那种形式的表单,所以我不知道),还有,不是吗还需要获取@campaign?

      【讨论】: