我在表挑战和条目之间有一个简单的1到多表关系。 我希望一个用户(本身就是一对一的挑战)能够创建一个条目,并为此更新挑战。我不知道该怎么做。我采取的方法如下:
在ajax调用页面上创建一个div,列出所有挑战。然后,用户单击挑战,然后可以按下按钮以新的条目形式(也是ajax调用)创建新条目。基本上我希望用户能够创建一个新条目,并使其具有与之关联的正确challenge_id
schema.rb
ActiveRecord::Schema.define(version: 20150719082241) do
create_table "challenges", force: :cascade do |t|
t.integer "pointsSpend"
t.string "rules"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
create_table "entries", force: :cascade do |t|
t.string "blob"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "challenge_id"
t.string "picture"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
end
challenge.rb
class Challenge < ActiveRecord::Base
has_many :entries
belongs_to :user
end
entry.rb
class Entry < ActiveRecord::Base
belongs_to :challenge
end
<_>在entry_controller.rb 中
def create
@challenge = Challenge.find(params[:challenge_id])
@entry = Entry.new(entry_params)
@entry.challenge_id = @challenge.id
respond_to do |format|
if @entry.save
format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
format.json { render :show, status: :created, location: @entry }
render :layout => false
else
format.html { render :new }
format.json { render json: @entry.errors, status: :unprocessable_entity }
render :layout => false
end
end
end
在entry.rb模型中
def entry_params
params.require(:entry).permit(:id, :blobby, :challenge_id)
end
但是,这不会使用challenge_id更新条目表。我究竟做错了什么?是因为challenge_id参数没有传递给报名表或其他什么?
在challenge_controller.rb中
def create
@challenge = Challenge.new(challenge_params.merge({user_id: current_user.id}))
respond_to do |format|
if @challenge.save
format.html { redirect_to users_path, notice: 'Challenge was successfully created.' }
format.json { render :show, status: :created, location: @challenge }
else
format.html { render :new }
format.json { render json: @challenge.errors, status: :unprocessable_entity }
end
end
end
用户创建新挑战的表单页面
<%= form_for(@challenge) do |f| %>
<% if @challenge.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@challenge.errors.count, "error") %> prohibited this challenge from being saved:</h2>
<ul>
<% @challenge.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :pointsSpend %><br>
<%= f.number_field :pointsSpend%>
<div class="field">
<%= f.label :rules %><br>
<%= f.text_area :rules %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
和创建新条目的表单
<%= form_for(@entry) do |f| %>
<% if @entry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@entry.errors.count, "error") %> prohibited this entry from being saved:</h2>
<ul>
<% @entry.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :blob %><br>
<%= f.text_field :blob %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
ajax调用渲染所有这些表单
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require chosen-jquery
//= require messages
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
$(document).ready(function(){
$('#ChallengeButton').click(function(e){
e.preventDefault();
$.ajax({
type: 'GET',
url: "/challenges/new",
success: function(response){
$('#challengeCentre').html(response);
}
})
});
});
$(document).ready(function(){
$('#ChallengeListButton').click(function(e){
e.preventDefault();
$.ajax({
type: 'GET',
url: "challenges",
success: function(response){
console.log(response)
$('#challengeListCentre').html(response);
}
})
});
});
$(document).ready(function(){
$('#ChallengeJoinButton').click(function(e){
e.preventDefault();
$.ajax({
type: 'GET',
url: "/entries/new",
success: function(response){
$('#challengeJoinCentre').html(response);
}
})
});
});
答案 0 :(得分:0)
所以你看到条目表格中没有challenge_id的任何字段。 如果你的路线是嵌套的。替换这个
<%= form_for(@entry) do |f| %>
用这个
<%= form_for([@challenge, @entry]) do |f| %>.
它也将通过@challenge。请参阅此http://guides.rubyonrails.org/routing.html#nested-resources
虽然嵌套路线是最佳选择,但是如果您的路线没有嵌套。您可以在事件表单中添加隐藏字段。
<%= hidden_field_tag :challenge_id, @challenge.id %>
你可以在事件创建动作中访问
@challenge = Challenge.find(params[:challenge_id])
或者你可以这样做
<%= f.hidden_field :challenge_id, value: @challenge.id %>
它会在事件参数中自动生成challenge_id,并为事件设置challenge_id。