New and Create are working good, data is stored in the database but edit and update is having issues.
This is my sages controller sages_controller.rb
class SagesController < ApplicationController
def home
end
def about
end
def new
#sage = Sage.new
end
def create
#sage= Sage.new(sage_params)
if #sage.save
flash[:success] = 'Thanks for Joining'
redirect_to '/thanks'
else
flash[:notice] = 'Please fill the form Correctly'
render 'new'
end
end
def edit
#sage=Sage.find(params[:id])
end
def update
#sage = Sage.find(params[:sage])
if #sage.update_attributes(sage_params)
redirect_to '/thankss'
else
render 'new'
end
end
def resources
end
private
def sage_params
params.require(:sage).permit(:Name, :Email, :Address, :Number)
end
end
THis is my view for new method new.html.erb
<div class="contact" style="padding: 50px; color: orange ">
<%= form_for #sage do |f| %>
<div class="form-group" >
<h3><%= f.label :Name%></h3>
<%= f.text_field :Name %>
<br/>
</div>
<div class="form-group" >
<h3><%= f.label :Email%></h3>
<%= f.email_field :Email %>
</div>
<div class="form-group" >
<h3><%= f.label :Address%></h3>
<%= f.text_field :Address %>
<br/>
</div>
<div class="form-group" >
<h3> <%= f.label :Number%></h3>
<%= f.number_field :Number %>
<br/>
</div>
<br/>
<br/>
<div class="submit" >
<h2><%= f.submit 'SUBMIT'%></h2>
</div>
<% end %>
<%= link_to 'Edit', 'edit' %>
</div>
`
This is my view for Edit method edit.html.erb
<%= form_for #sage do |f| %>
<%= f.label :Name %>
<%= f.text_field :Name %>
<br/>
<%= f.label :Email %>
<%= f.text_field :Email %>
<br/>
<%= f.label :Address %>
<%= f.text_field :Address %>
<br/>
<%= f.label :Number %>
<%= f.text_field :Number %>
<br/>
<%= f.submit 'Update' %>
<% end %>
`
This is my routes in routes.rb
resources :sages,only: [:new,:create,:edit, :update]
get 'sages/home'=>'sages#home'
get 'sages/about'=>'sages#about'
get 'sages/resources'=>'sages#resources' `
You are doing it wrong, how can you edit a record if it doesn't exist. In new.html.erb you have added a link
<%= link_to 'Edit', 'edit' %>
Remove that line, its wrong. You can only edit an existing record and not a new record. Also, the route is wrong. It should look something like
<%= link_to 'Edit', edit_sage_path(#sage) %>
Hope this helped!
Which URL or path you are using? I imagine you are using an incorrect URL or path.
With the routes you mentioned you should have the following access route for edit:
edit_sage GET /sages/:id/edit(.:format) sages#edit
So either you use edit_sage(id) or you type in the URL with /sages/id/edit. Please note that you have to replace id (in path or URL) by a valid id of a sage.
In the new view you cannot edit an object that does not exist (i.e. is not persisted in database).
<%= link_to 'Edit', 'edit' %>
is wrong as mentioned by other users.
Also if ever you want to call edit, consider what I mentioned in the beginning. Make sure you pass a valid id or sage object that effectively exists in database.
First thing you need to change edit link, it should be like :
<%= link_to 'Edit', edit_sage_path(#sage) %>
Second thing you can't call edit link in your new template because in this case you have #sage is empty (it just initiated),you can edit your sage by adding 'Edit' link to either index or show template
in update action change
def update
#sage = Sage.find(params[:id])
if #sage.update_attributes(sage_params)
redirect to ('/thanks')
else
render 'new'
end
end
Like an example if you have record with id = 2 then write url like /sages/2/edit and you will get data with record id=2.
Do this below change in your update method also,
#sage = Sage.find_by_id(params[:id])
The link i gave was wrong for edit in new.html.erb, removed it
and changed the route of edit to:
get 'sages/id/edit'=>'sages#edit' in routes.rb and now it works fine....
Related
i'm new to ruby on rails and I keep getting this error when trying to update an object.
here's my controller:
class SetorController < ApplicationController
def index
#setor = Setor.all
end
def new
end
def show
#setor = Setor.find(params[:id])
end
def create
#setor = Setor.new(setor_params)
#setor.save
redirect_to #setor
end
def edit
#setor = Setor.find(params[:id])
end
def update
#setor = Setor.find(params[:id])
if (#setor.update(setor_params))
redirect_to #post
else
render 'edit'
end
end
private def setor_params
params.require(:setor).permit(:nome, :sigla, :cnpj)
end
end
this is the index page - where I link to edit the object:
<%= #setor.each do |s| %>
<%= s.nome %> |
<%= s.sigla %> |
<%= s.cnpj %> |
<%= link_to "edit", edit_setor_path(s.id) %> <br>
<% end %>
and this is the update form:
<h1> Editar Setor </h1>
<%= form_for :setor, url: setor_path(:update) do |f| %>
<p>
<%= f.label :nome %>
<%= f.text_field :nome %> <br>
<%= f.label :sigla %>
<%= f.text_field :sigla %> <br>
<%= f.label :cnpj %>
<%= f.text_field :cnpj %> <br>
</p>
<%= f.submit %>
<% end %>
what am I missing?
Thanks!
First, make sure you have a route that matches edit_setor_path. You can do this by running rails routes in your terminal or by going to the url localhost:3000/rails/info/routes in your browser.
On your index page, you have the following:
<%= link_to "edit", edit_setor_path(s.id) %> <br>
You do not need to pass in the id of the s object. Rails will figure this out on its own. Instead, just pass in the object:
<%= link_to "edit", edit_setor_path(s) %> <br>
Change #setor in the index action of your SetorController to #setors and change #setor in your index.html.erb file to #setors.
Change the :sector in your edit.html.erb to #setor. You can also remove url: setor_path(:update) from the edit.html.erb form.
These changes follow Rails conventions. You should try to follow Rails conventions as much as possible, especially when just learning Rails.
Try to mention #setor object in the form code:
<%= form_for #setor do |f| %>
How about trying something like this:
Rename your controller to setors_controller.rb and change the class name to SetorsController -- this is to follow the typical Rails naming convention.
Then make sure you have a route:
# config/routes.rb
Rails.application.routes.draw do
resources :setors
end
Update your view's form_for tag to use the instance variable set up in the controller:
# app/views/setors/edit.html.erb
# NOTE: folder path above... "setor" is now "setors" to follow the Rails convention
<%= form_for #setor do |f| %>
<p>
<%= f.label :nome %>
<%= f.text_field :nome %> <br>
<%= f.label :sigla %>
<%= f.text_field :sigla %> <br>
<%= f.label :cnpj %>
<%= f.text_field :cnpj %> <br>
</p>
<%= f.submit %>
<% end %>
I'm giving the user the ability to reset their password and then reroute upon completion. However when using form tags and redirect_to, I'm getting some undesired rerouting.
Upon successful submission, the form should reroute to:
https://myherokuapp.herokuapp.com/customer_password_resets/new
but instead it reroute to:
https://myherokuapp/customer_password_resets/create.ShoyTPeAC5UAEjjWwZh_HA //the extra piece is the token id
CustomerPasswordResetsController Controller
def edit
#customer = Customer.find_by_password_reset_token!(params[:id])
end
def update
#customer = Customer.find_by_password_reset_token!(params[:id])
if #customer.password_reset_sent_at < 2.hours.ago
redirect_to new_customer_password_reset_path, :alert => "Password reset has expired."
elsif #customer.update_attributes(params[:customer])
redirect_to new_customer_password_reset_path, :notice => "Password has been reset!"
else
render :edit
end
end
customer_password_resets/edit.html.erb
<%= form_for #customer, :url => customer_password_resets_path(params[:id]) do |f| %>
<% if #customer.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in #customer.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Update Password" %></div>
The url option is the problem there. Rails will submit alright. The new view will submit to a create action and the edit to the update
Update, the previous answer did sucesfully reroute but did not update. I need the params in order to find the user in the update model. Rake routes showed that I indeed needed to change this from post to patch !
<%= form_for #customer, :url => customer_password_resets_path, :method => :patch do |f| %>
Views: _form.html.erb
<%= form_for(#product) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.number_field :price %>
</div>
<div class="actions">
<%= f.submit "Save" %>
<% if params[:action] == "edit" %>
<% f.submit "Save As New" %>
<% end %>
</div>
<% end %>
edit.html.erb
<%= render 'form' %>
In my products controller I have all the new,create,edit & update actions normally defined.
Question: This form update is working fine but I have a new submit button called "Save As New" is it anyway possible to invoke a create action inside the edit products page, when a user clicks on that.
I know the possibility to use a condition and check if params[:commit]=="Save As New" and try to invoke another action but I am not really sure if I can do that. How should I update my controller to make it work.
Thanks a ton.
You can change the update action like this:
if params[:commit]=="Save As New"
#product = Product.create(params[:product])
else
#product.update_attributes(params[:product])
end
redirect_to #product
So you wont need a new action and can handle it without adding routes...
If I'm understanding it correctly, you want the user to be able to create a new record from inside in the edit page, right?
If that's the case, I think you can do this:
<div class="actions">
<%= f.submit "Save" %>
<% if params[:action] == "edit" %>
<%= link_to "Save As New", {controller: 'products',
action: 'new'
},
class: "if there's a css class"} %>
<% end %>
</div>
I made a simple demo site, with an model for the patients name (name:string) and another model with the treatment (content:text). I created this "project" to learn more about the accepts_nested_attribute for tag and the fields_for tag.
Now my problem is that on the patient show page i created an nested formula for the treatment , like you can see here:
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #patient.name %>
</p>
<ol>
<% for treatment in #patient.treatments %>
<li><%= treatment.content %></li>
<% end %>
</ol>
<%= form_for(#patient) do |f| %>
<%= f.fields_for :treatments do |builder| %>
<div class="field">
<%= builder.label :content %><br />
<%= builder.text_field :content %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Edit', edit_patient_path(#patient) %> |
<%= link_to 'Back', patients_path %>
So my problem is that in the builder.text_field :content better called the input shows up the last saved treatment from <%= builder.content %>, but i want that he does not update it instead i want to add new treatments! Hopefully somebody understands me! Thanks
I would create separate controller for creating only the treatment, eg.
# treatments_controller.rb
class TreatmentsController < ApplicationController
def create
#patient = Patient.find(params[:patient_id])
#treatment = #patient.treatments.new(params[:treatment])
if #treatment.save
redirect_to #patient
else
# handle unsuccessful treatment save
end
end
end
# routes.rb:
resources :patients do
resources :treatments, only: :create
end
# form:
<%= form_for #patient, #treatment do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should also set #treatment variable in the patient#show action, like this:
#treatment = #patient.treatments.new
My Artist conroller, edit method looks like this
def edit
#user = User.find(params[:id])
end
In the view (artists/edit.html.erb), I have:
<%= form_for(#artist) do |f| %>
<ul>
<li class="clearfix">
<%= f.label :name %>
<%= f.text_field :name %>
</li>
...
</ul>
<%= image_submit_tag('update.png', :class => 'submit') %>
<% end %>
However, when rendered. The form element submits to the user controller, update method (i.e. ).
How do I make it submit to the artist controller, update method instead?
I think you want to change the form_for parameters :
<%= form_for(#artist, :url => edit_user_path(#artist)) do |f| %>
<ul>
<li class="clearfix">
<%= f.label :name %>
<%= f.text_field :name %>
</li>
...
</ul>
<%= image_submit_tag('update.png', :class => 'submit') %>
<% end %>
Cf http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
Also, are you sure your #artist points to #user ? I think you meant, in your controller :
#artist = User.find....