Im trying to make an app with Rails 4.
I have a projects model and profiles model.
My objective is to make the name of the project creator a link to the profile page of the user who made the project.
In my projects view, I have:
<%= link_to '
<%= #creator_profile.title %> <%= "#{#creator.first_name} #{#creator.last_name}" %>
', #creator_profile_path %> </span>
In my projects controller, I have defined creator as the user who creates the project (so the link doesnt go to the user profile that is logged in):
def show
#authorise #project
#project = Project.find(params[:id])
#creator = User.find(#project.creator_id)
#creator_profile = #creator.profile
end
My attempt is incorrect, but I can't figure out how to link these. Can anyone see what I've done wrong?
<%= link_to "#{#creator_profile.title} #{#creator.first_name} #{#creator.last_name}", #creator_profile %>
You just want one string interpolation. You don't need the extra erb tags as you already have them on the link_to. The alternative way would be to pass a block to link_to
<%= link_to #creator_profile do %>
<%= "#{#creator_profile.title} #{#creator.first_name} #{#creator.last_name}" %>
<% end %>
I assume you have routes setup so that rails can correctly "guess" the profile link based of the profile object.
Related
I am building a simple Rails app that has Users, Blogs, and Comments. The last thing I have to do is make a link on the blogs show page where comments show and have those links send the user to the comment/:id/edit page where they can edit their comment.
However, I'm not able to grab the comment ID correctly for some reason and even though I am getting thrown no errors I am not brought to the error form, just a blank web page that has my layout elements.
Here is my blogs controller's show action:
def show
#blog = Blog.find_by_id(params[:id])
#comment = Comment.new
# #current_comment = Comment.find(params[:id])
end
Here is my comments controller's edit action:
def edit
#comment = Comment.find_by_id(params[:id])
end
And here is where the link is in the Blogs show view:
<% #blog.comments.each do |c| %>
<div><%= c.content %></div>
<%= link_to 'Delete', "/comments/#{c.id}", method: :delete %>
<% end %>
The delete function works, but when I try to grab the comment id in the same way for the link to the comment view it does not work. Instead of seeing a number in the url like 1, 2, or whatever the comment id is, I see the #{c.id} syntax. Any thoughts?
<% #blog.comments.each do |c| %>
<div>
<%= link_to c.content, edit_comment_path(c) %>
</div>
<%= link_to 'Delete', c, method: :delete %>
<% end %>
In general you want to avoid hardcoding paths when possible as it makes your code more brittle and verbose.
As you have written it "/comments/#{c.id}/edit" is not even being evaluated since its not inside a ERB tag.
I suppose you mean this part:
<div><%= c.content %></div>
That's because the <div><a href="/comments/#{c.id}/edit"> part is outside a ERB block, therefore it is being interpreted as plain HTML, and that's what you see in the url. There's no variable expansion in plain HTML inside a ERB file.
Either use link_to:
<div><%= link_to c.content, "/comments/#{c.id}/edit" %></div>
or enclose the url part in ERB tags:
<div><%= c.content %></div>
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) %>
I have the below line in the user dropdown menu in the header of my app.
<%= link_to "profile (#{user.notifications.count})", current_user %>
This should show profile (3) if the user has three notifications. I want to color the profile a different color from the (3).
Is the best way to do this to give the two different parts different classes? If so, how can I go about doing that?
You can use a do block:
<%= link_to current_user do %>
profile (<span class='notifications_count'><%= user.notifications.count %></span>)
<% end %>
This will put a span with html class '.notifications_count' inside the <a></a> tag.
A quick way to accomplish that is to use span, like so
<%= link_to raw("<span style='color: #000'>profile</span> (#{user.notifications.count})"), current_user %>
or if you don't want to insert inline CSS, like so
<%= link_to raw("<span class='your_profile_class'>profile</span> (#{user.notifications.count})"), current_user %>
At the moment, I have a list of projects in the projects/index view. What the user currently has to do is click 'Show' on the project, then click 'Select Project'. This calls a custom action I've created in the controller, which passes the id of the project into the session, so only relevant tasks etc. are shown in the following pages.
What I want to happen is to have a dropdown menu on the index view, with a list of all the projects. Then, when the submit button is clicked, it will pass the id of that project into the session, exactly the same. I've tried every way I can think of doing this, but I can't get anything to work - mainly because it appears as if the id of the project isn't getting passed from the dropdown.
My question is - how can I get the submit button to call a custom action that will take the id from the dropdown menu's project and pass that into the session?
I don't know if I need to add the code to the index action of the controller, or whether the submit button can call the custom action. I'm pretty new to rails, so the more people can spell stuff out, the better!!
Here's the projects/index:
<%= form_for(#project) do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% #project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.hidden_field :company_id, :value => session[:company_id] %>
</div>
<div class="field">
<%= collection_select :project, :id, Project.all, :id, :name %>
</div>
<div class="actions">
<%= f.submit 'Select Project', :class => "btn btn-primary" %>
</div>
<% end %>
The controller code so far:
def index
#projects = Project.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #projects }
end
end
def select_project
project = Project.find(params[:id])
session[:project_id] = project.id
redirect_to root_url, notice: "Current project set to: #{project.name}, ID: #{project.id}"
end
I can't put
#project = Project.find(params[:id])
into the index action, otherwise it says that it can't find a project without an id.
When you are submitting a form this will be done through the either the update or create action in your controller.
May I ask why you are loading your dropdown from session? since you could just use the rails helpers to find (filtered) records using :where etc.
Once you get an array returned as you can list them in your dropdown as option attributes. and submit them to your update or create action. once you are there you can take value's from the submitted hash using
params[:key][:nested_key]
or just
params[:key]
While you are doing this here you are even able to bind these to sessions or variables for later use.
Just try to make the flow as robust and easy as you can.
Have fun
I have on my holder (think of it as a relationships table holding all the questions that belong to a certain holder and the holder specific data like name) show page a list of questions and a link to New Question link.
<% #questions.each do |question| %>
<%= question.question %>
<%= question.answer %>
<%= link_to "Edit Question", edit_question_path(question) %>
<br />
<% end %>
<%= link_to "New Question", new_question_path %>
The goal of this is to set the holder_id when the new question is created. I have the belongs_to and has_many setup in the model if that matters.
I've tried a few different things such as <%= link_to "New Question", new_question_path(#holder) %> but that just sets the format: to the #holder.id. I suppose, I could abuse that to make it work, but that's a very ugly hack.
I've heard people over use nested routes, and not to go more than 2 or 3 deep. So there's got to be a way to do this without using nested routes.
Am I correct in that there is a way to do this without using nested routes? What is it?
Why would nested routes be a good or bad idea for this example?
I suppose you can do something like this.
In your link to new:
<%= link_to "New Question", new_question_path(:holder_id => #holder.id)
This link will make your request send holder_id as a parameter to the new action in your question_controller.rb. So you can use it like this:
def new
holder_id = params[:holder_id]
# Do something with this id
# ...
end