I have a rails link_to that I want to run a post with...
<%= link_to 'Unfollow', follows_path(user_id: u.id), method: :delete, class: 'text-right btn btn-primary' %>
The route for it:
resource :follows, only: %i[create destroy]
However, when I click it runs as a get:
Started GET "/follows?user_id=1" for ::1 at 2020-05-27 00:51:00 +0100
ActionController::RoutingError (No route matches [GET] "/follows"):
I have checked multiple questions on SO on using link_toas a post method to no avail. I believe it is worth mentioning that the HTML rendered as:
<a class="text-right btn btn-primary" rel="nofollow" data-method="delete" href="/follows?user_id=8">Unfollow</a>
However, I eventually resigned to using a small form like so:
<% if !(current_user.is_following?(#user)) %>
<% #follow = Follow.new %>
<%= form_for (#follow) do |f| %>
<%= hidden_field_tag :user_id, #user.id %>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
<% else %>
<% #follow = Follow.new %>
<%= form_for (#follow), method: :delete do |f| %>
<%= hidden_field_tag :user_id, #user.id %>
<%= f.submit "Unfollow", class: "btn btn-primary" %>
<% end %>
<% end %>
Is it no longer possible to use post methods with link_to or has the syntax changed?
put follow_path with singular not follows
<%= link_to 'Unfollow', follow_path(user_id: u.id), method: :delete, class: 'text-right btn btn-primary' %>
Related
I am refactoring my application so that users are now managed in a dedicated Administration namespace. I did the following:
routes.rb
namespace :administration do
get 'users', to: "/administration/users#index"
resources :users
resources :groups
end
The Users and Groups controller are now in the app/controllers/administration folder. They are defined as Administration::UsersController and Administration::GroupsController. The models are not namespaced.
The related links are now based on administration_users_path and administration_groups_path.
I use some partials for hamonised layout. One of them diplays Edit and Delete buttons, where this_object represents the instance passed to the locals:
_object_actions.erb
<% if this_object.is_active %>
<%= link_to [ :edit, this_object], method: :get, class: "mat-flat-button mat-button-base mat-primary" do%>
<span class="fa fa-edit"></span>
<%= t("Edit") %>
<% end %>
<%= link_to this_object, data: { confirm: t("Sure") }, method: :delete, class: "mat-flat-button mat-button-base mat-warn" do%>
<span class="fa fa-trash"></span>
<%= t("Destroy") %>
<% end %>
<% else %>
<%= link_to [ :activate, this_object], method: :post, class: "mat-stroked-button mat-button-base" do%>
<span class="fa fa-trash-restore"></span>
<%= t("Recall") %>
<% end %>
<% end %>
But coming to to Users and Groups, the path to actions is not properly built. I would need to build the URL such as /administration/users/1/edit, but it actually is /users/1/edit.
How can I make sure to build the correct URL for this purpose?
Thanks to this post Rails using link to with namespaced routes, I undestood that it is possible to add more parameters to the link_to method to build the actual link.
To call namespaced controller methods, I added the following to the links:
<%# Check if the parent is a module defined as Namespace. If not (Object) then display default link %>
<% namespace = controller.class.parent.name == 'Object' ?
nil :
controller.class.parent.name.downcase %>
<% if this_object.is_active %>
<%= link_to [ :edit, namespace, this_object],
method: :get,
class: "mat-flat-button mat-button-base mat-primary" do %>
<span class="fa fa-edit"></span>
<%= t("Edit") %>
<% end %>
<%= link_to [namespace, this_object],
data: { confirm: t("Sure") },
method: :delete,
class: "mat-flat-button mat-button-base mat-warn" do %>
<span class="fa fa-trash"></span>
<%= t("Destroy") %>
<% end %>
<% else %>
<%= link_to [ :activate, namespace, this_object],
method: :post,
class: "mat-stroked-button mat-button-base" do %>
<span class="fa fa-trash-restore"></span>
<%= t("Recall") %>
<% end %>
<% end %>
You can specify the desired route for each link_to
<%= link_to "Edit", edit_administration_user_path(this_object) %>
I have an if statement that displays certain link based if a user voted or not for something. However, I'll get an error if the user is not logged in since current_user will be nil. I'm using devise for authentication.
undefined method `voted_as_when_voted_for' for nil:NilClass
<% if current_user.voted_as_when_voted_for #pin %>
<%= link_to dislike_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span> <%= #pin.get_upvotes.size %>
<% end %>
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
<%= link_to pins_path, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-step-backward"></span>
Back
<% end %>
<% if user_signed_in? %>
<%= link_to "Edit", edit_pin_path, class: "btn btn-default" %>
<%= link_to "Delete", pin_path, method: :delete, data: { confirm: "Are you sure"}, class: "btn btn-default" %>
<% end %>
What can I add to that if statement to display one of the links even if the user is not logged in without repeating link_to code. Using DRY Rails methodology. (if a user is not logged in and clicks like/dislike link, it will ask him to log in since I have my routes set up like this:
before_action :authenticate_user!, except: [:index, :show]
Try this:
<% if current_user && current_user.voted_as_when_voted_for #pin %>
... code here
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
In your case:
<% if signed_in? && current_user.voted_as_when_voted_for #pin %>
...
<% end %>
Assuming you're using devise since you included the authenticate_user! method.
Hello building a reddit clone.
When i click on this link found here http://postimg.org/delete/saq41zozs/ it should go another website. In this case it's www.twitter.com.au
Error found here http://postimg.org/delete/uhlsxso1e/
Im guessing because i don't have a twitter.com route. I don't know how to set it up so it routes to twitter.com
Here's my routes.rb.
Rails.application.routes.draw do
resources :comments
devise_for :users
resources :links do
member do
put "like", to: "links#upvote"
put "dislike", to: "links#downvote"
end
resources :comments
end
root "links#index"
Here is my show.html.erb
<div class="page-header">
<h1><%= #link.title %><br> <small>Submitted by <%= #link.user.name %></small></h1>
</div>
<div class="btn-group">
<%= link_to 'Visit URL', #link.url, class: "btn btn-primary" %>
</div>
<div class="btn-group pull-right">
<%= link_to like_link_path(#link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-up"></span>
Upvote
<%= #link.get_upvotes.size %>
<% end %>
<%= link_to dislike_link_path(#link), method: :put, class: "btn btn-default btn-sm" do %>
<span class="glyphicon glyphicon-chevron-down">
Downvote
<%= #link.get_downvotes.size %>
<% end %>
</div>
<% if #link.user == current_user -%>
<div class="btn-group">
<%= link_to 'Edit', edit_link_path(#link), class: "btn btn-default" %>
<%= link_to 'Destroy', #link, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-default" %>
</div>
<% end %>
<h3 class="comments_title">
<%= #link.comments.count %> Comments
</h3>
<div id="comments">
<%= render :partial => #link.comments %>
</div>
<%= simple_form_for [#link, Comment.new] do |f| %>
<div class="field">
<%= f.text_area :body, class: "form-control" %>
</div>
<br>
<%= f.submit "Add Comment", class: "btn btn-primary" %>
<% end %>
As you are linking to an external site, as opposed to a relative url, you need to prepend your link with 'http'. Just change the link in question to:
<div class="page-header">
<h1><%= link_to #link.title, "http://#{#link.url}" %><br> <small>Submitted by <%= #link.user.name %></small></h1>
</div>
How can I add a comment list in my posts' index.html page?
It is my PostsController:
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
#comments = #post.comments.all
#comment = #post.comments.build
end
and its my posts show view:
<p id="notice"><%= notice %></p>
<p>
<h3><%= #post.name %></h3>
</p>
<p>
<%= (#post.descriptopm).html_safe %>
</p>
<%= link_to 'Edit', edit_post_path(#post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Back', posts_path, :class => "btn btn-info btn-xs" %>
<h3>Comments</h3>
<% #comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
and its my posts index view:
<h1>Listing posts</h1>
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% #posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% #post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>
the post.rb :
class Post < ActiveRecord::Base
has_many :comments
end
the comment.rb :
class Comment < ActiveRecord::Base
belongs_to :post
end
the show page show the comments well but the index cannot...
it shows the error : undefined method `comments' for #
please help me >"<
I am a newly Ruby on Rails writer
Associations
If you're using ActiveRecord association to "link" your Post and Comment model, you'll be able to call .comments on each post you have in your index
Something to note (and this lies at the core of your error) is that you will have to call the .comments method on each instance of the Post. You're currently trying to call it on an #instance variable which doesn't exist:
#app/views/posts/index.html.erb
<% #posts.each do |post| %>
<% post.comments.each do |comment| %>
<%= comment.body %>
<% end %>
<% end %>
This, of course, is only possible by using the setup you have already (has_many / belongs_to):
#app/models/post.rb
Class Post < ActiveRecord::Base
has_many :comments #-> post.comments
end
#app/models/comment.rb
Class Comment < ActiveRecord::Base
belongs_to :post #-> comment.post
end
instead of using #post in comment use post on index page. You have typo error.
<%= link_to 'Create a New Post', new_post_path, :class => "btn btn-success btn-sm" %>
<% #posts.each do |post| %>
<div class="post thumbnail">
<h3><%= post.name %></h3>
<div><%= (post.descriptopm).html_safe %></div>
<div class="bottom-bottoms">
<%= link_to 'Display', post, :class => "btn btn-info btn-xs" %>
<%= link_to 'Edit', edit_post_path(post), :class => "btn btn-info btn-xs" %>
<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-info btn-xs" %>
</div>
<h3>Comments</h3>
<% post.comments.each do |comment| %>
<div>
<strong><%= comment.user_name %></strong>
<br />
<p><%= (comment.body).html_safe %></p>
</div>
<% end %>
<%= render 'comments/form' %>
</div>
<% end %>
I'm new to Ruby on rails. Now I have a search form in my homepage. When I enter something (like abc) in the form and submit, I would like the page to call my action info in my controller search. How should I configure the routes?
The current url is ~/info/?utf8=%E2%9C%93&location=berkeley&commit=submit
<div id='search_form'>
<%= form_tag('info/',method: "get") do %>
<%= text_field_tag('location', #location, :size => 30) %>
<%= submit_tag "submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
Add the following to your routes file:
get '/info', to: "search#info", as: 'search_info'
This gives you search_info_path and you can use it in your form_for declaration as follows:
<%= form_tag(search_info_path, method: "get") do %>
<%= form_tag some_path, method: "get" do %>
<%= text_field_tag('location', #location, :size => 30) %>
<%= submit_tag "submit", class: "btn btn-large btn-primary" %>
<% end %>
You can use path helper in you form_tag that leads to your controller.
i think this is wrong ('info/',method: "get")
run rake routes for look helper`s