Hi im new to ruby on rails. And im exploring the codes on it. Ive got this error line of code
undefined method `permit' for nil:NilClass
here's my controller below
class AddsController < ApplicationController
def new
#add = Add.new
end
def create
#add = Add.new(params[:post].permit(:first_name,:last_name,:email))
if #add.save
redirect_to(:controller=>'home')
else
render 'new'
end
end
end
my new.html.erb
<h1>Add Record </h1>
<%= render 'form' %>
<%= link_to "Back", controller: "home" %>
and my _form.html.erb
<%= form_for #add do |f| %>
<table>
<tr>
<td>FirstName: </td>
<td><%= f.text_field :first_name %></td>
</tr>
<tr>
<td>LastName: </td>
<td><%= f.text_field :last_name %></td>
</tr>
<tr>
<td>Email: </td>
<td><%= f.text_field :email %></td>
</tr>
<tr>
<td> </td>
<td><%= f.submit "Add", :class => 'btn btn-success' %></td>
</tr>
</table>
<% end %>
can someone help me figured out the error?
It should look like:
params.require(:add).permit(:first_name, :last_name, :email)
Related
I am new in rails
I have 7 fields in my forms in rails 5 and validations on some of them
When i am filling all correct data it's working fine.
When i do not fill any field which is mandatory it shows error message problem is it clear all the 4 or 5 or 6 textboxes and i need to fill all the same data again.
Is there any option to retain the data and show alert message?
I have tried redirect_to and render both but both are not working and refreshes the page and clear all the data.
def create
if user.save
redirect_to user_path, notice: 'User saved'
else
#redirect_to new_user_path, alert: user.errors.full_messages.first
flash[:alert] = user.errors.full_messages.first
render '/admin/user/new'
end
end
new.html.erb
<%= form_with(url: create_user_path+'?'+request.query_string, id: 'new_user', class: 'new_user', local: true) do |f| %>
<%= render 'form', f: f %>
<br><br>
<%= f.submit 'Submit' %>
<% end %>
_form.html.erb
<table class='mui-table mui-table--bordered'>
<%= f.fields_for :user do |f| %>
<tr>
<td><strong>First Name</strong></td>
<td><%= f.text_field :first_name %></td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td><%= f.text_field :last_name %></td>
</tr>
<tr>
<td><strong>Name of Bank</strong></td>
<td><%= f.text_field :bank_name %></td>
</tr>
<tr>
<td><strong>Branch Name</strong></td>
<td><%= f.text_field :branch_name %></td>
</tr>
<tr>
<td><strong>Account Name</strong></td>
<td><%= f.text_field :account_name %></td>
</tr>
<tr>
<td><strong>Account Number</strong></td>
<td><%= f.text_field :account_number, { onkeypress: 'return isNumberKey(event)' } %></td>
</tr>
<tr>
<td><strong>Membership Number</strong></td>
<td><%= f.text_field :sf_membership_number, { onkeypress: 'return isNumberKey(event)' } %></td>
</tr>
<% end %>
</table>
Create method is above.
Two steps need to follow:-
Use form_for instead of form_with
remove <%= f.fields_for :user do |f| %>
Its works
I'm trying to update multiple records with one form:
Here's the code in the view:
<%= form_for :product, :url => admin_products_update_multiple_path, :html => { :method => :put } do %>
<table>
<tr>
<th>Supplier Name </th>
<th>Product Name </th>
<th>type_name</th>
<th>brand</th>
<th>image</th>
</tr>
<% #products.each do |product| %>
<%= fields_for "product[]", product do |product_fields| %>
<tr>
<td><%= product.supplier_name %> </td>
<td><%= product.subcategory_name %> </td>
<td><%= product_fields.text_field :type_name %></td>
<td><%= product_fields.text_field :brand %></td>
<td><%= product_fields.text_field :image %></td>
</tr>
<% end %>
<% end %>
</table>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
... and here's the controller action I'm calling to update:
def update_multiple
params['product'].keys.each do |id|
#product = Product.find(id.to_i)
#product.update_attributes(product_params)
end
flash[:notice] = "Update products successfully"
redirect_to(admin_products_path)
end
private
def product_params
params.require(:product).permit(:id, :type_name, :brand, :image)
end
When I run the method, I'm getting a 'Unpermitted parameters' error showing all the ids it's trying to update. Any ideas? Thanks in advance!
Form screenshot
the image up explain that there is employ who has been allocated three projects at this time he want to full up the fields as he want example he can fill up 2 fields and press submit btn or 1 or 3 depending how many projects he has been assigned.
what i was unable to do is that i want to insert that multiple fields in a table in single submit i do not understand what code i write it will work
the code in my controller is the following.
def new
#pro=Employee.find(params[:employee_id])
#timesheet=Timesheet.new
#project=Project.where(:employee_id => params[:employee_id]).sorted
end
def create
#pro=Employee.find(params[:employee_id])
#timesheet=Timesheet.new(params.require(:timesheets).permit(:employee_id,:project_id,:IN,:comments))
if #timesheet.save
flash[:notice] = "data entered."
redirect_to(:action => 'show',:employee_id =>#pro.id)
else
flash[:notice]="logged out"
end
end
the code in my new.html.erb is the following
<%= form_for(:timesheets, :url => {:action => 'create',:employee_id => #pro.id}) do |d| %>
<% if !#project.nil? %>
<% #project.each do |page| %>
<tr>
<%= d.hidden_field("employee_id" ,:value => #pro.id) %>
<%= d.hidden_field("project_id" ,:value => page.id) %>
<% if !page.employee_id.blank? %>
<td><%= page.prog_name %></td>
<td><%= d.text_field("IN",:class => "qty1") %></td>
<td><%= d.text_field("comments") %></td>
<% end %>
<% end %>
</tr>
<% end %>
<tr>
<td>Total hours</td>
<td colspan="2"><%= text_field_tag("total")%></td>
</tr>
<tr border="0">
<td ><%= submit_tag("Submit") %></td>
<td colspan="2" border="0"></td>
</tr>
Assume you have the model called 'Topic' as a parent, and 'Comment' as a child.
On the url 'topics/show/35' you can see all the comments that belongs to this topic ID#35.
When logged-in user want to post his new comment at this page,
should I write 'comment_create' action in topics_controller.rb?
or just write 'create' action in comments_controller.rb, and call it from this page?
Which one is regular way??
If I call 'create' action in comments_controller, how can I write in view to pass
'Model name' to add comments into
'Models ID#'
'comment body'
or should I just write actions separately like this?
controllers/comments_controller.rb
def create_in_topic
code here! to add new comment record that belongs to topic....
end
def create_in_user
code here! to add new comment record that belongs to user....
end
for your information, comment adding action should be something like this.
def create
#topic = Topic.find(params[:topics][:id] )
#user_who_commented = current_user
#comment = Comment.build_from( #topic, #user_who_commented.id, params[:topics][:body] )
#comment.save
redirect_to :back
flash[:notice] = "comment added!"
end
Example Updated!!!
views/topics/show.html.erb
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
<th>Subject</th>
<th>Posted by</th>
<th>Delete</th>
</tr>
<% #topic.comment_threads.each do |comment| %>
<tr>
<td><%= comment.id %></td>
<td><%= comment.title %></td>
<td><%= comment.body %></td>
<td><%= comment.subject %></td>
<td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td>
<td> **Comment destroy method needed here!!!** </td>
</tr>
<% end %>
</table>
<%=form_for :topics, url: url_for( :controller => :topics, :action => :add_comment ) do |f| %>
<div class="field">
<%= f.label :'comment' %><br />
<%= f.text_field :body %>
</div>
<%= f.hidden_field :id, :value => #topic.id %>
<div class="actions">
<%= f.submit %>
<% end %>
controllers/topics_controller.rb
def add_comment
#topic = Topic.find(params[:topics][:id] )
#user_who_commented = current_user
#comment = Comment.build_from( #topic, #user_who_commented.id, params[:topics][:body] )
#comment.save
redirect_to :back
flash[:notice] = "comment added!"
end
I think the most straight forward implementation it's going to be an action (ie: add_comment) in your Topic Controller. Once the view call the TopicController#add_comment action you will have all your Topic information and also the comment data so you can easily add the comment to the Topic from there.
Let me know if you need further help.
FedeX
Well I'm not to sure because that gem, but you could try something like this:
<%=form_for #topic, url: url_for( :controller => :topics, :action => :add_comment ) do |f| %>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Body</th>
<th>Subject</th>
<th>Posted by</th>
<th>Delete</th>
</tr>
<% #topic.comment_threads.each do |comment| %>
<%= f.fields_for :comment_threads, comment do |comment_form| %>
<tr>
<td><%= comment.id %></td>
<td><%= comment.title %></td>
<td><%= comment.body %></td>
<td><%= comment.subject %></td>
<td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td>
<td>Delete? <%= comment_form.check_box :_destroy %></td>
</tr>
<% end %>
<% end %>
</table>
<div class="field">
<%= f.label :'comment' %><br />
<%= f.text_field :body %>
</div>
<%= f.hidden_field :id, :value => #topic.id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Let me know if it helps you! FedeX
(ruby 1.9.2 rails 3.0.6)
I am new to ROR, sorry for my simple question.
I have a form and a controller, when i launch my app, i can see the log have been displayed.. however there are exception when rendering the view...
Everything should look just fine.. what is the problem? how can I fix it?
Thanks.
undefined method `users_path' for #<#<Class:0x47c7678>:0x47c6118>
Extracted source (around line #2):
1: <h1>Welcome Aboard</h1>
2: <%= form_for(#user) do |f| %>
3: <table>
4: <tr>
5: <td><%= f.label :username %></td>
class RegisterController < ApplicationController
def sign_up
logger.debug("sign_up inoked")
#user = User.new
logger.debug("sign_up finished")
end
end
views/register/sign_up.html.erb
<%= form_for(#user) do |f| %>
<table>
<tr>
<td><%= f.label :username %></td>
<td><%= f.text_field :username %></td>
</tr>
<tr>
<td><%= f.label :password %></td>
<td><%= f.password_field :password %></td>
</tr>
<tr>
<td><%= f.label :password_confirmation %></td>
<td><%= f.password_field :password_confirmation %></td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2"><%= f.submit "Sign up" %></td>
</tr>
</table>
<% end %>
It seems that you do not have the appropriate routes. Do you have a line like :
resources :users
in your routes.rb file ? If not try adding that please. Or just create the particular users_path named route.