我制作了一个非常简单的RESTful应用程序,当我在表单字段中输入一个字符串并提交它时,数据库保持NULL而不是输入字符串。
这是我的控制器:
def create
@song = Song.create(params[:title])
flash[:success] = "You have successfully created a new project!"
redirect_to "/songs/#{@song.id}"
end
这是我在new.html.erb文件中的表单:
<%= form_for(@song) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<br>
<% end %>
答案 0 :(得分:4)
如果您使用 rails&lt; 4 然后你应该
def create
@song = Song.create(params[:song])
flash[:success] = "You have successfully created a new project!"
redirect_to @song
end
如果您使用 rails&gt; 4 然后你应该
def create
@song = Song.create(song_params)
flash[:success] = "You have successfully created a new project!"
redirect_to @song
end
private
def song_params
params.require(:song).permit(:title)
end