I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>
Related
So I am making a Social media website where user join events and compete against each other.
Each time a user join an event a status appears on his profile. The issue is that when I look at other user profiles, the statuses just match who ever the current user is.
If someone could help me write some code that shows the different statuses for each of the users that would be great. I know the question may sound a little vague.
I just need to know a way to see only post based on which profile I am viewing not the current user. Thanks!
<div class="center">
<div class="col-xs-6">
<div class="panel panel-default">
<div class="panel-body">
<% if !#user_events.present? %>
<h4>Hi <%= User.find_by_username(params[:id]).username %>, thanks for joining!</h4>
<h4>Join events, complete them, and compete</h4>
<% end %>
<% #user_events.each do |ue|%>
<% if ue.event.present? %>
<h4 class="blue">
<%= User.find_by_username(params[:id]).username %>
</h4>
<% userevent = User.find_by(params[:event]) %>
<p>I'm going to <%= link_to ue.event.title %>.</p>
<% if ue.hours.present? %>
<h5> Completed Hours: <%= ue.hours %> </h5>
<% end %>
<% if ue.hours.present? %>
<img src="#" style="width: 10%;" alt="">
<% end %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
First of all, it's very bad to have queries to the database inside of your view. Please, move expression inside <%= User.find_by_username(params[:id]).username %> to your controller and then use instance variable to show username.
It would be great if you provide your controller code too. I guess you should pick user events depending on the user you found by params[:id].
Without the controller, it is a bit hard to help. But I would suggest trying something on the user controller like
def get_status_by_user
user_id = params[:user_id]
user = User.find_by(id: user_id)
user.event
end
where the params[user_id] is the id of hte user you want to get the event.
Also what I can see is <% userevent = User.find_by(params[:event]) %> you do not specify a user so the method will return the last one
Finds the first record matching the specified conditions.
Find_by docuemntaion
So if you want to keep your code mostly as it is you need to first find the user you want to get the event and then get the status for that specific user
Also giving a little information on the DB schema could help
I'm learning RoR building social network. So in my views I have a index view which is rendering a mix of all the posts from my groups.
So now, I would like to build a link to redirect toward my original post (into his group). Redirect into the group is not really a problem, but I don't know how to redirect to my post into this group.
With a code example it will be more clear :
Index view :
<%= #posts.each do |p| %>
<%= p.title %>
Publish in <%= link_to p.group.name, group_path(p.group, ROUTE TO MY POST) %>
<% end %>
Show view(group):
<%= #group.posts.each do |post|%>
<div id='post_iter<%=post.id%>'
<%= p.title%>
</div>
<% end %>
So I would like to redirect the user toward his iteration into the group Show.
Something like
<%= link_to p.group.name, group_path(p.group, #post_iter#{p.id}) %>
This should be good :
<%= link_to p.group.name, group_path(p.group, anchor: "post_iter(#{p.id})") %>
I'm sure I'm missing something simple, but I'm generating a list of projects each with a form to add a new audition to that particular project. The form isn't being generated properly.
The view:
<% #projects.group_by(&:aasm_state).each do |state, projects| %>
<div class="project-column small-order-<%= state_priority(state) %>">
<div class="project-state spaced-out-2 project-<%= state %>"><%= state.titleize %></div>
<% for project in projects %>
<%= render partial: 'auditions/partials/dash_project_audition_form', locals: {project: project} %>
<% end %>
<% end %>
This is the code in the partial:
<button data-click="new-project-audition">+ Audition for <%= project.title %></button>
<%= form_for([project, #audition]) do |f| %>
... SNIP....
<% end %>
Everything works except the form. Each form generated contains the ID for the last record in #projects. So, for example the last project is ID 19, no matter which project form I click on, they're all for 19. The problem isn't with the partial, it's with something I'm doing in form_for, because + Audition for <%= project.title %>, which is inside the partial, generates the correct title.
UPDATE:
I restarted the server and form_for is now generating the right path, with the right ID.
I have this line also inside the form_for loop, and it's still wrong.
<h3 class="section-head">New Audition for <%= project.title %></h3>
It's generating the title for the last project in the whole loop.
UPDATE 2: Stopped working on a page reload, so... not even sure what to say now.
when you do form_for([project, #audition]), it generates a form for
projects:project_id\auditions:audition_id
so the id that you see in all forms may belong to the #audition variable. Where do you initialize it ?
It should be initialized ideally on a per project basis, right ? Let me know if this helps in any way.
EDIT: SOLVED, view the solution in Brian Kunzig's answer+comments.
I've decided to try out Ruby on Rails and have been constantly running into this problem. I was searching around for problems like this but none of the solutions have done the trick for me. It seems that I constantly write a part of code that just isn't correct. So here are some code snippets:
(ignore what the code would be for, it's just to try stuff out)
My controller:
class AuthsController < ApplicationController
def index
#auths=Auth.all
end
def new
#auth=Auth.new
end
def show
#auth=Auth.find(params[:id])
end
end
My index.html.erb:
<div style="margin: 0 auto; width:50%; text-align: center">
<h1>Please authorise your use of this webpage and its database(s).</h1>
<%= form_for :auth, url: auths_path do |f| %>
<% if #auth.errors.any? %> ==> RAILS REPORTS ERROR IN CODE HERE
<div id="error_explanation">
<h2>
<%= pluralize(#auth.errors.count, "error") %> prohibited
this authentification from being saved:
</h2>
<ul>
<% #auth.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :username %><br>
<%= f.text_field :username %>
</p>
<p>
<%= f.label :password %><br>
<%= f.text_field :password %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</div>
So, bottom line. Rails is saying that I cannot use #auth in the form_for block, for example. Or anywhere else for that matter.. It always says that it belongs to NilClass or something like that. It obviously wants me to instantiate it somehow, but isn't it enough to make the method new and put in the line: #auth=Auth.new ?
I'm just confused with this situation because I can't figure out how it's supposed to go. Thanks a lot !
P.S. I'm using <%= form_for :auth, url: auths_path do |f| %> because it won't accept #auth, that's what the error in the next line is. I have seen solutions to instantiate it "on the go" outside of the controller but I want to do it the way it's supposed to be done.
You should be putting this form in the new.html.erb file and not the index. The index is for listing entries while the new and create actions handle POST requests. You're getting an error because you're trying to list all Auth's when none have been created. Also, you're sending a form via a GET request if you're using standard rails routing. Use resourceful routing and put this form in your new view for that controller and it should work.
Routes file should be:
resources :auths
This will provide all the necessary routing automagically. If you type rake routes after modifying this you will see the newly generated urls and the helpers to them. You will notice it affords the create and update actions a POST/PUT request while others are GET.
Just trying to save the values of 4 checkboxes into a column named reminders to my Tickets Model.
But, either the values aren't being saved, or when I go back to edit this "Ticket" its not ale to pull the data from the db and display the true / false vaules correctly.
Any advice?
/tickets/_form.html.erb
<%= form_for(#ticket) do |f| %>
<div>
<% [ 'S.T.A.R.T', 'E.N.D.E.D', 'URGENT' , 'Repeat Request' ].each do |reminder| %>
<br><%= check_box_tag 'reminders', reminder, (params[:reminders] || {}).include (reminder) %>
<%= reminder.humanize %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Rather than using check_box_tag the way you are, change it to use your form builder object provided by form_for. It'll ensure that the name of the checkbox is correct which is your problem (I think).
Because you're not using it, the checkbox is probably taking on a different name to what rails is expecting to see, so when you submit the form, the param name won't be associated with your record and thus won't be changed. Try using
<%= f.check_box_tag :reminder %>
Rails will automatically handle whether or not it is ticked when the page loads :)