0

I have the following methods for Create and Update in my Controller:

  def new
    if request.post?
      @article = Article.new(article_params)
      @article.user = @user
      if @article.save
        redirect_to :admin_articles, :flash => { success: t(:article_created) }
      end
    else
      @article = Article.new
    end
  end

  def edit
    if request.patch?
      if @article.update(article_params)
        redirect_to :admin_articles, :flash => { success: t(:article_updated) }
      end
    end
  end

And I have the following for the article_params:

    def article_params
        article_params = params[:article].permit(:category_id, :title, :slug, :article_type, :content, :link, :summary)
        if params[:tags].present?
          tags = params[:tags].split ','
          tags_array = Array.new
          tags.each do |t|
            tags_array.append Tag.find_or_create_by slug: t
          end

          article_params[:tags] = tags_array
        end

    article_params
  end

When I do the Update it saves properly, but when I try to do the Create it says Article Tags is invalid. Does anyone know what I am doing wrong?

3
  • Where is your create action? Convention!!! Commented Dec 31, 2013 at 22:14
  • Hi, is it possible to determine if a record was successfully created using the create method? Commented Jan 1, 2014 at 1:40
  • Nevermind, saw it was. I did not realize this was improper convention, thanks. Commented Jan 1, 2014 at 1:51

2 Answers 2

1

You don't have (or at least haven't shown) a create method in your controller, so you're just getting ActiveModel's default implementation which isn't going to pick up any of the params. If you're doing something non-standard with your routes, such that POST is getting mapped to new, please share that.

Sign up to request clarification or add additional context in comments.

10 Comments

I map it like: match 'articles/new', to: 'articles#new', via: [:get, :post], as: :article_new
Can I ask how it is that you're causing the POST to go to articles/new on submission (i.e. from your view)?
<%= form_for @article, { :url => :admin_article_new } do |f| %>
Can I ask why you're not following the Rails defaults?
Have you confirmed your :url parameter is working (i.e. that you're taking the request.post? branch of your if/else)?
|
0

the issue was me not understanding convention. My object had not been created therefore it did not have a tags property yet. I have changed my methods to the following:

    def article_params
        params[:article].permit(:category_id, :title, :slug, :article_type, :content, :link, :summary)
      end

      def tags
        tags = Array.new

        if params[:tags].present?
          tag_param_array = params[:tags].split ','
          tag_param_array.each do |t|
            tags.append Tag.find_or_create_by slug: t
          end
        end

        tags
      end

      def new
    @article = Article.new
  end

  def create
    @article = Article.create article_params
    if @article.valid?
      @article.tags = tags

      redirect_to :admin_articles, :flash => { :success => t(:article_created) } if @article.save
    else
      render 'new'
    end
  end

  def edit
  end

  def patch
    @article.update_attributes article_params
    if @article.valid?
      @article.tags = tags

      redirect_to :admin_articles, :flash => { :success => t(:article_updated) } if @article.save
    end
  end

3 Comments

But you're still going against convention! You should really have a New and Create action to make life easier for yourself. See the intro to Rails for further info.
@tommyd456 I am confused. The Getting Started with Rails guide is showing to use the method before which you said was improper convention (.save). Can you explicitly link me a link to proper convention?
@tommyd456 I believe I understand what you meant. I have moved my create and patch logic into separate controller actions and routed them accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.