简体   繁体   English

Rails:基于另一个模型的控制器中的参数创建新模型

[英]Rails: Creating new model based on a parameter in another model's controller

I'm trying to create a model where every blog post has a number of tags (a separate model) associated to it. 我正在尝试创建一个模型,其中每个博客帖子都具有与之关联的多个标签(一个单独的模型)。 In my form, I pass back a separated string of tags along with the post, like "apples,oranges,bananas". 在我的表单中,我将带有标签的一串单独的标签与帖子一起传回,例如“苹果,橙子,香蕉”。 I'm eventually going to try and split this string and then create a tag for each string. 我最终将尝试分割此字符串,然后为每个字符串创建一个标签。 However, for now I'm just trying to see if I can make one tag with the whole string. 但是,现在我只是想看看是否可以用整个字符串制作一个标签。

The request is handled by the create method of the posts controller. 该请求由posts控制器的create方法处理。 However, I can't figure out how to create and commit the tag in the posts controller, or at least call the tags controller to delegate the creation. 但是,我无法弄清楚如何在posts控制器中创建和提交标签,或者至少不能调用tags控制器来委托创建。 I get the error: 我得到错误:

undefined method `new=' for Tag(id: integer, tag_name: string, post_id: integer):Class

This points to the line Tag.new = @post.tags.create(post_params[:tag_name]) of the posts_controller. 这指向posts_controller的Tag.new = @ post.tags.create(post_params [:tag_name])行。

Relevant code: 相关代码:

posts_controller posts_controller

def new
        @post = Post.new
    end

    def create
        @post = current_user.posts.create(post_params)
        Tag.new = @post.tags.create(post_params[:tag_name])
        if @post.save
            redirect_to post_path(@post), :notice => "Post was created successfully."
        else
            render :new
        end
    end

private
    def post_params
        params.require(:post).permit(:title, :content_md, :image, :user_id, :year_created, :medium, :dimension_height, :dimension_width, :measurement, :weight_in_pounds, :price, :quantity)
    end

tags_controller tags_controller

class TagsController < ApplicationController

  def new
    @tag = Tag.new
  end

  def create
    @tag = tag.create(tag_params)
  end

  private
  def tag_params
    params.require().permit(:tag_name, :user_id)
  end

end

post.rb post.rb

class Post < ActiveRecord::Base

  # Relations
  belongs_to :user
  has_many :comments
  has_many :post_votes
  has_many :tags
  accepts_nested_attributes_for :tags
end

tag.rb tag.rb

class Tag < ActiveRecord::Base
  validates :tag_name, presence: true
  validates :post_id, presence: true
  belongs_to :post
end

Edit: 编辑:

Also tried: 还尝试了:

def create
        @tag = Tag.new(post_params[:tag_name])
        @tag.save
        @post = current_user.posts.create(post_params)
                # @post.tags.create(post_params[:tag_name])
        if @post.save
            redirect_to post_path(@post), :notice => "Post was created successfully."
        else
            render :new
        end
    end

for the posts controller. 用于职位控制器。 It creates the post, but doesn't seem to actually save the @tag 它创建了帖子,但似乎并没有真正保存@tag

Without going into much deep, I see you you've the following line in your code (in posts_controller) 无需深入探讨,我看到您在代码中有以下行(在posts_controller中)

Tag.new = @post.tags.create(post_params[:tag_name])

which is not correct. 这是不正确的。 If you want to just create (but not persist) you should call, 如果您只想创建(但不保留),请致电,

@post.tags.new(post_params[:tag_name])

For persist 对于坚持

@post.tags.create(post_params[:tag_name])

This will get rid of the error that you've posted. 这将消除您发布的错误。 However, you shouldn't require these as your Post model can accept nested attributes for tags. 但是,您不应该要求这些,因为您的Post模型可以接受标记的嵌套属性。 Just make sure you've generated form correctly (hint: fields_for ) 只要确保您已正确生成表单(提示: fields_for

This line 这条线

Tag.new = @post.tags.create(post_params[:tag_name])

has no sense. 没有任何意义 Without further discussion: 无需进一步讨论:

@post.tags.create(post_params[:tag_name])

will work and actually create the tag. 将起作用并实际创建标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM