I have what should be a fairly simple setup for paperclip on my rails app.
My products_controller.rb is this:
class ProductsController < ApplicationController
...
def new
#product = Product.new
end
def create
#product = Product.new(product_params)
#product.save
flash[:notice] = "Your product has been created!"
redirect_to products_path
end
def edit
#product = Product.find(params[:id])
end
def update
#product = Product.find(params[:id])
if #product.update_attributes(product_params)
redirect_to products_path
flash[:notice] = "That product has been updated."
else
render :action => :edit
flash[:alert] = "Something went terribly wrong there..."
end
end
...
private
def product_params
params.require(:product).permit(:name, :price, :active, :short_description, :weight, :box_length, :box_width, :box_depth, :product_image)
end
end
My products#edit form (products#new doesn't work either, but it's the same form):
<%= form_for #product, html: { multipart: true } do |f| %>
<div class="form-inputs row">
<div class="form-group col-xs-9">
<%= f.label "Product Name" %>
<%= f.text_field :name, class: "form-control" %>
</div> <!-- form group -->
<div class="form-group col-xs-3">
<%= f.label :price %>
<%= f.text_field :price, class: "form-control", data: {autonumeric: true} %>
</div> <!-- form group -->
<div class="form-group col-xs-12">
<%= f.label "Product Description" %>
<%= f.text_area :short_description, class: "form-control" %>
</div> <!-- form group -->
<div class="form-group col-xs-12">
<%= f.file_field :image, as: :file %>
</div> <!-- form group -->
<div class="form-group text-center col-xs-12">
<p><%= f.check_box :active %> This is an active product.</p>
</div> <!-- form group -->
<div class="row">
<hr class="col-xs-6 col-xs-push-3" />
</div>
<div class="col-xs-12">
<h2 class="text-center">Shipping Information</h2>
</div>
<div class="form-group col-xs-6">
<%= f.label "Product Length (in Inches)" %>
<%= f.text_field :box_length, class: "form-control" %>
</div> <!-- form group -->
<div class="form-group col-xs-6">
<%= f.label "Product Width (in Inches)" %>
<%= f.text_field :box_width, class: "form-control" %>
</div> <!-- form group -->
<div class="form-group col-xs-6">
<%= f.label "Product Depth (in Inches)" %>
<%= f.text_field :box_depth, class: "form-control" %>
</div> <!-- form group -->
<div class="form-group col-xs-6">
<%= f.label "Product Weight (in Pounds)" %>
<%= f.text_field :weight, class: "form-control" %>
</div> <!-- form group -->
</div>
<div class="form-actions text-center">
<%= f.button :submit, class: "btn btn-manly" %>
</div>
<% end %>
And my relevant routes:
resources :products
And finally the product.rb model:
class Product < ActiveRecord::Base
has_many :order_items
has_attached_file :product_image
validates_attachment_content_type :product_image, content_type: /\Aimage\/.*\z/
validates_attachment_file_name :product_image, matches: [/png\z/, /jpe?g\z/]
default_scope { where(active: true) }
end
And the schema.rb for the products table:
create_table "products", force: :cascade do |t|
t.string "name"
t.decimal "price", precision: 12, scale: 3
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "short_description"
t.decimal "weight"
t.decimal "box_length"
t.decimal "box_width"
t.string "box_depth"
t.string "product_image_file_name"
t.string "product_image_content_type"
t.integer "product_image_file_size"
t.datetime "product_image_updated_at"
end
I am getting no errors when I submit. I have also tried both the simple_form_for and the form_for variations given in the Paperclip documentation. I have double checked and ImageMagick is installed. Can anyone see why it isn't saving? When I check the console after trying to upload an image it just says nil for all four paperclip fields.
You are using :image for file_field but your paperclip field is using product_image, change as follow to upload file
<div class="form-group col-xs-12">
<%= f.file_field :product_image, as: :file %>
</div> <!-- form group -->
Related
This started when I added the show actions in my podcasts controller. Here is the source code:
Any help would be greatly appreciated.
controllers:
class PodcastsController < ApplicationController
before_action :find_podcast, only: [:show]
def index
#podcasts = Podcast.all.order("created_at DESC")
end
def show
#podcast = Podcast.find(params[:id])
end
private
def find_podcast
if params[:id].nil?
#podcast = current_podcast
else
#podcast = Podcast.find(params[:id])
end
end
end
The podcast model
class Podcast < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
routes:
Rails.application.routes.draw do
resources :podcasts, only: [:index, :show]
devise_for :podcasts
root 'welcome#index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
The views
<h1> <%= #podcast.title %> </h1>
<p class="description"> <%= #podcast.description %> </p>
Actually, If I click the following links i.e sign up, sign in, edit, sign out
I get these errors
sign in: ActiveRecord::RecordNotFound in PodcastsController#show, Couldn't find Podcast with 'id'=sign_in
sign up: ActiveRecord::RecordNotFound in PodcastsController#show, Couldn't find Podcast with 'id'=sign_up
edit: ActiveRecord::RecordNotFound in PodcastsController#show, Couldn't find Podcast with 'id'=edit
sign out: ActiveRecord::RecordNotFound in PodcastsController#show, Couldn't find Podcast with 'id'=sign_out
The devise views:
**app/views/devise/registrations/new.html.erb**
<div class="sign_up_banner">
<h1>Create a podcast, it's 100% free.</h1>
</div>
<div id="form">
<div class="wrapper_skinny">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign Up", class: "button button_highlight button_block" %>
</div>
<% end %>
</div>
</div>
**app/views/devise/registrations/edit.html.erb**
<div id="podcast_show">
<div id="show_banner">
<div class="wrapper_skinny">
<h1>The Retrospective</h1>
</div>
</div>
<div id="links">
<div class="wrapper_skinny">
<li>Episodes </li>
<li> New Episodes </li>
<li> <%= link_to "Account Settings", edit_podcast_registration_path, class: "current" %> </li>
<li> View Podcast</li>
</ul>
</div>
</div>
</div>
<div id="form">
<div class="wrapper_skinny">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title, autofocus: true %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<h2>Links</h2>
<div class="field">
<%= f.label :itunes %><br />
<%= f.text_field :itunes %>
</div>
<div class="field">
<%= f.label :stitcher %><br />
<%= f.text_field :stitcher %>
</div>
<div class="field">
<%= f.label :podbay %><br />
<%= f.text_field :podbay %>
</div>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "new-password" %>
<% if #minimum_password_length %>
<br />
<em><%= #minimum_password_length %> characters minimum</em>
<% end %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "current-password" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
</div>
</div>
My schema.rb file
ActiveRecord::Schema.define(version: 2021_11_12_082327) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "podcasts", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "title"
t.text "description"
t.string "itunes"
t.string "stitcher"
t.string "podbay"
t.index ["email"], name: "index_podcasts_on_email", unique: true
t.index ["reset_password_token"], name: "index_podcasts_on_reset_password_token", unique: true
end
end
I have a fundings table and organisations table. Fundings table has organisation_id. In fundings table I have fields that are same with organisations table. What I want is when application is created and on funding show page there is a add button to add all fields that are same in fundings and organisations table gets stored in organisations table as well. Please help
_form.html.erb (funding_form)
<%= form_for([#parent, #child, #funding], :html => {class: "form-horizontal",role: "form"}) do |form| %>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :activity_details, class: "required" %>
</div>
<div class="col-sm-8">
<%= form.text_area :activity_details, required: true %>
</div>
</div>
<!--Adding organisation -->
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :name_of_organisation, id: "name_of_organisation-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :name_of_organisation, class: "form-control hidden", id: "name_of_organisation-textfield" %>
</div>
</div>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :address, id: "address-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :address, class: "form-control hidden", id: "address-textfield" %>
</div>
</div>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :city, id: "city-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :city, class: "form-control hidden", id: "city-textfield" %>
</div>
</div>
<div class = "form-group">
<div class="control-label col-sm-2">
<%= form.label :province, id: "province-label" %>
</div>
<div class="col-sm-8">
<%= form.text_field :province, class: "form-control hidden", id: "province-textfield" %>
</div>
</div>
<!-- End Adding organisation -->
<div class="form-group">
<div class="col-sm-10">
<%= form.submit "Apply", class: 'btn btn-primary btn-lg' %>
</div>
</div>
</div>
</div>
<% end %>
show.html.erb(funding show page)
<p>
<strong>Name of Organisation:</strong>
<%= #funding.name_of_organisation %></p><br>
<p>
<strong>Address:</strong>
<%= #funding.address %></p><br>
<p>
<strong>City:</strong>
<%= #funding.city %></p><br>
<p>
<strong>Province:</strong>
<%= #funding.province %></p><br>
<p>
<% if current_user.admin? %>
<%= link_to 'Add Organisation', "" %>
<% end %>
if admin clicks on add organisation fields get added to organisation table.
schema funding table
t.text "activity_details"
t.string "city"
t.string "name_of_organisation"
t.string "province"
t.text "address"
schema organisations table
t.string "name_of_organisation"
t.text "address"
t.string "city"
t.string "province"
funding.rb
belongs_to :organisation
organisation.rb
has_many :fundings
organisations controller
I have a new and create method to create organisation by the admin directly.so i have added a new_org method to get add the organisation from the funding table. But I am not able to find out how to implement it.
def new_org
#organisation = Organisation.new
end
def create_org
#organisation = Organisation.new(organisation_params)
if #organisation.save
flash[:success] = "Organisation is added"
redirect_to main_admin_service_provider_path
else
render 'new'
end
end
Something like this should work:
app/controllers/fundings_controller.rb
class FundingsController < ApplicationController
before_action :set_funding, on: %i[show add_organisation]
# ...
def update_organisation
return head :forbidden unless current_user.admin?
# assuming funding always has an organisation
whitelist = %w[your field names]
#funding.organisation.update!(#funding.attributes.slice(*whitelist))
redirect_to #funding, notice: 'fields successfully added to organisation'
end
# ...
private
def set_funding
#funding = Funding.find(params[:id])
end
end
config/routes.rb
Rails.application.routes.draw do
# ...
resources :fundings, only: :show do
# using patch because we are updating an existing resource
# but you can change this to whatever you like
patch 'update_organisation', on: :member
end
# ...
end
app/views/fundings/show.html.erb
<% if current_user.admin? %>
<%= link_to 'Add Organisation', update_organisation_funding_path(#funding), method: :patch %>
<% end %>
Alternatively you can update all double fields (without whitelisting) using the following code:
double_attribute_names = #funding.attribute_names & #funding.organisation.attribute_names
#funding.organisation.update!(#funding.attributes.slice(*double_attribute_names)
However keep in mind that this may produce unwanted results. For example some fields you don't want to update like 'id', 'created_at', 'updated_at' are most likely present on both instances, and maybe some custom double fields.
This can be resolved by creating a blacklist to exclude those fields:
blacklist = %w[id created_at updated_at]
double_attribute_names = #funding.attribute_names & #funding.organisation.attribute_names
#funding.organisation.update!(#funding.attributes.slice(*double_attribute_names).except(*blacklist))
But generally speaking it's better to whitelist.
This is not a good way to do it but you can define a hidden form with hidden fields with values of #funding.x on show page by defining
#organization = Organization.new in show method on Fundings.controller then you can simply post it as normal form. Or you can show this form to only admins.
Trace: https://i.gyazo.com/6487f4eee162e8c2207d7fdb5fc4ef3b.png
I can't get what's happening, I did the same process with the contact page and everything worked fine. Any ideas?
profile/new.html.erb
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="text-center">Create Your Profile</h1>
<p class="text-center">Be a part of the DevMatch community and fill out your profile!</p>
<div class="well">
<%= render 'form' %>
</div>
</div>
</div>
profile/_form.html.erb
<%= form_for #profile, url: user_profile_path do |f| %>
<div class="form-group">
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :job_title %>
<%= f.select :job_title, ['Developer', 'Entrepreneur', 'Investor'], {}, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :contact_email %>
<%= f.text_field :contact_email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.submit "Update Profile", class: 'btn btn-primary' %>
</div>
<% end %>
schema
create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.string "first_name"
t.string "last_name"
t.string "job_title"
t.string "phone_number"
t.string "contact_email"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Profiles controller
class ProfilesController < ApplicationController
# GET to /users/:user_id/profile/new
def new
# Render blank profile details form
#profile = Profile.new
end
end
I get the rror in the profile creator page. It says the firstname is undefined but I tried deleting it and then it says that the laast name is undefined and so on
If I delete the "<%= render 'form' %>" form the other html, the page loads perfectly, but I need a form and I am trying to learn Ruby. Sorry
undefined method `first_name' for Profile id: nil
It says the firstname is undefined but I tried deleting it and then it
says that the last name is undefined and so on
You didn't have columns in the profiles table. You should run rake db:migrate to migrate the columns which should resolve your problem.
Additionally always make sure your migrations are run properly without any errors You check the status of the pending migrations with rake db:migrate:status
I am working on an app where people can go to edit their user and upon submission it redirects them to their show profile view.
This all seems to work according to plan but for some reason some when I go back to my edit view I see that some of my form fields get automatically pre-populated while others don't.
Why is this happening?
Specifically the all text fields are being remembered and pre-populated but my image file field and time weekly fields are not. They are definitely still in the database and are displayed in my show view but not pre-populated on the edit view?
Do certain types of fields not get pre-populated or what is this behavior? I would ideally like to have all of the fields pre-populated(image, dates, text, etc)
Here is my code:
Edit view:
<div class="editprofilebox">
<h1>Take a moment to fill out your profile:</h1>
<div class="container-fluid">
<div class="edit-profile-form">
<%= form_for #user, :html => { :role => 'form', :class => 'form-horizontal', :multipart => true } do |f| %>
<div class="form-inputs">
<div class="row">
<div class= "col-sm-4">
<div class: "form-group">
<%= f.text_field :first_name, class: "form-control", placeholder:"First Name" %>
</div>
<div class: "form-group">
<%= f.text_field :last_name, class: "form-control", placeholder:"Last Name" %>
</div>
<div class: "form-group">
<%= f.label :profile_image, class: "control-label" %>
<%= f.file_field :image, class: "profile-picture-upload" %>
</div>
</div>
<div class= "col-sm-4">
<div class: "form-group">
<%= f.label :twitter %>
<%= f.text_field :twitter, class: "form-control", placeholder:"Type your update title here" %>
</div>
<div class: "form-group">
<%= f.text_field :occupation, class: "form-control", placeholder:"Occupation" %>
</div>
<div class: "form-group">
<%= f.text_field :gender, class: "form-control", placeholder:"Gender" %>
</div>
<div class: "form-group">
<%= f.text_field :work_history, class: "form-control", placeholder:"Work History" %>
</div>
<div class: "form-group">
<%= f.number_field :years_of_experience, class: "form-control", placeholder:"years_of_experience" %>
</div>
</div>
<h3> time available </h3>
<div class="col-sm-2">
<%= f.label :Monday %>
<%= f.check_box :monday, class: "time-checkbox", placeholder:"Type your update title here" %>
<%= f.time_field :mondaytime1, class: "form-control time-box"%>
<%= f.time_field :mondaytime2, class: "form-control time-box" %>
<%= f.label :Tuesday %>
<%= f.check_box :tuesday, class: "time-checkbox", placeholder:"Type your update title here" %>
<%= f.time_field :tuesdaytime1, class: "form-control time-box" %>
<%= f.time_field :tuesdaytime2, class: "form-control time-box" %>
<%= f.label :Wednesday %>
<%= f.check_box :wednesday, class: "time-checkbox" %>
<%= f.time_field :wednesdaytime1, class: "form-control time-box" %>
<%= f.time_field :wednesdaytime2, class: "form-control time-box" %>
<%= f.label :Thursday %>
<%= f.check_box :thursday, class: "time-checkbox" %>
<%= f.time_field :thursdaytime1, class: "form-control time-box" %>
<%= f.time_field :thursdaytime2, class: "form-control time-box" %>
</div>
<div class:"col-sm-2">
<%= f.label :Friday %>
<%= f.check_box :friday, class: "time-checkbox" %>
<%= f.time_field :fridaytime1, class: "form-control time-box" %>
<%= f.time_field :fridaytime2, class: "form-control time-box" %>
<%= f.label :Saturday %>
<%= f.check_box :saturday, class: "time-checkbox" %>
<%= f.time_field :saturdaytime1, class: "form-control time-box" %>
<%= f.time_field :saturdaytime2, class: "form-control time-box" %>
<%= f.label :Sunday %>
<%= f.check_box :sunday, class: "time-checkbox" %>
<%= f.time_field :sundaytime1, class: "form-control time-box" %>
<%= f.time_field :sundaytime2, class: "form-control time-box" %>
</div>
</div>
</div>
<div class="form-actions">
<%= f.button :submit, class: "btn btn-default btn-lg" %>
</div>
<% end %>
</div>
</div>
</div>
show view:
<div class="profilebox">
<div class="profile-title-box" >
<%= #user.first_name %> <%= #user.last_name %> profile
</div>
<hr>
<div class="profile-container">
<div class="row ">
<div class="profile-image-box col-sm-4" >
<%= image_tag #user.image.thumb('150x185#').url if #user.image_stored? %>
</div>
<div class="profile-info-box col-sm-8">
<%= #user.email %>
</br>
<%= #user.first_name %>
</br>
<%= #user.last_name %>
</br>
<%= #user.occupation %>
</br>
<%= #user.gender %>
</br>
<%= #user.work_history %>
</br>
<%= #user.years_of_experience %>
</br>
</div>
</div>
</div>
<hr>
<div class="profile-extra-box">
<h3>Complete your profile here:</h3>
<%= link_to 'Edit', edit_user_path(#user) %>
</div>
</div>
users controller:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
end
def index
end
def edit
#user = User.find(params[:id])
end
def update
#user = User.find_by_id(params[:id])
if #user.update_attributes(user_params)
flash[:success] = "User updated successfully!"
redirect_to user_path
else
flash[:danger] = "User could not be updated!"
end
end
def destroy
end
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :image, :twitter, :monday, :mondaytime1, :mondaytime2, :tuesday, :tuesdaytime1, :tuesdaytime2, :wednesday, :wednesdaytime1, :wednesdaytime2, :thursday, :thursdaytime1, :thursdaytime2, :friday, :fridaytime1, :fridaytime2, :saturday, :saturdaytime1, :saturdaytime2, :sunday, :sundaytime1, :sundaytime2, :occupation, :gender, :years_of_experience, :work_history)
end
end
Scheema:
ActiveRecord::Schema.define(version: 20140906225655) do
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "last_name"
t.string "image_uid"
t.string "image_name"
t.text "twitter"
t.boolean "monday"
t.text "mondaytime1"
t.text "mondaytime2"
t.boolean "tuesday"
t.text "tuesdaytime1"
t.text "tuesdaytime2"
t.boolean "wednesday"
t.text "wednesdaytime1"
t.text "wednesdaytime2"
t.boolean "thursday"
t.text "thursdaytime1"
t.text "thursdaytime2"
t.boolean "friday"
t.text "fridaytime1"
t.text "fridaytime2"
t.boolean "saturday"
t.text "saturdaytime1"
t.text "saturdaytime2"
t.boolean "sunday"
t.text "sundaytime1"
t.text "sundaytime2"
t.string "occupation"
t.string "gender"
t.string "work_history"
t.decimal "years_of_experience"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
"Pre-population" has two connotations, both of which you need to consider:
Browser-based data
Server-based data
Browser
The difference here is that browser based data is basically the "remember me" stuff you type into forms on e-commerce sites and the like.
The reason I mention this is because when you have a user form, modern browsers (exclusing IE) will generally populate it with the relevant data you have used before. This can be seen with this introduction to Autofill on Chrome's site:
In essence, it means that if you load standard "input names" on your pages, Chrome will endeavour to populate them with data you've either saved, or inputted into other websites.
Firstly, you need to make sure you are not having your details inputted by the browser. If this the is the case, it will mean you've got to get the server-side functionality working regardless.
Either way, you should look at using the server-based data as described below:
Server
Secondly, you'll have sever-based data. This is real Rails data, and what you need in your page:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def edit
#user = User.find params[:id]
end
end
#app/views/users/edit.html.erb
<%= form_for #user do |f| %>
<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= image_tag #user.image.url if #user.image.present? %>
<%= f.file_field :image %>
<%= f.submit %>
<% end %>
As per the form_for documentation, if you have the correct ActiveRecord object populated, and have the data in your database, calling the attribute-based input helpers should populate for you.
There are some caveats to this, however:
--
Image
Using file_field will not pre-populate your image.
The file upload element is distinctly different to the image element - simply that the upload element cannot show you an image. This means you have to explicitly show the image in your edit form, if indeed you want to show it:
# Edit View
<%= image_tag #user.image.thumb('150x185#').url if #user.image_stored? %>
<%= f.file_field :image %>
We've used this method here (just sign up for free and try to upload a profile image):
Although we used JQuery heavily here, we made it so that the image form shows the image, which then gives you the ability to upload a new one.
--
Time
Frankly, I'm not sure about your time field.
Like the explanation above, you'll want to ensure you're using the attributes from your database to populate your time fields. I see you're using a lot of different checkboxes, which although might help create a better system, will likely not populate the data you want
When I am sending new form, some fields aren't put in final map - they are just near by. Have no idea why is it happening...
View - sending
<%= form_for :review, :url => { :action => "show" } do |f| %>
<div class="field">
<%= f.label :owner %><br />
<%= f.text_field :owner %>
</div>
<div class="field">
<%= f.label :special %><br />
<%= f.check_box :special %>
</div>
<div class="field">
<%= f.label :overview %><br />
<%= text_area_tag(:overview, "", :size => "24x6") %>
</div>
<div class="field">
<%= label_tag(:service_overview, "overview") %>
<%= radio_button_tag(:service, "overview") %>
<%= label_tag(:service_repair, "repair") %>
<%= radio_button_tag(:service, "repair") %>
</div>
<div class="actions">
<%= f.submit "Next" %>
</div>
<% end %>
Migration
create_table :reviews do |t|
t.string :owner
t.string :service
t.string :overview
t.boolean :special
t.integer :cost
t.timestamps
end
edit - and of course:
Server log
Parameters: {"commit"=>"Next", "overview"=>"123123", "service"=>"repair", "re
view"=>{"special"=>"1", "owner"=>"aaaa"}, "authenticity_token"=>"HehoAEoQRvLwFL9
yZsbBXzy4PI9a53JjtfM4rqtMUt8=", "utf8"=>"Ôťô"}
Ok, I see, that in HTML there is difference between tags which are included in sent map and these, which aren't:
<input id="review_special" name="review[special]" type="checkbox" value="1">
<input id="service_overview" name="service" type="radio" value="overview">
but still no idea how to fix it...