Thank you in advance for any help provided. I've tried researching this a bunch but I can't get it to work.
When trying to render contacts/_new.html.erb inside of pages/home.html.erb
I get "First argument in form cannot contain nil or be empty"
I think it has something to do with which controller Rails is looking in.
Does Rails know to look in my ContactsController even though the main view is coming from the PagesController? I have tried many things and researched this a lot. I've tried locals and changing url and actions. It works when it is not in a partial. It works when I hard code form_for Contact.new.
Thanks again!
_new.html.erb
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit',class: 'btn btn-default' %>
<% end %>
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to root_path, notice: "Message sent."
else
redirect_to root_path, notice: "Error occured."
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
Render with:
<%= render new_contact_path %>
in the views/pages/home.html.erb
This is because #contact doesn't exist in your pages_controller.rb, and at the moment to load the contact variable in your form_for it throws this error.
You only have it defined in your contacts_controller but that's not being accessed when you load the pages/home view, it'll go to look for a #contact variable defined in your pages_controller specifically in your home method.
Try adding it in your pages_controller.rb as:
# app/controllers/pages_controller
def home
#contact = Contact.new
end
Related
I am getting Forbidden Attributes Error even though I have used strong parameters in Rails. I have two models: Posts and Categories.
Here is my Post Controller:
class PostsController < ApplicationController
def index
#posts=Post.all
end
def new
#post=Post.new
#category=Category.all
end
def create
#post = Post.new(params[:post])
if #post.save
redirect_to posts_path,:notice=>"Post saved"
else
render "new"
end
end
def allowed_params
params.require(:post).permit(:title, :body, :category_id)
end
end
And here is my view for posts/new:
<%= form_for #post do |f| %>
<p>
<%= f.label :title %></br>
<%= f.text_field :title%><br/>
</p>
<p>
<%= f.label :body %></br>
<%= f.text_area :body%><br/>
</p>
<p>
<%= f.select :category_id, Category.all.collect{|x| [x.name,x.id]},{:include_blank =>"Select one"}%><br/>
</p>
<p>
<%= f.submit "Add Post" %>
</p>
<% end %>
But I am still getting Error.
You need to use allowed_params instead of params[:post]:
#post = Post.new(allowed_params)
Rails 5. keep getting the error that route could not be found. What did I do wrong here?
App/views/contacts/new.html.erb
<%= form_for "#contact" do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
routes.rb file
Rails.application.routes.draw do
root to: 'pages#home'
get '/about', to: 'pages#about'
resources :contacts
end
contacts_controller.rb
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path, notice: "Message sent."
else
redirect_to new_contact_path, notice: "Error occured."
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
contact.rb (model file, blank for now)
class Contact < ActiveRecord::Base
end
I can't figure out why. Thanks in advance.
You have quotes around your #contact variable in your form, try just using the plain #contact variable.
I've been trying to setup a contact form but I keep getting errors and I just can't figure out what I'm doing wrong. The latest error I'm getting is:
NameError in Contacts#new
Showing .../app/views/contacts/new.html.erb where line #2 raised:
undefined local variable or method `contact' for #<#<Class:0x007fa2933ca1f8>:0x007fa29a4df460>
Did you mean? #contact
concat
Extracted source (around line #2):
1
2
3
4
5
6
<h1>Contact us</h1>
<%= form_for(contact) do |f| %>
<div class="field entry_box">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control entry_field" %>
</div>
My ContactsController
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to contacts_path, notice: "Thanks for contacting us!"
else
render :new
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :message)
end
end
Model
class Contact < ApplicationRecord
end
Routes (relevant parts)
resources :contacts, only: [:new, :create]
get "contact" =>'contacts#new'
post "contact" =>'contacts#create'
View (new.html.erb)
<h1>Contact us</h1>
<%= form_for(contact) do |f| %>
<div class="field entry_box">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control entry_field" %>
</div>
<div class="field entry_box">
<%= f.label :email %>
<%= f.number_field :email, class: "form-control entry_field" %>
</div>
<div class="field entry_box">
<%= f.label :message %>
<%= f.text_field :message, class: "form-control entry_field" %>
</div>
<div class="actions center space_big">
<%= f.submit "Submit", class: "btn btn-lg btn-success" %>
</div>
<% end %>
Thanks for any assistance!
Your controller defines an instance variable, #contact, but your view uses contact. There's a difference - make sure your form_for call uses the #contact variable that your controller defines.
You have:
<%= form_for(contact) do |f| %>
which you should change to
<%= form_for(#contact) do |f| %>
because of this line in your controller
#contact = Contact.new
In most errors, Rails will give you the source code and line number that caused the exception, and will then give you a hint - in this case, it says "did you mean #contact?"
The variable contact is undefined in new.html.erb. See the suggestion made by rails to use #contact?
The line
<%= form_for(contact) do |f| %>
should be
<%= form_for(#contact) do |f| %>
The instance variable #contact is passed on to the view from the new action of ContactsController.
You have to write instance variable in new form just write
<%= form_for(#contact) do |f| %>
<% end %>
I'm trying to solve a really simple problem with my code. I want to upload image into a post, I use paperclip, and the last step is not working.
That is my controller :
class PostsController < ApplicationController
def index
#posts = Post.all
end
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
flash[:success] = "uccess!"
redirect_to post_path(#post)
else
flash[:error] = #post.errors.full_messages
redirect_to new_post_path
end
end
def show
#post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :image, :prix, :adress, :description)
end
end
that my form :
<%= simple_form_for #post, url: root_path do |f|%>
<%= f.input :title, label: "Nom du plat" %>
<br>
<%= f.input :image, as: :file %>
<br>
<%= f.input :prix %>
<%= f.input :adress, label: "Localisation" %>
<%= f.input :description %>
<br>
<%= f.button :submit %>
<% end %>
and that my view :
<%= image_tag (#post.image.url(:medium)) %>
<br>
<%= #post.description %>
<br>
<button>
<%= link_to "Home", root_path %>
</button>
So if you go through, and you spot a stupid mistake, please let me know.
You're calling root_path as the helper in your form when you need to call the route for post. Run rake routes and look for the create action related to post, which should be the same as the get route for the show action.
This should not be root_path:
<%= simple_form_for #post, url: root_path do |f|%>
I'm building a simple blog tool using rails and having trouble with variables displaying from a form. What is most confusing is that the post times are showing correctly, but the title and text aren't coming through.
My new form page looks like:
<h1>New post</h1>
<%= form_for :blog, url: blogs_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The controller is currently:
class BlogsController < ApplicationController
def index
end
def new
end
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
def show
#blog = Blog.find(params[:id])
end
private
def post_params
params.require(:blog).permit(:title, :body)
end
end
And the show page is:
<div id="blog">
<h1>
<%= #blog.title %>
</h1>
<p class="date">
Submitted <%= time_ago_in_words(#blog.created_at) %> ago
</p>
<p>
<%= #blog.body %>
</p>
</div>
Any ideas?
Change:
def create
#blog = Blog.new(post_params[:id])
#blog.save
redirect_to #blog
end
to:
def create
#blog = Blog.new(post_params)
#blog.save
redirect_to #blog
end
The reason why it wasn't working is that you were only trying to savepost_params[:id]. You need to pass the whole param as an argument when creating a new blog post Blog.new(post_params)