I have been fighting this for a while.
I have a simple app that use users, timesheets and entries. timesheets belong to to users and an user has many time sheets. Entries belong to time sheets.
A user can click on a time sheet and be presented with the entries from that time sheet. I can create entries in the rails console and they show up correctly. But I am having trouble inserting the correct timesheet ID on the entries.
I may not be doing this correctly, but I have a link on the time sheet view to the entry view. I am having trouble bringing over the time sheet id to insert it in the the create entry view.
I am using a form_for that looks like this.
<%= form_for(:entry, :url => {:action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Customer</th>
<td><%= f.text_field(:customer_name) %></td>
</tr>
<tr>
<th>Order Number</th>
<td><%= f.text_field(:order_number) %></td>
</tr>
<tr>
<th>Time In</th>
<td><%= f.text_field(:time_in) %></td>
</tr>
<tr>
<th>Time Out</th>
<td><%= f.text_field(:time_out) %></td>
</tr>
<tr>
<th>Time Sheet ID</th>
<td><%= f.text_field(:time_sheet_id, :value => #sheet_id ) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Add Entry") %>
</div>
<% end %>
This is my entry controller.
def create
#time_id = TimeSheet.find(1)
#new_entry = Entry.new(entry_params)
if #new_entry.save
flash[:notice] = "New Entry has been Added!"
redirect_to(:action => 'index')
else
render('new')
end
end
Do instead:
#new_entry = time_id.build_entry(entry_params)
Sorry, I Am closing this.
It was poorly worded and I figured out the problem.
All I needed to do is run the form_for from my timesheets controller and redirect it to the entries controller.
Related
I realize the heading is a little confusing but my problem is quite simple. I hae two models in my rails 5 app. User and Expense. Each expense belongs_to a user. I have an index page where all expenses are being listed. I can list the user IDs for each expense from the expenses table but I want to instead look up the name of the user (in column username) in the users table and display it with the expense instead. The view I have written is below. But it doesn't work.
<p id="notice"><%= notice %></p>
<h1>Teamjournals</h1>
<table style="padding: 2px; width: 50%" border="2px" align="center">
<thead>
<tr>
<td align="center"><%= link_to new_expense_path, :class =>"btn btn-success btn-wide" do%>Add New Expense<% end %></td>
</tr>
<tr>
<th>User</th>
<th>Expense Date</th>
<th>Currency</th>
<th>Expense Amount</th>
<th>Description</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #expenses.each do |expense| %>
<tr>
<td><%= User.joins(:expense).where('expense.user_id = ?', #user.id) %></td>
<td><%= expense.expense_date %></td>
<td><%= expense.currency.currency %></td>
<td align="right"><%= expense.expense %></td>
<td><%= expense.description %></td>
</tr>
<% end %>
</tbody>
</table>
Ok so in your iteration over #expenses you have this line:
<%= User.joins(:expense).where('expense.user_id = ?', #user.id) %>
you can change it to this:
<% user = expense.user %>
Note that I'm using <% not <%= because I'm just trying to assign a variable, not print the output to html.
Then after defining user you can say <%= user.name %>.
You should read a bit more about active record associations, but here's a few side comments about the query you've shown
User.joins(:expense).where('expense.user_id = ?', #user.id)
In this case, you should use the method generated by belongs_to instead of writing a query. But in situations where you do want to write a custom query, you should only be using where when you want to get an array. In this case you're looking for a single record so you could use find_by. Furthermore, the joins you're doing here is unnecessary
# any of these works
user = User.where('id = ?', expense.user_id).first
user = User.where(id: expense.user_id).first
user = user.find_by(id: expense.user_id)
I'm building a small admin page for my app that will display data from 4 models in one table. The columns are: Clubs, Users, Posts, Comments.
A club has_many users, a user has_many posts and has_many comments.
So my questions is do I need to add pagination explicitly to each of my 4 models in my admin_controller? The way it is now, I get the page list on the top and bottom of my table, and I can go back and forward pages, but all of my results are shown on the first page (~9000 results).
In my admin_controller I have
#clubs = Club.all.paginate(page: params[:page], per_page: 50)
and in my view
<%= will_paginate #clubs %>
<table>
<% i = 0 %>
<tr class="new-admin-top-row">
<td><%= "Club Location" %></td>
<td>| <%= "Number of Signups "%> </td>
<td>| <%= "Number of Posts By Users"%> </td>
<td>| <%="Number of Comments By Users"%> </td>
</tr>
<%= #clubs.find_each do |club| %>
<tr class="new-admin-row">
<td class="new-admin-cell"><%= club.name %></td>
<td class="new-admin-cell f"><%= #users_array[i] %></td>
<td class="new-admin-cell s"><%= #posts_array[i] %></td>
<td class="new-admin-cell"><%= #comments_array[i] %></td>
<td class="new-admin-cell"><%= #elevates_array[i] %></td>
<% i+=1 %>
</tr>
<% end %>
</table>
<%= will_paginate #clubs %>
The find_each method works on ActiveRecord::Relation objects and fetches 1000 records in batches. So that is where you problem most likely is. Change it to each and it'll probably solve your issue.
You can read more about find_each here: http://apidock.com/rails/ActiveRecord/Batches/find_each
I am working with an open source school management software, Fedena and I am trying to sort a list of users by their surnames. The software currently shows all users with their first names. I've found these two files to be responsible for showing the information I intend to change.
student_controller.rb
def list_students_by_course
#students = Student.find_all_by_batch_id(params[:batch_id], :order => 'last_name ASC')
render(:update) { |page| page.replace_html 'students', :partial => 'students_by_course' }
end
When I delete the above section of the file, the users names don't show up so I believe this section to be responsible for populating the table with the usernames.
_students_by_course.erb
<div class="students-table">
<table align="center" width="100%" cellpadding="1" cellspacing="1">
<tr class="tr-head">
<td><%= t('sl_no') %></td>
<td><%= t('name') %></td>
<td><%= t('adm_no') %></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<% #students.each_with_index do |r, i| %>
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-1">
<%= i+1 %>
</td>
<td class="col-2">
<%= link_to r.full_name,:controller => "student", :action => "profile", :id => r.id %>
</td>
<td class="col-1">
<%= r.admission_no %>
</td>
<td class="col-7">
<%= link_to "#{t('view_profile')}", :controller => "student", :action => "profile", :id => r.id %>
</td>
</tr>
<% end %>
</table>
</div>
I have tried changing 'last_name ASC' to 'last_name DESC' but nothing changed.
Any help is appreciated.
You've clarified in the comments that you want not sort order (I take it you've figured that out?) but the format of full name displayed for each instance of a model Student.
It's not hard if traced to the source. Let's start with a view, since that's what we see.
#students.each_with_index do |r, i|
We start a loop, each iteration of which processes an entry r with an index i. Since we're looping on a collection of Students (seems like a valid assumption), r is an instance of Student. The problematic line is:
<%= link_to r.full_name,:controller => "student", :action => "profile", :id => r.id %>
Actually, we should only look at r.full_name since that's what we get as a link label. It's a Student's method. Here it is.
def full_name
"#{first_name} #{middle_name} #{last_name}"
end
You might be wondering where is a second space, since such implementation implies that if middle_name is absent, we'd have two. Look at the source of the page and you'll see that there are actually two! So in order to change how the full name looks, you'll have to modify this method.
I am trying to find my time sheet id to pass into a form_for
here is my controller
def show
#time_id = current_user.time_sheets.find(params[:id])
if current_user
#current = current_user.time_sheets
else
redirect_to new_user_session_path, notice: 'You are not logged in.'
end
end
This is my form_for in my view:
<%= form_for(:entry, :url => {:controller => Entry, :action => 'create'}) do |f| %>
<table summary="Subject form fields">
<tr>
<th>Customer</th>
<td><%= f.text_field(:customer_name) %></td>
</tr>
<tr>
<th>Order Number</th>
<td><%= f.text_field(:order_number) %></td>
</tr>
<tr>
<th>Time In</th>
<td><%= f.text_field(:time_in) %></td>
</tr>
<tr>
<th>Time Out</th>
<td><%= f.text_field(:time_out) %></td>
</tr>
<tr>
<th>Time Sheet ID</th>
<td><%= f.hidden_field :time_sheet_id, value: #time_id.id %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Add Entry") %>
</div>
<% end %>
What needs to happen is that the timesheet id needs to get passed into the form_for so the entry can have the timesheet id.
I have user, timesheets and entries. user has_many time sheets, timesheets belongs to users and users have many time sheets. Entry belongs to timesheet.
I am getting this error "Couldn't find TimeSheet without an ID"
You should use "Nested Resources", eg. see here: Rails 3: How to create a new nested resource?
You could use form_for([#timesheet, #entry]) to pass the id of the timesheet without using a hidden field. If you do this, the :url param also become obsolete.
Try adding:
<%= #time_id.id %>
to output your variable and make sure it's exactly what you think it is. If #time_id is a timesheet, you should probably rename that variable to #time_sheet.
I keep getting this error when I try and view my register user page
Firefox has detected that the server
is redirecting the request for this
address in a way that will never
complete.
def register
#User registration form
#user = User.new(params[:user])
if #user.save
flash[:notice] = "Account Created Successfully"
redirect_to(:action=>'menu')
else
flash[:notice] = "Please fill in all fields"
redirect_to(:action=>'register')
end
end
<div class="user new">
<h2>Create User</h2>
<%= form_for(:user, :url => {:action => 'register'}) do |f| %>
<table summary="User form fields">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name) %></td>
</tr>
<tr>
<th>UserName</th>
<td><%= f.text_field(:user_name) %></td>
</tr>
<tr>
<th>Password</th>
<td><%= f.text_field(:password) %></td>
</tr>
<tr>
<tr>
<th>Email</th>
<td><%= f.text_field(:email) %></td>
</tr>
<tr>
<tr>
<th>Telephone</th>
<td><%= f.text_field(:telephone) %></td>
</tr>
<tr>
<td> </td>
<td><%= submit_tag("Register") %></td>
</tr>
</table>
<% end %>
</div>
Aren't you putting a redirect in your view method?
redirect_to(:action=>'register')
Are you distinguishing between the view method and the method that is actually fired when the user submits the form? Ex. for RestfulAuthentication the view is called "new", and the actual method that creates a user after the form is submitted is called "create".
You're redirecting you the same register action in the else. Try render :action => '<action that contains the form>'
I think that FireFox message means that you have some kind of server redirect loop going on. For example, if you have a before filter in your application controller that says if the user in not logged in then send them to the register user page. But then on redirect to the register page, that user is still not logged in. Then Rails will try to redirect them again to that page. Thus forming a loop.