I am attempting to add product information to a rails sqlite database through a form. I can get users to be added but not the products for the store. This is the form.
<form action='/products/create' method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<label>Name:
<input type="text" name="product">
</label>
<label>Amount:
<input type="number" name="amount">
</label>
<center><input type="submit" value="Sell"></center>
</form>
It does not throw an error but it does not save to db either.
Products_controller is
def create
#products = Product.new(name: params[:name], amount: params[:amount])
if #products.valid?
#products.save
flash[:message] = "New product added"
redirect_to "/users"
else
flash[:errors] = #products.errors.full_messages
redirect_to "/products"
end
I figured it out. The problem was this line.
<input type="text" name="name">
In my database product is defined as name and amount. Product does not have a product field.
Related
I'm working on a project in Sinatra and I can't seem to get the delete method to work. My intent is to be able to remove an object using a form in a modal. Here's what I have:
routes.rb:
delete '/songs/:id/delete' do
#song = Song.where(:id => params[:id]).first
#song.delete
redirect to '/songs'
end
index.erb:
<form action="/songs/:id/delete" method="post">
<input type="hidden" name="_method" value="delete">
<div id="song_id">
<label>id:</label>
<input type="text" name="id">
</div>
<button type="submit" id="delete">Delete</button>
<div id="back">Back to Songs</div>
</form>
Feedback is appreciated.
(Also, sorry indentation isn't perfect)
You have to inject the id into the form. Not :id.
# example
<form action="/songs/1234/delete" method="post">
Also, you can see what is happening with puts params.
I just learned about pry for debugging in rails, I'm working on a ecommerce site , on the main sign in page, when the login button is clicked, the email, username and the password field of the login form is suppose to save to the database. Instead of getting Example A, I get Example B but I'm not sure what {"controller"=> "application", "action" => "login"} means. Can someone help me please.
Example:A
correct result
Example:B
[wrong result pasted below][1]
23: def login
24: #username = params[:username]
25: #email = params[:email]
26: #password = params[:password]
=> 27: binding.pry
28: if #email && #password
29: session[:signed_in] = true
30: session[:username] = params[:username]
31: redirect_to '/profile'
32: end
33: end
[1] pry(#<ApplicationController>)> #username
=> nil
[2] pry(#<ApplicationController>)> params
=> {"controller"=>"application", "action"=>"login"}
[3] pry(#<ApplicationController>)>
My sign in form:
<form action='/login' class='validate-form' method='post'>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Email Address
<em>*</em>
</label>
<input type="email">
</p>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Username
<em>*</em>
</label>
<br>
<input type="email">
</p>
<p class="checkout-coupon top-down log a-an">
<label class="l-contact">
password
<em>*</em>
</label>
<input type="password">
</p>
<div class="forgot-password1">
<label class="inline2">
<input type="checkbox" name="rememberme7">
Remember me! <em>*</em>
</label>
<a class="forgot-password" href="#">Forgot Your password?</a>
</div>
<p class="login-submit5">
<input class="button-primary" type="submit" value="login">
</p>
</form>
My routes.rb:
Rails.application.routes.draw do
get '/profile', to:'application#profile'
get '/logout', to: 'application#logout'
post '/login', to: 'application#login'
end
In answer to your specific question
I'm not sure what {"controller"=> "application", "action" => "login"} means
This is a Ruby Hash with two key value pairs that is referenced by your link within the form. The value of the key controller refers to the specific controller that you are linking to with your form. In this case your ApplicationController as defined by the value application. The value of the key action refers to the specific method within that controller that you are calling.
In your form you have <input type="email">, but no name attributes. Name attributes are used by the server to identify the fields in the form.
link check this for more information.
Your params attributes are just {"controller"=>"application", "action"=>"login"} because thats where the form is being submitted, To your application controller and login action, your login action is defined in your form <form action='/login' class='validate-form' method='post'> here /login, thats your action.
In this case login. To get example A, your form should be like this
<input type="email" name='email'>
<input type="text" name='username'>
<input type="password" name='password'>
If you post your form I can try to provide an answer to your other problems.
Change the form to this and try again,
Try adding names to the input fields, and then use the pry and insect the params from it.
Also did you use the strong parameters for the fields?
<form action='/login' class='validate-form' method='post'>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Email Address
<em>*</em>
</label>
<input type="email" name="email">
</p>
<p class="checkout-coupon top log a-an">
<label class="l-contact">
Username
<em>*</em>
</label>
<br>
<input type="email" name="username">
</p>
<p class="checkout-coupon top-down log a-an">
<label class="l-contact">
password
<em>*</em>
</label>
<input type="password" name="username">
</p>
<div class="forgot-password1">
<label class="inline2">
<input type="checkbox" name="rememberme7">
Remember me! <em>*</em>
</label>
<a class="forgot-password" href="#">Forgot Your password?</a>
</div>
<p class="login-submit5">
<input class="button-primary" type="submit" value="login">
</p>
</form>
In my contact.html, I have this code
<div class="form">
<form name="email-form" method="POST">
<label class="field-label" for="name">Name:</label>
<input class="w-input text-field-2" id="name" type="text" name="name" data-name="Name" required="required">
<label class="field-label" for="Email">Email:</label>
<input class="w-input text-field-2" id="email" type="email" name="email" data-name="Email" required="required">
<label class="field-label" for="Subject">Subject:</label>
<input class="w-input text-field-2" id="subject" type="text" name="subject" data-name="Subject">
<label class="field-label" for="Content">Text Message:</label>
<textarea class="w-input text-field-2 area" id="content" name="content" data-name="Text Area" required="required"></textarea>
<div>
<input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
</div>
</form>
</div>
This is under my controller
def contact
end
def send_mail
MessageMailer.new_message(contact_params).deliver
redirect_to contact_path, notice: 'Your messages has been sent.'
end
private
def contact_params
params.require(:contact).permit(:name, :email, :subject, :content)
end
under mailers/message_mailer.rb
class MessageMailer < ActionMailer::Base
default from: "sys.questdentalusa#gmail.com"
default to: "questdentalusa#gmail.com"
def new_message(contact)
#contact = contact
mail subject: #contact.subject
end
end
and under my new_message.text.erb is this code
Name: <%= #contact.name %>
Email: <%= #contact.email %>
Message: <%= #contact.content %>
I am to send an email consisting user's name, email and message which is inputed and NOT saved in the database. When I pass the four parameters like this
def send_mail
MessageMailer.new_message(:name, :email, :subject, :content).deliver
redirect_to contact_path, notice: 'Your messages has been sent.'
end
it worked just fine but i was told to use only one parameter, seems like group the four parameters: name, email, subject, content as one (contact)
When I typed the info and hit the submit button, I get this error message
param is missing or the value is empty: contact
I presume that what caused this error is because my def contact is empty. So I added contact.new and #contact=contact.new and MessageMailer.new but this error occurs NoMethodError
How can I possibly fix this? What should I write under my def contact ?
Your controller code is correct. You don't need Contact.new or something. The problem is with your <form>. What ends up in params depends on your form and in your form you don't have contact.
Instead of:
<input type="text" name="subject">
You have to do something like this:
<input type="text" name="contact[subject]">
And that for all the fields on your contact form.
Another option would be to use Rails' form helpers.
contact[] is missing in your form, see below correct one:
<div class="form">
<form name="email-form" method="POST">
<label class="field-label" for="name">Name:</label>
<input class="w-input text-field-2" id="name" type="text" name="contact[name]" data-name="Name" required="required">
<label class="field-label" for="Email">Email:</label>
<input class="w-input text-field-2" id="email" type="email" name="contact[email]" data-name="Email" required="required">
<label class="field-label" for="Subject">Subject:</label>
<input class="w-input text-field-2" id="subject" type="text" name="contact[subject]" data-name="Subject">
<label class="field-label" for="Content">Text Message:</label>
<textarea class="w-input text-field-2 area" id="content" name="contact[content]" data-name="Text Area" required="required"></textarea>
<div>
<input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
</div>
</form>
</div>
problem: I have a form but the submit button doesn't do anything.
i instantiate the class the form is for in the employee_controller controller.
def employee
#body_class = "employee membership"
#employee_contact_form = CorporateContactForm.new
end
I create the form in the page the above controller action serves
= simple_form_for [#employee_contact_form] do |f|
= f.input :firstname
= f.button :submit
In my routes I set the resources for the contact forms
resources :corporate_contact_forms
I then have a controller that serves the form
class CorporateContactFormsController < ApplicationController
def create
raise
end
I am aware there is no code in the corporatecontactcontroller, but the submit button should at least fire to an error. It doesn't do anything.
This feels like such a simple problem, and surely it is.
What am I missing?
update
html output
<form>
<form accept-charset="UTF-8" action="/corporate_contact_forms" class="simple_form new_corporate_contact_form" id="new_corporate_contact_form" method="post"><div style="display:none"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="7iMwWQOuyzV3jJt4jTtr9MGvI129hPaG+m+Pe2D3YyM=" /></div> <div class="input string optional corporate_contact_form_firstname"><label class="string optional" for="corporate_contact_form_firstname">Firstname</label><input class="string optional" id="corporate_contact_form_firstname" maxlength="255" name="corporate_contact_form[firstname]" size="255" type="text" /></div>
<input class="button" name="commit" type="submit" value="Create Corporate contact form" />
</form>
</form>
According to the documetation (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for), you can do something like this to force the form to go to CorporateContactFormsController#create
<%= simple_form_for #employee_contact_form, url: corporate_contact_forms_path, method: :post do |f| %>
# awesome code...
<% end %>
Also I'm not sure if the f.button :submit has something to do, the default for submitting is f.submit
I am using ruby on rails 3.1. And am trying to post html form data to controller for saving the record in database. But I am getting routing error like this 'No route matches [POST] first/save' .But when I tried to run this link in address bar like '127.0.0.1:3000/first/save' it is working fine. Can any one please tell me where am doing wrong.
My routes are like:
Rails.application.routes.draw do
root 'first#hello'
get 'first/save'
end
And my html form is like:
<form accept-charset="UTF-8" method='post' action='/first/save'>
<label for='S.No'>S.No</label>
<input type="text" name="s_no" placeholder='Enter s. no.'>
<label for='name'>Name</label>
<input type="text" name='name' placeholder='Enter your name'> <br>
<label for='seller_id'>Seller ID</label>
<input type="text" name='seller_id' placeholder='Enter your seller ID'> <br>
<label for='email'>Email</label>
<input type="email" name='email' placeholder='Enter your email'> <br>
<label for='img_url'>Image</label>
<input type='text' name='img_url' placeholder='Enter your image url'> <br>
<input type="submit" name='save' value='Save'>
</form>
And here is my controller:
class FirstController < ApplicationController
def save
#name = params[:name]
#email = params[:email]
#seller_id = params[:seller_id]
#img_url = params[:img_url]
#s_no = params[:s_no]
end
end
If you want to do POST requests, instead of
get 'first/save'
you should have
post 'first/save'