how to display a link to individual microposts? (ruby on rails 3)
You should add a new route to your routes.rb file like the following:
match '/users/:username/:id', to 'microposts#show', via: :get, as: :user_micropost
and on the page that shows user's microposts, add the link as:
<a href="<%= user_micropost_path(username: @user.username, id: micropost.id) %>">Whatever..</a>
On the microposts controller, add the method show:
def show
@user = User.find_by_username(params[:username])
@post = Post.find_by_id(params[:id])
# handle any errors from the code above
end
and create under
app/views/microposts/show.html.erb
the new page that will display the micropost.
Rails 3: Link that puts user information a specific section of to the current page, without refresh?
This is a good task for a bit of javascript or jQuery. It looks like you're new to Rails so I'll start with the js and css inline, and you can move it to the correct locations once you understand how it works.
Start by getting the content you want in your comments_snippet
div so the page looks like you want it to look after the link is clicked. Then change comments_feed
:
<div class="comments_feed" id="comments" style="display: none;">
Refresh the page, and verify the div doesn't show up. Then at the top of your view add a bit of javascript to show:
<script type="text/javascript">
function show() {
document.getElementById("comments").style.display = "block";
}
</script>
Then add a link to call it:
<%= link_to "Show comments", "javascript:void(0)", onclick: "show()" %>
In a real app you might use a js library like jQuery, or css transitions, for a smooth show/hide animation. You should also keep your styles and js in separate files so they can be compiled by the Rails asset pipeline. I think that's covered later in the microposts tutorial.
how to display individual listings? Ruby on Rails
Here is what I did to get it to work (with the help of the following stackoverflow post how to display a link to individual microposts? (ruby on rails 3))
Routes rb.
match '/users/:id/:id', to 'listings#show', via: :get, as: :user_listing
users view file
add the link
<li><%= link_to "Show", user_listing_path(id: @user.id, id: listing.id) %></li>
Changed my listing_controller file
def show
@user = User.find_by_id(params[:id])
@listing = Listing.find_by_id(params[:id])
end
link_to post ID in Rails
Long answer
Rails generates a number of handy named routes for you when you add routes to your routes.rb file. Usually when in doubt for routes I take a look at my rake routes task which shows you a list of all available routes. Try running rake routes > routes.txt
and open up the corresponding routes.txt file.
The resulting file will list out a series of requests for you, in your case you should see something similar to this for your microposts controller:
POST /microposts(.:format) microposts#create
micropost GET /microposts/:id(.:format) microposts#show
DELETE /microposts/:id(.:format) microposts#destroy
Rake routes produces the following information for each of your routes (where applicable):
- The route name (if any)
- The HTTP verb used (if the route doesn’t respond to all verbs)
- The URL pattern to match
- The routing parameters for the route
With that information in mind be can simply look at the urls provided in the routes.txt file for the url we're trying to get to (/microposts/25). You'll see that the listed /microposts/:id(.:format)
url pattern handles that perfectly. Lo and behold it also maps to the microposts#show action that you want so now to get the named route just look at the first column to appear and you'll see the "microposts" keyword. Simply add _path` to this and you'll have your named route usable in views to generate link urls. Since this particular route requires an id parameter (as detailed in the url pattern) you have to pass the named route helper and id argument as well.
Also in your routes.rb file when you add resources :something
it generates routes for each of the default seven RESTful routes (new, create, edit, update, delete, index, show). In your case you're explicitly telling rails to generate default routes for the actions create, destroy and show so you can erase the line at the bottom
because that's already being handled.
match "/microposts/:id" => "microposts#show"
Short answer
Change this:
<%= link_to feed_item.title, @micropost %>
To this:
<%= link_to feed_item.title, micropost_path(feed_item) %>
See Ruby on Rails Guides: Rails routing from the Outside In for all you need to know about routes.
How to link to users/id/microposts in Ruby on Rails?
In routes.rb, you should have something like this:
match '/users/:id/microposts' => 'microposts#index_by_user', :as => :microposts_by_user
and in view, you can do like this:
<%= link_to "Microposts", microposts_by_user_path( @user) %>
Making links an active URL?
You're just showing the link. If you want to make a link:
<td><%= link_to('link', url_for(micropost.content)) %></td>
Displaying only unique values and many associated values on a html page
This will get one value for each competition name:
@entries.reduce({}){|m,e| m[e.competition.name] = e.competition; m}.values
You can do likewise for full.name
Rails 3 / nested routes: Problem to redirect to the correct url for a collection of microposts in a user show page
You need to pass in @micropost as well:
<%= link_to "show this micropost", user_micropost_url(@user, @micropost) %> #link for every microposts displayed
You could also shorten it to:
<%= link_to "show this micropost", [@user, @micropost] %> #link for every microposts displayed
Related Topics
Writing a Pre-Push Hook in Git to Grep All Files for Regex Want to Reject Push If Regex Not Found
How to Sort a Ruby Array by Two Conditions
How to Verify Pacts Against an API That Requires an Auth Token
Rspec Error 'Report_Activate_Error': Could Not Find Rubygem Rspec-Core (>=0) (Gem:Loaderror)
Does Ruby Provide a Constant_Added Hook Method
Duplicate Elements of Array in Ruby
Rails 4 Order by Virtual Attribute
Gem::Loaderror: When Trying to Deploy on Heroku - Rails 4
No Route Found in Rspec Test for an Existing Route
How to Condense Summable Metrics to a Unique Identifier in a Ruby Table
Instagram Ruby Gem - Unable to Reach Callback Url
No Such File to Load -- Bundler/Setup in Rails 3.2 with Passenger
How to Use 'Debugger' and 'Pry' When Developing a Gem? (Ruby)
Enter & Ioerror: Byte Oriented Read for Character Buffered Io
Certificate to Pem to Certificate Not Working: Nested Asn1 Error
Render Erb from Database into View Problem Please Help!
Selenium Webdriver & Chrome Driver - Not Able to Run Chrome Driver