我遇到了一个奇怪的问题,即我的一个表单在rails中没有收到任何我的帖子数据。
我的模型叫做条目,路由嵌套在campaign控制器中。在提交新条目时,保存条目,但所有值都显示为空白,在编辑现有条目并单击提交时,条目不接受输入的任何新值,而是恢复到原始值。
我的代码如下
/条目/_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
解决了问题,就像我想的那样傻。
基本上我一直在弄乱我的条目。并且在不应该定义attr_reader和attr_accessible的时候定义了它们。不确定幕后到底发生了什么,但显然rails没有解析发布的信息。
希望这能帮助到任何有类似问题的人,谢谢你的帮助
检查您的日志,参数散列究竟是什么样子的?最有可能的是,你没有一个根级键称为:入口,也许它的params[:campaign][:entry](我没有使用的形式的那种风格,所以我不知道),也,你不需要取回@campaign ?