Rails表单提交空值或旧值

时间:2011-09-14 10:28:57

标签: ruby-on-rails ruby forms post

我遇到了一个奇怪的问题,即我在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

2 个答案:

答案 0 :(得分:1)

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

基本上我一直在搞乱我的entry.rb文件与其他东西相关,并且定义了attr_reader和attr_accessible,而不应该这样。不确定幕后发生了什么,但显然导致rails没有解析发布的信息。

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

答案 1 :(得分:0)

检查你的日志,params hash究竟是什么样的?很可能你没有一个名为的根级别密钥:entry,也许它是params [:campaign] [:entry](我没有使用那种形式的风格,所以我不知道),还有,不是吗还需要获取@campaign吗?