首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionController::在提交时

ActionController::在提交时
EN

Stack Overflow用户
提问于 2016-12-22 06:13:41
回答 2查看 194关注 0票数 1

我有一个程序,允许用户为某一事件输入他们的歌曲。您必须输入该事件的partycode才能提交。下面是它的截图:

当我提交它时,会出现以下错误:

下面是我的SongsController的样子:

代码语言:javascript
复制
class SongsController < ApplicationController

    def new
        @song = Song.new
    end

    def create
        current_event = Event.find(song_params[:event_id])
        @song = current_event.songs.build(song_params)
        if @song.save
            flash[:success] = "Success"
            redirect_to event_path(@song.event)
        else
            flash[:error] = "Failed"
        end

    end

    def destroy
    end
    private

      def song_params
        params.require(:song).permit(:event_id, :artist, :title, :genre)
      end
end

事件模型

代码语言:javascript
复制
class Event < ApplicationRecord
  belongs_to :user
  validates :name, presence: true
  validates :partycode, presence: true, length: {minimum: 5}
  has_many :songs, dependent: :destroy  

end

宋模型

代码语言:javascript
复制
class Song < ApplicationRecord
  belongs_to :event
  validates :artist, presence: true
  validates :title,  presence: true
end

New.html.erb(歌曲)

代码语言:javascript
复制
<br>
<br>
<h1> Member page </h1>
<div class ="container">
  <div class="jumbotron">
    <h2> Select an event to add songs to: </h2>
    <%= form_for Song.new do |f| %>
        <%= f.collection_select(:event_id, Event.all, :id, :name) %>
    <h3> Enter your song </h3>
    <%= form_for Song.new do |f| %>
      <%= f.text_field :artist, placeholder: "Artist" %>
      <%= f.text_field :title,  placeholder: "Title" %>
      <%= f.text_field :genre,  placeholder: "Genre" %>
    <h2> Enter the partycode for that event: </h2>
    <%= form_for Event.new do |f| %>
      <%= f.text_field :partycode %>
      <%= f.submit "Submit", class: "btn btn-primary" %>
    <% end %>
    <% end %>
    <% end %>
  </div>
</div>

我怎样才能使我的网站的这一功能发挥作用?任何帮助都是非常感谢的

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-22 06:45:46

我看到许多form_for嵌套在您的视图上。不可能的。只提交一份表格。

我想你可能想改变你的_form.html.erb

代码语言:javascript
复制
<div class ="container">
  <div class="jumbotron">
    <h2> Select an event to add songs to: </h2>
    <%= form_for @song do |f| %>
      <%= f.collection_select(:event_id, Event.all, :id, :name) %>
      <h3> Enter your song </h3>
      <%= f.text_field :artist, placeholder: "Artist" %>
      <%= f.text_field :title,  placeholder: "Title" %>
      <%= f.text_field :genre,  placeholder: "Genre" %>
      <h2> Enter the partycode for that event: </h2>
      <%= f.text_field :partycode %>
      <%= f.submit "Submit", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>
票数 1
EN

Stack Overflow用户

发布于 2016-12-22 06:57:37

你完全搞砸了你的表格。理想情况下,您应该有一个表单,但是在这里,您只是使用form_for将一个表单保存在另一个表单中。

我建议你看看form_for documentation

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41276809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档