Rails 4 - ActiveModel::ForbiddenAttributesError - ruby-on-rails

I have read other issues about it, but still I can't point what is causing the error. I have defined the strong parameters of Rails 4, but it keeps showing the error:
ActiveModel::ForbiddenAttributesError in MessagesController#create
My view is this:
<%= form_for(#message) do |f| %>
<div class="form-group field">
<%= f.label :phrase %>
<br/>
<%= f.text_field :phrase, autofocus: true, class: 'form-control' %>
</div>
<div class="form-group field">
<%= f.label :date %>
<br/>
<%= f.date_field :date, class: 'form-control' %>
</div>
<div class="actions text-center">
<%= f.submit "Submit", class: 'btn btn-default' %>
</div>
<% end %>
My controller:
class MessagesController < ApplicationController
def today
#dates = Message.all()
end
def history
#messages = Message.history_checker
end
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.save
flash[:notice] = "OK"
redirect_to root_path
else
render :action => 'new'
end
end
private
def message_params
params.require(:message).permit(:phrase,:date)
end
end
The error points to line 15 of controller #message = Message.new(params[:message]). Any ideas?

You just need to use message_params instead of params[:message]:
#message = Message.new(message_params)

Related

After hitting send button the form submits but how do I redirect to new page in rails?

Here's my form code. After hitting send I'd like to see a confirmation message or redirect to a different page.
<%= flash[:notice]
<%= form_with(url: "/static_pages/thank_you") do |form| %>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control", placeholder: "Enter Name" %>
</div>
<div class="form-group">
<%= form.label :email, "Email Address" %>
<%= form.text_field :email, class: "form-control", placeholder: "Enter Email" %>
</div>
<div class="form-group">
<%= form.label :message %>
<%= form.text_area :message, class: "form-control", placeholder: "Enter Message" %>
</div>
</div>
</div>
<%= form.submit "Send" %>
<% end %>
Here is my controller code or at least the start of it. I'm suspecting I'm missing something on the page below.
class StaticPagesController < ApplicationController
def index
##products = Product.all
end
def landing_page
#products = Product.limit(4)
end
def contact
end
def about
end
def thank_you
#name = params[:name]
#email = params[:email]
#message = params[:message]
UserMailer.contact_form(#email, #name, #message).deliver_now
end
end
def thank_you
#name = params[:name]
#email = params[:email]
#message = params[:message]
UserMailer.contact_form(#email, #name, #message).deliver_now
flash[:notice] = "Mail sent successfully!"
redirect_to root_path
end

THE SUBMIT BUTTON

Udemy: Bootcamp course: section: 8 Lecture: 122: SAVING TO THE DATABSE: In the "Contact Us" page I filled in the info, Name: test1, Email: test1#example.com, Comments: test1, as a test, pressed submit, but the button do not submit the info and gives no error message. Where am I going wrong? Any expert advice? Here is my code:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
flash[:success] = 'Message sent.'
redirect_to new_contact_path
else
flash[:danger] = 'Error occured, message has not been sent.'
redirect_to new_contact_path
end
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well"
<%= 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.email_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 %>
</div>
</div>
</div>
Im not a rails expert but you can try <%= form_for(#contact, :method => :get) do |f| %> that should get it to work.
In your contact.rb model type the following
class Contact < ActiveRecord::Base
end

Ruby Incorrect link generation redirect_to products_path(#product) generate /products.1 instead of /products/1

I'm trying to add a reviews on my single product page. But when I click Submit - It takes me to the /products.1 page, instead of /products/1
class CommentsController < ApplicationController
def create
#product = Product.find(params[:product_id])
#comment = #product.comments.new(comment_params)
#comment.user = current_user
#comment.save
redirect_to products_path(#product)
end
def destroy
end
private
def comment_params
params.require(:comment).permit(:user_id, :body, :rating)
end
end
and the comment.html.erb
<div class="row">
<div class="col-sm-6">
<% if signed_in? %>
<h4>Add a review:</h4>
<%= form_for([#product, #product.comments.build]) do |f| %>
<p>
<%= f.label :body, "Comment" %><br>
<%= f.text_area :body, class: "form-control" %>
</p>
<p>
<%= f.label :rating %><br>
<%= f.text_field :rating, class: "rating form-control" %>
</p>
<p>
<%= f.submit "Submit", class: "btn" %>
</p>
<% end %>
<% end %>
</div>
</div>
Try redirect_to #product instead of redirect_to products_path(#product).
Did you check your routes.rb under config? Try running rake routes in the terminal and you can debug from there.

Undefined Method Error in rails

I'm new in rails and i could use a little help.
I have the following setup in rails
Day has_many Bookings
Booking belongs_to Day
The Booking form in shared/_booking_form is as follows:
<%= form_for Booking.new do |t| %>
<%= render 'shared/error_messages', object: t.object %>
<div class="field">
<%= t.label :start %><br>
<%= t.time_select :start %>
</div>
<div class="field">
<%= t.label :end %><br>
<%= t.time_select :end %>
</div>
<div class="field">
<%= t.label :comentariu %><br>
<%= t.text_area :comentariu, placeholder: "Adauga un comentariu.." %>
</div>
<%= t.submit "Post", class: "btn btn-primary" %>
<% end %>
The bookings controller look like this
def show
#bookings = Booking.all
end
def new
#booking = Booking.new
end
def create
#booking = #day.bookings.build(booking_params)
#booking.save
flash[:success] = "Book created!"
redirect_to root_url
end
def destroy
end
private
def booking_params
params.require(:booking).permit(:start, :end, :comentariu)
end
I get this error and i haven't been able to solve
NoMethodError (undefined method `bookings' for nil:NilClass):
app/controllers/bookings_controller.rb:9:in `create'
Any help would be appreciated.

Ruby / ActionMailer / Saving mail

I am creating a customer support app where clients can create, view, edit and comment support tickets.
I have this portion of the app working fine, but I want to have the data they submit into the ticket form emailed to me.
I have a separate "contact us" form that emails the data to me perfectly, but I want to combine the two forms into one.
It should work like this: client creates ticket, ticket is saved into the database, a copy of the ticket is emailed to me.
I can't figure out how to make all of these actions happen from one form.
Here is my tickets controller:
class TicketsController < ApplicationController
def new
#ticket = Ticket.new
end
def create
#ticket = Ticket.new(ticket_params)
#ticket.save
redirect_to #ticket
end
def show
#ticket = Ticket.find(params[:id])
end
def index
#tickets = Ticket.all
end
def edit
#ticket = Ticket.find(params[:id])
end
def update
#ticket = Ticket.find(params[:id])
if #ticket.update(ticket_params)
redirect_to #ticket
else
render 'edit'
end
end
def destroy
#ticket = Ticket.find(params[:id])
#ticket.destroy
redirect_to tickets_path
end
private
def ticket_params
params.require(:ticket).permit(:name, :email, :phone, :help)
end
end
Here is my new ticket view:
<%= link_to "View an Existing Ticket", tickets_path, :class =>'btn btn-danger btn-sm'%>
<h1>New Ticket</h1>
<%= form_for :ticket, url: tickets_path do |f| %>
<p>
<%= f.label "Name:" %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label "Email:" %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :"Phone #:" %>
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :"How can we help?" %>
<p><%= f.text_area :help, :cols=> 38, :rows => 8 %></p>
</p>
<p>
<button type="submit" class="btn btn-danger btn-sm">Submit Ticket</button>
</p>
<% end %>
<p><%= button_to "Back", root_path, :class => "btn btn-danger btn-sm", :method => :get %></p>
Here is my email controller:
class ContactController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.new_message(#message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
Here is my email view:
<%= form_for #message, :url => contactcreate_path do |form| %>
<fieldset class="fields">
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :subject %>
<%= form.text_field :subject %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
</fieldset>
<fieldset class="actions">
<%= form.submit "Send" %>
</fieldset>
<% end %>
Ticket Model:
class Ticket < ActiveRecord::Base
after_create :send_new_ticket_to_email
private
def send_new_ticket_to_email
NotificationsMailer.send_new_ticket(self).deliver
end
end
Notifications Mailer:
class NotificationsMailer < ActionMailer::Base
def send_new_ticket(ticket)
#ticket = ticket
mail(:subject => "HelpDesk: #{message.subject}")
default :from => "HelpDeskApp#ascendstudioslive.com"
default :to => "Support#ascendstudioslive.com"
end
Let me know if there is anything else you would like to see. Basically, I want to have one form that saves a ticket to the database and then emails a copy of it out.
Thank you!
You can create an after_create callback in your Ticket model to e-mail the saved ticket to yourself.
class Ticket < ActiveRecord::Base
after_create :send_new_ticket_to_email
private
def send_new_ticket_to_email
UserMailer.send_new_ticket(self).deliver
end
end
and in your ActionMailer class:
class UserMailer < ActionMailer::Base
def send_new_ticket(ticket)
#ticket = ticket
/* here you configure the variables for your email */
mail(to: your#email.com, subject: 'New ticket...')
end
end
then you will be able to use the #ticket object in your mailer views whatever way you please.
Have you tried this? I don't know if it is going to work, but all the same:
#new_message = NotificationsMailer.new_message(#message)
save_to_db(#new_message) # custom method to write it into your db somehow
#new_message.deliver
Not really sure what a contact is versus a ticket, but, broadly, the way to do what you want is, in your create action:
def create
#message = Message.create(params[:message])) # create will save and validate the message
if #message.valid?
NotificationsMailer.new_message(#message).deliver # #message gets set to
# else...
end

Resources