My forum is not updating the table? - ruby-on-rails

I am trying to update my table by using the fourm :-
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for Message.new do |f| %>
<%= f.label :To %>
<%= f.email_field :to, class: 'form-control' %>
<%= f.label :Subject %>
<%= f.text_field :subject, class: 'form-control' %>
<%= f.label :Content %>
<%= f.text_area :content, class: 'form-control' %>
<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %>
</div>
</div>
and in my controller I have done :-
def create
#message = Message.new(user_params)
redirect_to root_url
end
def user_params
params.require(:message).permit(:to, :subject, :content)
end
But when i check my Table, there is no update .
Where am i going wrong ?
My table have 2 more extra attributes , but i am not filling them up with the forum .

.new just builds the new object, you need to actually save it to persist it to the database.
def create
#message = Message.new(user_params)
#message.save!
redirect_to root_url
end

Related

Using validations and pluralize in views to show errors count for forms rails 7

i am trying to display a message for whenever someone does not fill the form completely and press the submit button!
Example: when someone clicks the submit button without filling the Name and Email field in the form, it should display
2 Errors Prohibited from submitting the form!
. Name can't be blank
. Email can't be blank
but it is not being displayed whenever we submit the form with empty fields
can someone explain me the reason why it is not working?
Orders_controller.rb
class OrdersController < ApplicationController
# GET to /orders/new
def new
#order = Order.new
end
# POST to /orders
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save!
format.html{ redirect_to root_url, notice: "Order Succesfully Submitted!" }
else
format.html{ render :new, status: :unprocessable_entity }
end
end
end
private
def order_params
params.require(:order).permit(:first_name, :last_name, :phone_number, :email, :paper_size, :color, :paper_style, :quantity, :description, files: [] )
end
end
_form.html.erb (i already rendered the partial in new.html.erb by <%= render 'form', order: #order%>
<div class="container">
<h1 class="text-center">Order From Home!</h1>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<%= form_with(model: order) do |f| %>
<% if order.errors.any? %>
<div style="color: red">
<h3><%= pluralize(order.errors.count,"errors")%> Prohibited from submitting the form! <br/>
All Fields Are Required!</h3>
<ul>
<% order.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :first_name%>
<%= f.text_field :first_name, class:"form-control" %><br/>
<%= f.label :last_name %>
<%= f.text_field :last_name, class:"form-control" %><br/>
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class:"form-control" %><br/>
<%= f.label :email %>
<%= f.text_field :email, class:"form-control" %><br/>
<%= f.label :files %>
<%= f.file_field :files, multiple: true %><br/>
<%= f.label :paper_size %>
<%= f.select :paper_size, ['A4', 'B4'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :color %>
<%= f.select :color, ['Black & White', 'Color'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :paper_style %>
<%= f.select :paper_style, ['Black to Back', 'Side to Side'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :quantity %>
<%= f.number_field :quantity, class:'form-control' %><br/>
<%= f.label :description %>
<%= f.text_area :description, class:"form-control" %><br/>
<div class="btn-order">
<%= f.submit %>
</div>
<% end %>
</div>
</div>
</div>
#order.save! raises validation errors. You don't need bang method if you want to render such errors, just use #order.save instead
form_with sends XHR request. If you need to render something, you can disable Turbo for this form
<%= form_with(model: order, data: { turbo: false }) do |f| %>

Contact name not showing name in DB

I'm a rookie, and looking to create a contact form, but when I check my submitted forms, it doesn't show the name. It just says name "" I'm using Ruby on Rails.
My html looks like;
Contact Us
<div class="col-md-4 col-md-offset-4">
<%= flash[:notice] %>
<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.text_field :name, 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>
My controller code looks like;
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
Probably your second text field is the problem, it's called :name twice...
change the second input and try it again
<%= f.text_field :name, class: 'form-control' %>
to
<%= f.text_field :email, class: 'form-control' %>

How get params from link ruby on rails?

I have column inviter in my User model. I want get params from the link and save it in cookies. For example, from this link:
domain.com/?ref=5
I want save to cookies number ?ref=**5**
And when a user goes to registration, the user will not need to write in the number of the referral in the form, because it is already in cookies.
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name,'Your name'%>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email,'Email' %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :invite, 'Number who invited you' %>
<%= f.text_field :invite, class: 'form-control' %>
<%= f.label :password, 'password' %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation ,'password confirmation' %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<br>
<br>
<div class="textc">
<%= f.submit yield(:button_text), class: "formbut" %>
</div>
<% end %>
The invite number will use ?ref=**5** and register the new user.
Add something like this to your application_controller.rb:
before_action :store_ref
private
def store_ref
cookies[:ref] = params[:ref] if params[:ref].present?
end
And something like this to your user create action:
def create
#user = User.new(user_params)
#user.ref = cookies[:ref]
if #user.save
# ...
A better approach would be to use a hidden input and populate it with the value from params[:ref].
class UsersController < ApplicationController
def new
#user = User.new do |u|
if params[:ref].present?
u.inviter = params[:ref]
end
end
end
# ...
end
<%= form_for(#user) do |f| %>
# ...
<% if f.object.inviter.present? %>
<%= f.hidden_field :inviter %>
<% end %>
<% end %>
It requires less workarounds.

Update feature does not update post but creates a new post

I've been creating a system where I create these entries but the problem is when I tried to create the edit feature, it does not update the post but creates a new one.
My controller file looks like this:
class BetsController < ApplicationController
def new
#bet = Bet.new
end
def create
#bets = Bet.new(bet_params)
if #bets.save
flash[:success] = 'Bet Successfull Logged.'
redirect_to new_bet_path
else
flash[:danger] = 'Error, Bet has not been logged. Try again mate.'
redirect_to new_bet_path
end
end
def show
#bet = Bet.find(params[:id])
end
def edit
#bet = Bet.find(params[:id])
end
def update
#bet = Bet.find(params[:id])
if #bet.update_attributes(bet_params)
flash[:success] = "Bet Updated!"
redirect_to bet_path(params[:id])
else
render action: :edit
end
end
private
def bet_params
params.require(:bet).permit(:bet_placed, :game, :units_placed, :odds, :profit_or_loss)
end
end
And also the form that is submitting looks like this:
<%= form_for :bet, url: bets_path, :html => { :multipart => true } do |f| %>
<p class="form-group">
<%= f.label :bet_placed %><br>
<%= f.text_field :bet_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :game %><br>
<%= f.text_field :game, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :units_placed %><br>
<%= f.text_field :units_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :odds %><br>
<%= f.text_field :odds, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :profit_or_loss %><br>
<%= f.text_field :profit_or_loss, class: 'form-control' %>
</p>
<%= f.submit 'Update Profile', class: 'btn btn-default' %>
<% end %>
The way your form is currently setup, it will always hit the create action. If you change your form_for to the following it should work
<%= form_for #bet do |f| %>
...
<% end %>
You shouldn't need multipart => true either since your form doesn't have any file inputs.
According to the rails natural routing the path bets_path always goes to the create method. As update needs an id, and you are not giving it, it goes to create.
Change the form to,
<%= form_for #bet, :html => { :multipart => true } do |f| %>
<p class="form-group">
<%= f.label :bet_placed %><br>
<%= f.text_field :bet_placed, class: 'form-control' %>
</p>
....................
<p class="form-group">
<%= f.label :profit_or_loss %><br>
<%= f.text_field :profit_or_loss, class: 'form-control' %>
</p>
<%= f.submit 'Update Profile', class: 'btn btn-default' %>
<% end %>
For reference,
HTTPVerb Path controller#Action Named Helper
GET /bets bets#index bets_path
GET /bets/new bets#new new_bet_path
POST /bets bets#create bets_path
GET /bets/:id bets#show bet_path(:id)
GET /bets/:id/edit bets#edit edit_bet_path(:id)
PATCH/PUT /bets/:id bets#update bet_path(:id)
DELETE /bets/:id bets#destroy bet_path(:id)

Nested model doesn't update

I have two models: PointPage and PointPageClass, which belongs_to :point_page. When user create PointPage rails creates several (now there're 6 of them) PointPageClasses in database. I need to edit all of them on PointPage edit page. So, I'm using nested attributes in controller and fields_for in view.
point_page_form
<%= form_for #point_page, role: 'form' do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
... # some other PointPage fields
<% #point_page.point_page_classes.each do |class| %>
<%= f.fields_for :point_page_classes do |builder| %>
<div class="row">
<div class="col-lg-6">
<%= builder.text_field :distance_price, class: 'form-control' %>
</div>
<div class="col-lg-6">
<%= builder.text_field :show_price, class: 'form-control' %>
</div>
</div>
<% end %>
<% end %>
<%= f.submit 'Save', class: 'btn btn-default' %>
<% end %>
point_page_controller
def update
#point_page = PointPage.find(params[:id])
if #point_page.update_attributes(point_page_params)
respond_to do |format|
format.html { redirect_to new_point_path }
format.js
end
end
end
private
def point_page_params
params.require(:point_page).permit(:name, :url, :article,
point_page_classes_attributes: [:distance_price, :show_price])
end
So, I've added in point_page_controller PointPageClass attributes, but when I save the changes, it only saves changes for PointPage, but not for PointPageClass (distance_price and show_price).
Thanks for any help!
One of the problem could be not permitting the :id in the point_page_params.For the update to take place you need to permit the :id.
Try changing your point_page_params to like this
def point_page_params
params.require(:point_page).permit(:id,:name, :url, :article,point_page_classes_attributes: [:id,:distance_price, :show_price])
end

Resources