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...
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'm trying to create an event, the event has a date and a time field. When creating the table for protest I didn't add a time field, so I had to run a migration to add a time field. But I am now having issues when creating the form. What is the elegant solution to create a time and date field?
class CreateProtests < ActiveRecord::Migration[5.1]
def change
create_table :protests do |t|
t.string :name
t.text :description
t.string :location
t.datetime :starts_at
t.references :creator, index: true, foreign_key: { to_table: :users }
end
end
end
I then added a field for the time of day:
class AddStartsAtTimeOfDayToProtests < ActiveRecord::Migration[5.1]
def change
add_column :protests, :starts_at_time_of_day, :datetime
end
end
protests/new.html.erb
<div class="col-md-12">
<div class="card">
<div class="card-header"><h2>Create A Protest</h2></div>
<div class="card-block">
<%= form_for(#protest, url: protests_path, html: {multipart: true}) do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :location %>
<%= f.text_field :location, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label "Date:" %>
<%= f.text_field :starts_at, value: "02/16/13", id: "dp2"%>
</div>
<div class="form-group">
<%= f.label "Time:" %>
<%= f.time_select :starts_at, ignore_date: true, class: "form-control"%>
</div>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, as: :file, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Create", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
The Date and Time are both captured in the single column :starts_at so you don't need another column for the time of day.
Your form should just have:
<div class="form-group">
<%= f.label "Starts At:" %>
<%= f.datetime_select :starts_at, class: "form-control"%>
</div>
Here is a link to the docs:
https://apidock.com/rails/ActionView/Helpers/FormBuilder/datetime_select
add date_field for Date and time_field for the time to have the two fields as follows.
<div class="form-group">
<%= f.label "Date:" %>
<%= f.date_field :starts_at, class: "form-control"%>
</div>
<div class="form-group">
<%= f.label "Time:" %>
<%= f.time_field :starts_at, class: "form-control"%>
</div>
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
Good Morning,
I have two scaffolds person and city.
rails g scaffold person :name, city_id
rails g scaffold city :cityname
and one formular view/people/
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :city_id %><br />
<%= f.text_field :city_id %>
</div>
<div class="field">
<%= f.label :cityname%><br />
<%= f.text_field :cityname%>
</div>
it doesnt go, i have to create an object of city and put the id in the city_id hidden_field and after that after create-button selected all should be saved in the database.
Not too hard or? Who would you make this?
No need to pass city_id.
<%= form_tag url_for(:controller => :your_controller, :action => :some_action, :method => :get do %>
<div class="field">
<%= label_tag :name %><br />
<%= text_field_tag :name %>
</div>
<div class="field">
<%= label_tag :cityname%><br />
<%= text_field_tag :cityname%>
</div>
<% end %>
In controller:
def some_action
city = City.find_or_create_by_cityname(params[:cityname])
person = Person.new(params[:name])
person.city_id = city.id
person.save!
end
I followed this screencast to make a nested model form.
http://railscasts.com/episodes/196-nested-model-form-part-1
Now, I am trying my application to include such a nested model form but I received a argument error (wrong number of arguments(0 for 1)).
I can't seem to figure out where I went wrong and would like to seek some advice on what I could try out and why some an error might have occurred.
The error happens on this line in the Subject Model.
has_many :lessons, :dependent => destroy
The other relevant codes:
Subjects Controller:
def new
#subject = Subject.new
#3 times one for lecture one for lab one for tut.
3.times{#subject.lessons.build}
respond_to do |format|
format.html # new.html.erb
format.json { render json: #subject }
format.js
end
end
Subject Model
class Subject < ActiveRecord::Base
has_many :lessons, :dependent => destroy
attr_accessible :lesson_attributes, :acad_unit, :cohort_size, :discipline, :remarks, :subject_code, :subject_name, :year_of_study
accepts_nested_attributes_for :lessons, :reject_if => lambda { |a| a[:lesson_type].blank? }, :allow_destroy => true
end
Lesson Model
class Lesson < ActiveRecord::Base
belongs_to :subject
attr_accessible :frequency, :lesson_type, :no_of_lesson, :possible_venues
end
_form.html.erb
<%= form_for(#subject,:remote=>true) do |f| %>
<% if #subject.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#subject.errors.count, "error") %> prohibited this subject from being saved:</h2>
<ul>
<% #subject.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :subject_code %><br />
<%= f.text_field :subject_code %>
</div>
<div class="field">
<%= f.label :subject_name %><br />
<%= f.text_field :subject_name %>
</div>
<div class="field">
<%= f.label :year_of_study %><br />
<%= f.text_field :year_of_study %>
</div>
<div class="field">
<%= f.label :discipline %><br />
<%= f.text_field :discipline %>
</div>
<div class="field">
<%= f.label :acad_unit %><br />
<%= f.text_field :acad_unit %>
</div>
<div class="field">
<%= f.label :cohort_size %><br />
<%= f.text_field :cohort_size %>
</div>
<div class="field">
<%= f.label :remarks %><br />
<%= f.text_field :remarks %>
</div>
<ol>
<%= f.fields_for :lessons do |builder| %>
<%= render "lesson_fields", :f => builder %>
<% end %>
</ol>
<% end %>
_lesson_fields.html.erb
<p>
<div class="field">
<%= f.label :lesson_type %><br />
<%= f.text_field :lesson_type %>
</div>
<div class="field">
<%= f.label :no_of_lesson %><br />
<%= f.text_field :no_of_lesson %>
</div>
<div class="field">
<%= f.label :frequency %><br />
<%= f.text_field :frequency %>
</div>
<div class="field">
<%= f.label :possible_venues %><br />
<%= f.text_field :possible_venues %>
</div>
</p>
I guess you wanted :
has_many :lessons, :dependent => :destroy
?
Here you'll find a nice discussion on topic.