我正在Sinatra編寫一個非常基本的CRUD應用程序(這是一個CRUD應用程序,它使得CRUD應用程序,它的價值)。創建功能正常工作,但更新功能不起作用。控制器未收到params散列中的表單數據。我可以看到在Chrome開發工具中收集的表單數據,但是在它到達控制器時,params散列看起來像{"splat"=>[], "captures"=>["23"], "id"=>"23"}
,而不是例如{"crudapp"=>{"title"=>"Promises", "description"=>"tbd", "model"=>"Promise"}, "columns"=>[{"key_name"=>"name", "data_type"=>"String"}, {"key_name"=>"description", "data_type"=>"String"}, {"key_name"=>"fulfillment", "data_type"=>"Boolean"}]}
。我錯過了什麼?下面的相關代碼。Sinatra:表單數據在提交後從參數散列表中缺失
edit.erb
<div class="entry-header">edit an app</div>
<form method="post" action="/crudapps/<%= @crudapp.id %>">
<input type="hidden" name="crudapp[user_id]" value="<%= @crudapp.user_id %>">
<ul class="basic-form">
<li>
<label for="crudapp[title]">Application title</label>
<input class="fieldbox" type="text" name="crudapp[title]" value="<%= @crudapp.title %>">
</li>
<li>
<label for="crudapp[description]">Description</label>
<textarea name="crudapp[description]"><%= @crudapp.description %></textarea>
</li>
<li>
<label for="crudapp[model]">Item name</label>
<input class="fieldbox" type="text" name="crudapp[model]" value="<%= @crudapp.model %>">
</li>
<input type="hidden" name="columns[0][id]" value="<%= @crudapp.columns[0].id %>">
<li>
<label for="columns[][key_name]">First attribute name</label>
<input class="fieldbox" type="text" name="columns[0][key_name]" value="<%= @crudapp.columns[0].key_name %>">
</li>
<li>
<label for="columns[0][data_type]">First attribute data type</label>
<select name="columns[0][data_type]">
<option value="String" <%= 'selected' if @crudapp.columns[0].data_type=='String' %>>String</option>
<option value="Number" <%= 'selected' if @crudapp.columns[0].data_type=='Number' %>>Number</option>
<option value="Date" <%= 'selected' if @crudapp.columns[0].data_type=='Date' %>>Date</option>
<option value="Boolean" <%= 'selected' if @crudapp.columns[0].data_type=='Boolean' %>>Boolean</option>
</select>
</li>
<input type="hidden" name="columns[1][id]" value="<%= @crudapp.columns[1].id %>">
<li>
<label for="columns[][key_name]">Second attribute name</label>
<input class="fieldbox" type="text" name="columns[1][key_name]" value="<%= @crudapp.columns[1].key_name %>">
</li>
<li>
<label for="columns[1][data_type]">Second attribute data type</label>
<select name="columns[1][data_type]">
<option value="String" <%= 'selected' if @crudapp.columns[1].data_type=='String' %>>String</option>
<option value="Number" <%= 'selected' if @crudapp.columns[1].data_type=='Number' %>>Number</option>
<option value="Date" <%= 'selected' if @crudapp.columns[1].data_type=='Date' %>>Date</option>
<option value="Boolean" <%= 'selected' if @crudapp.columns[1].data_type=='Boolean' %>>Boolean</option>
</select>
</li>
<input type="hidden" name="columns[2][id]" value="<%= @crudapp.columns[2].id %>">
<li>
<label for="columns[][key_name]">Third attribute name</label>
<input class="fieldbox" type="text" name="columns[][key_name]" value="<%= @crudapp.columns[2].key_name %>">
</li>
<li>
<label for="columns[2][data_type]">Third attribute data type</label>
<select name="columns[2][data_type]">
<option value="String" <%= 'selected' if @crudapp.columns[2].data_type=='String' %>>String</option>
<option value="Number" <%= 'selected' if @crudapp.columns[2].data_type=='Number' %>>Number</option>
<option value="Date" <%= 'selected' if @crudapp.columns[2].data_type=='Date' %>>Date</option>
<option value="Boolean" <%= 'selected' if @crudapp.columns[2].data_type=='Boolean' %>>Boolean</option>
</select>
</li>
<li>
<label for="crudapp[github]">GitHub link</label>
<input class="fieldbox" type="text" name="crudapp[github]" value="<%= @crudapp.github %>">
</li>
<li>
<label for="crudapp[web]">Web link</label>
<input class="fieldbox" type="text" name="crudapp[web]" value="<%= @crudapp.web %>">
</li>
</ul>
<div class="entry-submit-margin">
<input class="entry-submit" type="submit" value="submit">
</div>
</form>
application_controller.rb
class ApplicationController < Sinatra::Base
# lots of other routes omitted
post "/crudapps/:id" do
if logged_in?
@crudapp = Crudapp.find(params[:id])
if current_user.id == @crudapp.user_id
if !params[:crudapp] # This data is absent, so we always land here.
flash[:message] = "edit functionality is down!"
else
@crudapp.update(params[:crudapp])
params[:columns].each do |params_column|
column = Column.find_or_create_by(id: params_column[:id])
@crudapp.columns << @column.update(params_column)
end
end
redirect "/crudapps/#{@crudapp.id}"
else
flash[:message] = "you must be the app's creator to edit"
redirect "/crudapps"
end
else
flash[:message] = "please log in to edit an app"
redirect "/login"
end
end
end
好的答案是我在其中一個輸入標籤的名稱字段中排除了數組索引(2)。不過,我會發佈一個問題的答案,因爲它似乎指向一個更廣泛的問題,因爲也許別人會有一個破解的參數哈希問題,並會在這裏看到這個積極的結果。 –