Question

Trying to create a form field where a user can submit a url per: http://apidock.com/rails/v3.2.13/ActionView/Helpers/FormHelper/url_field

I'm getting an error: ActionView::Template::Error (undefined method `homepage' for #

here is the model:

class Idea < ActiveRecord::Base
  has_many :comments
  mount_uploader :picture, PictureUploader
  attr_accessible :description, :name, :picture, :homepage
end

the view in form.html.erb

<%= form_for(@idea) do |f| %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
<div class="field">
    <%= f.label :link %><br />
    <%= url_field("homepage") %><br />
  </div>
  <div class="actions">
    <%= f.submit %>

the view in show.html.erb

<p><b>Name: </b><%= @idea.name %></p>
<p><b>Link:</b><%= @idea.homepage %></p>

ideas_controller

def create
    @idea = Idea.new(params[:idea])

    respond_to do |format|
      if @idea.save
        format.html { redirect_to @idea, notice: 'Idea was successfully created.' }
        format.json { render json: @idea, status: :created, location: @idea }
      else
        format.html { render action: "new" }
        format.json { render json: @idea.errors, status: :unprocessable_entity }
      end
    end
  end

def show
    @idea = Idea.find(params[:id])
    @comment = @idea.comments.build

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @idea }
    end
  end
Was it helpful?

Solution 2

IMHO using url_field in the form builder is antiquated and prone to errors. Eventually I was able to find: rails auto link from tenderlove: https://github.com/tenderlove/rails_autolink coupled with tinymce-rails from spohlenz: https://github.com/spohlenz/tinymce-rails. With these 2 gems you can build a full-featured form field and display the output much more effectively. Hopefully this helps someone else.

OTHER TIPS

Basically, when you're yielding, and using, a variable to the block in form_for, it already sets the association of the form fields.

ie:

url_field('user', 'homepage')

is equivalent to

f.url_field('homepage')

Check out the url_field, and the form_for documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top