Rails 表单提交空值或旧值

发布于 2024-12-04 09:41:03 字数 3381 浏览 0 评论 0原文

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

I'm having a weird problem whereby one of my forms in rails is not receiving any of my post data.

My model is called entries, and the routes are nested within the campaigns controller. When submitting a new entry, the entry is saved but all of the values appear blank, and when editing an existing entry and clicking submit, the entry doesn't take any of the new values entered and instead reverts to original values.

My code is as follows

/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">
    
  </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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倚栏听风 2024-12-11 09:41:03

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

基本上我一直在搞乱我的entry.rb文件与其他东西的关系,并且在不应该定义的时候定义了attr_reader和attr_accessible。不确定幕后到底发生了什么,但显然 Rails 没有解析发布的信息。

希望这对遇到类似问题的人有所帮助,为帮助干杯

Figured out the problem, was something silly like I thought.

Basically i had been messing with my entry.rb file in relation to something else, and had defined attr_reader and attr_accessible when they should not have been. Not sure exactly what goes on behind the scenes but obviously as a result rails was not parsing the posted information.

Hope this helps anyone with a similar problem, cheers for the help

若有似无的小暗淡 2024-12-11 09:41:03

检查你的日志,参数哈希到底是什么样的?很可能你没有名为 :entry 的根级密钥,也许它是 params[:campaign][:entry] (我没有使用过那种形式的样式,所以我不知道),另外,你不是吗还需要获取@campaign 吗?

Check your logs, what exactly does the params hash look like? Most likely you don't have a root level key called :entry, maybe it's params[:campaign][:entry] (I haven't used that style of form so I don't know), also, don't you need to fetch the @campaign as well?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文