Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:43:51+00:00 2026-05-31T01:43:51+00:00

I feel like I have no idea what I’m doing.. I have a vague

  • 0

I feel like I have no idea what I’m doing.. I have a vague Idea. I’m hoping I did this all right so far.

Any way you can see to refactor this would be greatly appreciated.

One thing I noticed it does wrong is it won’t load the proper options that were previously submitted if there is an error and it posts to the same URL. The text inputs seem to load the previous value but the select and the radio buttons reset to the default every submit.

ResourcesController#new

 def new
    @resource = Resource.new
    @title = "Submit Resource"
    @categories = Category.all
 end

ResourcesController#create (notice I have @categories = Category.all in both… according to DRY im not sure where else it should go, or it only works on the first form submit.

  def create
    @title = "Submit Resource"
    @categories = Category.all

    @resource = Resource.new(params[:resource])

    category_ids = @categories.map { |c| c[1] }

    if @resource.valid? and category_ids.include? params[:category_id]
      @resource.cost = params[:cost]
      @resource.category_id = params[:category_id]
      @resource.save
      redirect_to root_url
    else
      render :action => :new
    end 
  end

Resource.rb (model)

# == Schema Information
#
# Table name: resources
#
#  id          :integer         not null, primary key
#  upvotes     :integer         default(0)
#  downvotes   :integer         default(0)
#  url         :string(255)
#  title       :string(255)
#  cost        :integer         default(0)
#  description :text
#  flags       :integer
#  category_id :integer
#  user_id     :integer
#  created_at  :datetime        not null
#  updated_at  :datetime        not null
#

class Resource < ActiveRecord::Base

  belongs_to :category
  belongs_to :user
  has_many :favorites
  has_many :resource_tags
  has_many :tags, :through => :resource_tags

  attr_accessible :url, :title, :cost, :description, :category_id, :user_id

  # Pseudo-Enum
  COST = [:free, :paid, :both]

  url_regex = /^(?:http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/ix

  validates :url,         :presence => true,
                          :format   => { :with => url_regex, 
                                         :message  => "must be valid"},
                          :uniqueness => { :case_sensitive => false,
                                           :message => "has already been submitted"}
  validates :title,       :presence => true,
                          :length   => { :within => 6..75 }
  validates :cost,        :presence => true
  validates :description, :presence => true,
                          :length   => { :within => 25..200 }
  validates :category_id, :presence => true,
                          :format   => { :with => /\d+/ }
  validates :user_id,     :presence => true,
                          :format   => { :with => /\d+/ }

  def cost
    COST[read_attribute(:cost)]
  end

  def cost=(value)
    write_attribute(:cost, COST.index(value.downcase.to_sym))
  end

  def category_id
    read_attribute(:category_id).to_i
  end

  def category_id=(value)
    write_attribute(:category_id, value.to_i)
  end

end

My view file for the Resource#new form

  <div class="field">
    <%= f.label :category %>
    <%= select_tag(:category_id, options_for_select(@categories.map {|c|[c.name, c.id]})) %> 
  </div>

Last Q: i havent worked with the user_id field yet. This is going to be pulled from devise and will associate a User with a submitted resource. But how do I assign this without making some sort of input, like a hidden input. Would this go on behind the scenes in the controller?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:43:52+00:00Added an answer on May 31, 2026 at 1:43 am

    To your last question:

    devise adds a current_user method which is the logged in user. So if a user has multiple resources you could do something like:

    @resource = current_user.resources.new(params[:resource])
    

    First question:

    When a form is rendered it is done so based on the @resource & @categories variables. When you post the form the create action is called which creates a new @resource. If the save fails for whatever reason the form is rerendered using the new @resource variable. The problem you have is that @resource.category is not set when you show the form again. So you’ll have to do this before the is_valid? check.

     def create
        @title = "Submit Resource"
        @categories = Category.all
    
        @resource = Resource.new(params[:resource])
        @resource.category = Category.find(params[:category_id])
    
        if @resource.valid? # won't be valid if there is no category found.
          @resource.cost = params[:cost]
          @resource.save
          redirect_to root_url
        else
          render :action => :new
        end 
      end
    

    But the real problem is with your form. It should nest the category_id in the resource params so that the category is set when you do Resource.new(params[:resource]).

    Check the POST request body in your console or something and see if it’s nested in the resource or not. I don’t know the exact syntax for it but if you change this you can drop the @resource.category = Category.find line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like an idiot for even asking this but does anyone have any
I feel like this must have been asked before, but I've looked at Knockout
I feel like this is quite delicate, I have various folders whith projects I
So I feel like a serious rookie right now, but I have a problem
I feel like an idiot for having to ask this question, but I have
I feel like I have enough experience with CI to finally start fooling around
I feel like I have everything I need in my routes.rb, my controller, and
I am pretty new to Haskell but I feel like I have a decent
I feel like GUID/UUID questions have been done to death here, but I can't
I feel like I've entered the Twilight Zone. I have a Grails form that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.