Nested form validation in Rails - ruby-on-rails

I have the following code:
<%= form_with(model: [#lawsuit, #lawsuit.suits.build]) do |f| %>
<fieldset>
<legend>New Suit</legend>
</fieldset>
<br />
<div class="form-group">
<%= f.label :claim %>
<%= f.text_field :claim, class: 'form-control', placeholder: 'Name' %>
</div>
<div class="form-group">
<%= f.label :sentence %>
<%= f.text_area :sentence, class: 'form-control', placeholder: 'Sentence' %>
</div>
<div class="form-group">
<%= f.label :result %>
<%= f.select(:result, [['Not Favorable', false], ['Favorable', true]], {}, {class: 'form-control'}) %>
</div>
<%= button_tag type: 'submit', class: "btn btn-primary float-right" do %>
<i class="fa fa-plus" aria-hidden="true"></i>
Create
<% end %>
<% end %>
How can I show the list of errors of the suit (which is a nested attribute of #lawsuit) and show it's errors on the screen ? I have already done the validations on the model. The model is like that:
class Suit < ApplicationRecord
belongs_to :lawsuit
validates_presence_of :claim, :sentence
end
My controllers are like below.
The process start in lawsuit controller. There I build the #suit (which is used in the form).
Suit controller:
class SuitsController < ApplicationController
before_action :set_suit, only: [:show]
def new
end
def create
Rails.logger.info "=====================SUIT CREATION"
#lawsuit = Lawsuit.find(params[:lawsuit_id])
#suit = #lawsuit.suits.build(suit_params)
Rails.logger.info "AISHA #{#suit.errors.any?}"
# #suit = #lawsuit.suits.new(suit_params)
if #suit.save
flash[:notice] = "Suit created successfully"
redirect_to lawsuit_path(#lawsuit)
else
Rails.logger.info "AISHA #{#suit.valid?}"
flash[:alert] = "Something went wrong"
redirect_to lawsuit_path(#lawsuit)
end
end
Lawsuit Controller
class LawsuitsController < ApplicationController
before_action :set_lawsuit, only: [:show]
def index
#lawsuits = Lawsuit.paginate(:page => params[:page], :per_page => 8)
end
def show
begin
#blob = Lawsuit.get_blob_for_document(#lawsuit.document_number)[1]
rescue
#blob = "Cannot load document!"
flash[:error] = "Cannot load document!"
end
#lawsuit = Lawsuit.find(params[:id])
#suit = #lawsuit.suits.build
end
private
def set_lawsuit
#lawsuit = Lawsuit.find(params[:id])
end
def lawsuit_params
params.require(:lawsuit).permit(:document_number, :region, :court, :noted)
end
end

Modify view as follows
<%= form_with(model: [:lawsuit, #suit]) do |f| %>
<fieldset>
<legend>New Suit</legend>
</fieldset>
<br />
<% if #suit.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(#suit.errors.count, "error") %>
<%= "prohibited this suit from being saved:" %>
</h2>
<ul></ul>
<% #suit.errors.full_messages.each do |message| %>
<li>
<%= message %>
</li>
<% end %>
</div>
<% end %>
<div class="form-group">
<%= f.label :claim %>
<%= f.text_field :claim, class: 'form-control', placeholder: 'Name' %>
</div>
<div class="form-group">
<%= f.label :sentence %>
<%= f.text_area :sentence, class: 'form-control', placeholder: 'Sentence' %>
</div>
<div class="form-group">
<%= f.label :result %>
<%= f.select(:result, [['Not Favorable', false], ['Favorable', true]], {}, {class: 'form-control'}) %>
</div>
<%= button_tag type: 'submit', class: "btn btn-primary float-right" do %>
<i class="fa fa-plus" aria-hidden="true"></i>
Create
<% end %>
<% end %>
note you should build #suit object from controller action first
#suit = #lawsuit.suits.build
and you controller should be
class SuitsController < ApplicationController
before_action :set_suit, only: [:show]
def new
end
def create
#lawsuit = Lawsuit.find(params[:lawsuit_id])
#suit = #lawsuit.suits.build(suit_params)
if #suit.save
flash[:notice] = "Suit created successfully"
redirect_to lawsuit_path(#lawsuit)
else
render :form
end
end
Or
If you want to display flash message then you should write following code on view
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>

Related

How does my show page need to be written in order to display data?

I am a newbie to the world of Ruby on Rails and I am developing a form that is dropdown in nature for the first time and you can see the code here in _form.html.erb:
<%= simple_form_for(#job, html: {class: 'form-horizontal'}) do |f| %>
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Category:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Landscaping','Landscaping'],
['Babysitting','Babysitting'],['Tree planting','Tree planting']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Location:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Dorchester','Dorchester'],
['Roxbury','Roxbury'],['Mattapan','Mattapan']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Status:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['New','New'],
['Pending','Pending'],['Complete','Complete']])) %>
</div>
</div>
<%= f.input :description, label: "Job Description" %>
<%= f.submit 'Add Job', class: 'btn btn-default' %>
<% end %>
When I click on 'Add Job', it takes me to a blank screen which I have not been able to resolve on my own.
I have changed the code around a view times in the views/show.html.erb. I have had this:
<h1><%= #job.poster %></h1>
<p><%= #job.category %></p>
<p><%= #job.location %></p>
and this:
<tbody>
<% #job.each do |job| %>
<tr>
<td><%= job.poster %></td>
</tr>
<% end %>
</tbody>
and I continue to get a blank screen or if I went to my jobs_controller.rb and developed it like this:
class JobsController < ApplicationController
before_action :find_job, only: [:show, :edit, :update, :destroy]
def index
# #job = Job.all
end
def show
#job = Job.find_job(jobs_params[:id])
end
def new
#job = Job.new
end
def create
#job = Job.new(jobs_params)
if #job.save
redirect_to #job, notice: 'Your job was successfully added'
else
render "New"
end
end
def edit
end
def update
end
def destroy
end
private
def jobs_params
params.require(:job).permit(:poster, :category, :location, :status, :description)
end
def find_job
#job = Job.find(params[:id])
end
end
I would get an undefined method for 'poster' for ActiveRecord object, but if I remove the code in the show action, I just get a blank page. I am out of solutions here and I have looked on SO, but nothing resembled a solution for me.
My routes.rb file looks like this:
Rails.application.routes.draw do
resources :jobs
root 'jobs#index'
end
I changed the show action to reflect this:
def show
#job = Job.find(jobs_params[:id])
end
then I got a param is missing or the value is empty: job.
I did a rails console and I have this:
Job Load (1.1ms) SELECT "jobs".* FROM "jobs" LIMIT ? [["LIMIT", 11]] => #<ActiveRecord::Relation [#<Job id: 1, poster: nil, category: nil, location: nil, status: "New", description: "Your job is to plant trees all over the Dorchester...", created_at: "2017-08-01 10:29:40", updated_at: "2017-08-01 10:29:40">]>
I don't understand why I have nil on everything except description.
It seems that every form input like this:
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
Gave me a nil in the rails console and only this one:
<%= f.input :description, label: "Job Description" %>
successfully inputted data, so something is wrong with the code for f.select, but I am not sure what.
change this
def show
#job = Job.find_job(jobs_params[:id])
end
to this
def show
end
The #job record is retrieved for you by the before_action
And use this in the show.html.erb
<h1><%= #job.poster %></h1>
<p><%= #job.category %></p>
<p><%= #job.location %></p>
Try to Replace your _form.html.erb with
<%= simple_form_for(#job, html: {class: 'form-horizontal'}) do |f| %>
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:poster, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Category:", class: 'control-label' %>
<div class="controls">
<%= f.select(:category, options_for_select([['Landscaping','Landscaping'],
['Babysitting','Babysitting'],['Tree planting','Tree planting']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Location:", class: 'control-label' %>
<div class="controls">
<%= f.select(:location, options_for_select([['Dorchester','Dorchester'],
['Roxbury','Roxbury'],['Mattapan','Mattapan']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Status:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['New','New'],
['Pending','Pending'],['Complete','Complete']])) %>
</div>
</div>
<%= f.input :description, label: "Job Description" %>
<%= f.submit 'Add Job', class: 'btn btn-default' %>
<% end %>
In controller
def show
end
show.html.erb
<h1><%= #job.poster %></h1>
<p><%= #job.category %></p>
<p><%= #job.location %></p>
You got
Job id: 1, poster: nil, category: nil, location: nil, status:
"New", description: "Your job is to plant trees all over the
Dorchester..."
because by mistek you have used :status in every select in _form.html.erb
Remove this line from the show action in your JobsController :
#job = Job.find_job(jobs_params[:id])

unable to do validations in rails

I have a contact us page
class ContactPagesController < ApplicationController
def new
#contact_page = ContactPage.new
end
def create
#contact_page = ContactPage.new(contact_page_params)
if #contact_page.save
flash[:notice]="details saved successfully"
redirect_to contact_pages_path
else
flash[:notice]="details not saved"
redirect_to contact_pages_path
end
# if #contact_page.errors.any?
# flash[:notice]="there are errors"
# end
end
private
def contact_page_params
params.require(:contact_page).permit( :first_name, :last_name, :email, :phone, :do_you_have_land, :moving_time_frame, :financing, :to$
end
end
and my model
class ContactPage < ApplicationRecord
validates :email, presence: true
end
and the view
new.html.erb
<%= form_for #contact_page, :html => { :class => 'form-horizontal', multipart: true } do |f| %>
<% if #contact_page.errors.any? %>
<div id="error_explanation">
<h3>
<%= pluralize(#contact_page.errors.count, "error") %>
prohibited this employee from being saved:
</h3>
<ul>
<% #contact_page.errors.full_messages.each do |message| %>
<li>
<%= message %>
</li>
<% end %>
</ul>
</div>
<% end %>
<div class="control-group">
<%= f.label :First_Name, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :first_name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :Email, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :email, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :How_can_we_assist_you, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :assist_you, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
</div>
<% end %>
If all the fields are filled the data is being saved into the database.If email is left blank data is also not being saved inside the database(due to validation), but the error message is also not rendering . The #contact_page.errors.any? inside the view page is being ignored for some reason.I am expecting an error "email can't be blank" on the top of the form.
why the save action is not triggering the validation errors?
my routes for contact page
get 'contact_pages' => 'contact_pages#new', :as => 'contact_pages'
post 'contact_pages' => 'contact_pages#create'
Need help in what I have gone wrong.Any help is highly appreciated.Thanks in advance.
class SaveController < ApplicationController
def new
#contact_page = ContactPage.new
end
def create
if
#contact_page.save
flash[:notice]="Successfuly stored"
redirect_to contact_pages_path
else
flash[:notice]="please check your inputs"
render :new
end
end
end
In View: -
<% if notice %>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<%= notice %>
</div>
<% end %>
you should user render on error not redirect
def create
#contact_page = ContactPage.new(contact_page_params)
if
#contact_page.save
flash[:notice]="details saved successfully"
redirect_to contact_pages_path
else
flash[:notice]="details not saved"
render :new
end
end

Rails - How to embed a wistia video dynamically through a form

I am making an e-learning platform and have a Course resource so I can add courses dynamically. I am now at the stage where I need to upload videos to each course, and I have decided to use wistia for this.
What I'm not sure about exactly is how can I Embed the videos that belong to each course directly through the Course form? I couldn't find documentation as to how to achieve this, so any help would be highly appreciated.
Example:-
I have this embed code from wistia:
<iframe src="//fast.wistia.net/embed/iframe/tk6picrt4k" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="640" height="388"></iframe>
<script src="//fast.wistia.net/assets/external/E-v1.js" async></script>
Once I make a :video_url table in the database, how do I then make the above code work as intended if I paste it in a form box in the Create Course action and submit the form? and What data type should :video_url be?
Courses Controller:-
class CoursesController < ApplicationController
before_action :set_course, only: [:show, :edit, :update, :destroy]
before_action :set_grade
respond_to :html
def index
#grades = Grade.all
#courses = Course.where grade: params[:grade_id]
respond_with(#course)
end
def show
respond_with(#course)
end
def new
#grade = Grade.find(params[:grade_id])
#course = #grade.courses.new
respond_with(#course)
end
def edit
end
def create
#grade = Grade.find(params[:grade_id])
#course = #grade.courses.build(course_params)
if #course.save
redirect_to grade_courses_path(#grade, #courses), notice: 'Successfully Created Course'
else
render action: 'new'
end
end
def update
#course.update(course_params)
redirect_to grade_courses_path(#grade, #courses), :notice => "Successfully Updated Course"
end
def destroy
#course.destroy
redirect_to grade_courses_path(#grade, #courses), :notice => "Successfully Deleted Course"
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
#course = Course.find(params[:id])
end
def set_grade
#grade = Grade.find(params[:grade_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:title, :period, :description, :semester, :grade, :subject, :grade_id, :chapters_from, :chapters_to, :instructor, :price)
end
end
_form.html.erb
<%= bootstrap_form_for [#grade, #course] do |f| %>
<% if #course.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#course.errors.count, "error") %> prohibited this course from being saved:</h2>
<ul>
<% #course.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.select :subject, [["Math"], ["Physics"], ["Chemistry"], ["Science"], ["Biology"], ["English"], ["Arabic"], ["Social Studies"]], { label: "Subject"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_field :instructor %>
</div>
<div class="field">
<%= f.select :period, [["First"], ["Second"], ["Third"], ["Final"]], { label: "Period"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.select :semester, [["First"], ["Second"]], { label: "Semester"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.number_field :chapters_from %>
</div>
<div class="field">
<%= f.number_field :chapters_to %>
</div>
<div class="field">
<%= f.number_field :price %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Inside your controller we have to permit the new attribute video_url to be set. Rails will handle updates/creates for it on its own:
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:title, :period, :description, :semester, :grade, :subject, :grade_id, :chapters_from, :chapters_to, :instructor, :price, :video_url)
end
Inside your view where you have the static iframe:
...
<iframe src="#{#course.video_url}" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen mozallowfullscreen webkitallowfullscreen oallowfullscreen msallowfullscreen width="640" height="388"></iframe>
...
This will populate your view with the given video_url. Note, there's no exception handling if the video_url is NIL.
Your video_url should be of datatype :string. That's sufficient for a simple URL :)
Considering you already have a form to submit data for your course (To create, edit, update the course with information) you will just have to add another input field for the URL (video_url).
Now that you provided the form, we can just add another input:
<%= bootstrap_form_for [#grade, #course] do |f| %>
<% if #course.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#course.errors.count, "error") %> prohibited this course from being saved:</h2>
<ul>
<% #course.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.select :subject, [["Math"], ["Physics"], ["Chemistry"], ["Science"], ["Biology"], ["English"], ["Arabic"], ["Social Studies"]], { label: "Subject"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_field :instructor %>
</div>
<div class="field">
<%= f.select :period, [["First"], ["Second"], ["Third"], ["Final"]], { label: "Period"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.select :semester, [["First"], ["Second"]], { label: "Semester"}, { class: "selectpicker" } %>
</div>
<div class="field">
<%= f.number_field :chapters_from %>
</div>
<div class="field">
<%= f.number_field :chapters_to %>
</div>
<div class="field">
<%= f.number_field :price %>
</div>
<div class="field">
<%= f.text_field :video_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
That should be it!
If you already have a form as well as an existing controller, hit us up with more code and I will gladly edit my answer to give you more specific input.
So far, this should be it and enough for you to work with :)
--- If you still need something else, let us know.

Rails: Two different NoMethodError's when trying to display files

I'm making a basic application and I made it so a user can attach a file to a form post. That all works perfectly fine, but now I'm trying to display a link to the file and it doesn't seem to work.
I'm getting two errors. One if I attach a file and another if I don't attach a file. They both say undefined method 'doc=' for nil:NilClass but are on different lines of code.
If I don't upload a file this is what I get: NoMethodError in Projects#index on this line of code <% if #project.doc %>.
If I do upload a file this is what I get: NoMethodError in ProjectsController#create on this line of code #project.doc = uploaded_io.original_filename
projects_controller.rb
class ProjectsController < ApplicationController
def index
#projects = Project.all
end
def show
end
def new
#projects = Project.new
end
def create #no view
#projects = Project.new(project_params)
uploaded_io = params[:doc]
if uploaded_io.present?
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
#project.doc = uploaded_io.original_filename
end
end
if #projects.save
redirect_to projects_path, :notice => "Your project was sent!"
else
render "new"
end
end
def edit
#projects = Project.find(params[:id])
end
def update #no view
#projects = Project.find(params[:id])
if #projects.update_attributes(project_params)
redirect_to projects_path, :notice => "Your project has been updated."
else
render "edit"
end
end
def destroy #no view
#projects = Project.find(params[:id])
#projects.destroy
redirect_to projects_path, :notice => "Your project has been deleted."
end
private
def project_params
params.require(:project).permit(:title, :description)
end
end
index.html.erb
<div class="container">
<div class="page-header">
<h1>Projects<small> Here are all of your projects.</small></h1>
</div>
</div>
<% #projects.each do |project| %>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<%= project.title %>
</div>
</div>
<div class="panel-body">
<p>
<%= project.description %>
</p>
<br>
<%= link_to "Discuss", new_project_discussion_path(project) %> |
<%= link_to "Tasks", new_project_task_path(project) %> |
<%= link_to "Edit", edit_project_path(project) %> |
<%= link_to "Delete", project, :method => :delete %>
<% if #project.doc %>
<p>Document: <%= link_to #project.doc, "/uploads/#{#project.doc}", :target => "_blank" %></p>
<% end %>
</div>
</div>
<%end%>
<br>
<br>
<div class="container">
<p><a class="btn btn-primary btn-lg" href="/projects/new" role="button">Create project</a></p>
</div>
_form.html.erb
<%= form_for(#projects, :html => { :multipart => true}) do |f| %>
<% if #projects.errors.any? %>
<ul>
<% #projects.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<div class="container">
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<%= label_tag :doc, 'Files (optional)' %>
<%= file_field_tag :doc %>
<br>
<div class="form-group">
<%= f.submit "Submit Project", class: "btn btn-primary" %>
</div>
<% end %>
Updated :
You have many errors, here are a few that I found :
uploaded_io = params[:doc]
Should be
uploaded_io = params[:project][:doc]
Also delete this line
#project.doc = uploaded_io.original_filename
You don't need that.
Finally, in your views, you should have project.doc instead of #project.doc

Submit button does not work unless I refresh the page form_for Rails

I have an app that has a problem model and when I go to create a record the submit button does nothing. No errors given it just simply doesnt execute, unless I refresh the page and attempt add it again. The same happens when I go to update a record.
Here is my controller
class ProblemsController < ApplicationController
include Concerns::Votes
def index
#problems = Problem.all
end
def show
#problem = find_problem
end
def new
#problem = Problem.new
end
def edit
#problem = find_problem
end
def create
#problem = current_user.problems.new(problem_params)
#problem.save
redirect_to #problem
end
def update
#problem = find_problem
if #problem.update_attributes(problem_params)
redirect_to #problem
else
redirect_to #problem
end
end
private
def find_problem
#problem = Problem.find(params[:id])
end
def problem_params
params.require(:problem).permit(:name, :description, :url)
end
end
Here is my _form.html.erb partial that I am rendering on new.html
<div class="row">
<div class="large-12 columns">
<%= form_for #problem do |f| %>
<label>Name</label>
<%= f.text_field :name, placeholder: "Name your problem!" %>
</div>
<div class="large-8 columns">
<%= f.text_field :url, placeholder: "Link to any supporting material" %>
</div>
<div class="large-12 columns">
<%= f.text_area :description %>
</div>
<div class="large-12 columns">
<%= f.submit "Create" %>
</div>
</div>
<% end %>
I have resources :problems in my routes.
Here for good measure is my show.html.erb as well.
<%= div_for #problem do %>
<%= link_to 'Edit', edit_problem_path(#problem) %>
<h2><%= #problem.name %> (<%= #problem.cached_votes_score %>)</h2>
<a =href"<%= #problem.url %>"><%= #problem.url %></a>
<p><%= #problem.description %><p>
By <%= #problem.user.name %></br>
<a class="button"<%= link_to 'Up', {:controller => 'problems', :action => 'up_vote'}, {:method => :post } %></a>
<a class="button"<%= link_to 'Down', {:controller => 'problems', :action => 'down_vote'}, {:method => :post } %></a>
<%= link_to 'Edit', edit_problem_path(#problem) %> |
<%= link_to 'Back', problem_path %>
<% end %>
Here is my index.html.erb
<div class="row">
<div class="large-12 columns">
<% #problems.each do |problem| %>
<h1><small><%= problem.cached_votes_score %></small> <%= link_to problem.name, problem %></h1>
<% end %>
</div>
<%= link_to 'New Problem', new_problem_path %>
</div>
I really cant understand why it works if i refresh the page but otherwise it doesnt work at all.
Your HTML is invalid, the submit button is actually not nested under the form tag. Try changing your view code to this:
<div class="row">
<div class="large-12 columns">
<%= form_for #problem do |f| %>
<label>Name</label>
<%= f.text_field :name, placeholder: "Name your problem!" %>
<div class="large-8 columns">
<%= f.text_field :url, placeholder: "Link to any supporting material" %>
</div>
<div class="large-12 columns">
<%= f.text_area :description %>
</div>
<div class="large-12 columns">
<%= f.submit "Create" %>
</div>
<% end %>
</div>
</div>
I had same issue
Before
<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.button :submit, class: 'pull-right' %>
<%end%>
</div>
</div>
After
<%= simple_form_for(:schedule_list, url: schedulelists_create_with_block_path, :html => { novalidate: false}) do |f| %>
<div class="row">
<div class="col-md-12">
<%= f.button :submit, class: 'pull-right' %>
</div>
</div>
<%end%>

Resources