Rails 4:创建新实例并将其保存到数据库但不在视图中显示

时间:2015-07-06 22:50:59

标签: ruby-on-rails forms ruby-on-rails-4 relational-database instances

在我们的Rails 4应用程序中,有四种模式:

class User < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :calendars, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations
end

class Post < ActiveRecord::Base
    belongs_to :calendar
end

以下是相应的迁移:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email
      t.integer :total_calendar_count
      t.integer :owned_calendar_count

      t.timestamps null: false
    end
  end
end

class CreateAdministrations < ActiveRecord::Migration
  def change
    create_table :administrations do |t|
      t.references :user, index: true, foreign_key: true
      t.references :calendar, index: true, foreign_key: true
      t.string :role

      t.timestamps null: false
    end
  end
end

class CreateCalendars < ActiveRecord::Migration
  def change
    create_table :calendars do |t|
      t.string :name

      t.timestamps null: false
    end
  end
end

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
        t.references :calendar, index: true, foreign_key: true
        t.date :date
        t.time :time
        t.string :focus
        t.string :format
        t.string :blog_title
        t.text :long_copy
        t.text :short_copy
        t.string :link
        t.string :hashtag
        t.string :media
        t.float :promotion
        t.string :target
        t.integer :approval
        t.text :comment

      t.timestamps null: false
    end
  end
end

我们有以下表格来创建帖子:

<h2>Create a new post</h2>
<%= form_for(@post) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
    <tr>
        <td class="field"><%= f.date_field :date, placeholder: "When do you want to publish this post?" %></td>
        <td class="field"><%= f.time_field :time, placeholder: "What time do you want to publish this post?" %></td>
        <td class="field"><%= f.text_field :focus, placeholder: "What is this post about?" %></td>
        <td class="field"><%= f.text_field :format, placeholder: "What type of post is this?" %></td>
        <td class="field"><%= f.text_field :blog_title, placeholder: "If this post is about a blog post, what is the title of the blog post?" %></td>
        <td class="field"><%= f.text_area :long_copy, placeholder: "What is the copy of the post?" %></td>
        <td class="field"><%= f.text_area :short_copy, placeholder: "What is the short copy of the post (to be used on Twitter for instance)?" %></td>
        <td class="field"><%= f.url_field :link, placeholder: "Which link to you want to embed in this post?" %></td>
        <td class="field"><%= f.text_field :hashtag, placeholder: "Which hashtag(s) do you want to you in this post?" %></td>
        <td class="field"><%= f.text_field :media, placeholder: "Which media file (image, video) do you want to include in this post?" %></td>
        <td class="field"><%= f.number_field :promotion, placeholder: "What advertising budget should be allocated to this post?" %></td>
        <td class="field"><%= f.text_field :target, placeholder: "Who do you want to target with this post?" %></td>
        <td class="field"><%= f.select(:approval, %w[Approved Needs edits To be deleted], {prompt: 'How does this post look?'}) %></td>
        <td class="field"><%= f.text_area :comment, placeholder: "Any comment?" %></td>

        <td><%= f.submit "Create", class: "btn btn-primary" %></td>

    </tr>

<% end %>

此表单已嵌入Calendars#Show视图中,因此只要创建帖子,它就会显示在相应的日历中。

编辑2 :这是ou PostsController

class PostsController < ApplicationController

  def create
    @post = Post.create!
    if @post.save
      redirect_to root_url
    else
      render root_url
    end
  end

end

目前,每当我们提交表单时,实际上都会创建一个帖子(我们通过控制台进行了检查)。

编辑:实际上,新的@post已创建并保存到数据库,但所有值都设置为nil。似乎我们在从表单和数据库中保存数据方面也存在问题。

但是,它不会出现在相应的日历中。

我们认为这是因为,在创建帖子时,活动日历(嵌入表单的日历)的calendar_id不会添加到新帖子实例中。

但是,我们不知道如何实施它:

  • 我们是否应该创建active_calendar方法,然后使用它自动将活动日历的active_calendar id分配给新创建的@post
  • 我们是否应该简单地将活动日历id声明为变量,在post控制器或帖子模型中,以便我们可以在创建新的@post时添加它?
  • 我们是否应该通过表单中的隐藏字段在新创建的id中添加有效日历@post

我们在这里遇到了障碍,并且不确定我们做错了什么。

如何解决这个问题?

更新:正如@ Fire-Dragon-DoL在回答评论中所建议的那样,我们将使用隐藏字段。

以下代码是否有效?

<td type="hidden" value="#{@calendar.id}"><% f.number_field :calendar_id %></td>

2 个答案:

答案 0 :(得分:1)

我不确定如何或在哪里存储您的“活动日历”信息,您肯定需要它。将其设为User的字段,这是一个不错的选择。

然后我会创建一个像

这样的应用程序控制器方法
def active_calendar
  # Assuming you have a method to return current logged in user
  current_user.try(:active_calendar)
end

然后在创建帖子时,您可以轻松执行以下操作

attrs = post_params.merge(calendar: active_calendar)
@post = Post.new(attrs)
# Or whatever logic you want
@post.save

仅当允许用户指定与活动日历不同的日历时才使用隐藏字段,否则可能只是一个安全漏洞

请注意,我编写的代码也是无证件

答案 1 :(得分:0)

您可能希望嵌套路线,例如:

resources :calendars do
    resources :posts
end

现在更改您的表单以包含您希望与帖子相关联的日历,如:

form_for([@calendar, @post])...

然后在你的帖子控制器创建路径中,你可以找到你的日历并用以下内容构建你的帖子:

def create
  my_calendar = Calendar.find(params[:calendar_id])
  post = my_calendar.posts.create(post_params)
  redirect_to root_path 
end

private

def post_params
  params.require(:post).permit(:date, :time, :focus, :format, :blog_title, :long_copy, :short_copy, :link, :hashtag, :media, :promotion, :target, :approval, :comment)
end