I have been trying to add followers to microposts with the following controller:
microposts_controller.rb :
def follow
#user = current_user
set_micropost
if #micropost.user_id != current_user.id
#micropost.followed += 1
#micropost.save
end
end
The thing is there is always a 'missing parameter' error 'microposts'. Meaning I am not passing a #micropost to the controller. The
<%= link_to 'Follow' uses micropost_new_path(micropost), method: 'follow' %>
So I don't know what I'm doing wrong. Defined the appropriate routes (the error states it is a microposts#create error, and I'm not sure why.
There's likely a few things that aren't quite right here, but the first one that stands out is that it looks like you've misunderstood what the method option for link_to is for. Here, "method" doesn't mean the name of the method your controller, it means the HTTP method to use for the request (such as POST, PUT, DELETE and and so on.) So it's likely this link is unintentionally calling the create action instead of the follow action.
For adding additional actions take a look at the section in the routing guide on Adding More RESTful Actions
As a brief example for creating a method to add followers:
If you have an micropost defined as a resource in your routes:
resources :microposts
and would like a new action (such as "follow") that applies to individual microposts you can update your resource to be:
resources :microposts do
post 'follow', on: :member
end
You can then add a follow method in your microposts_controller.rb, similar how you've done already. After editing your routes this will also provide a follow_micropost_path helper function that can be used to link to this action.
Related
I have a simple controller "Users" which has a show action (for each user). Now, I want to have a textbox on the home page which allows to search for a specific user by username.
So on show action, I accept a username param and I respond with the user object. I've seen form_for() but it seems I need an instance of #user which I don't quite understand, since I am only just retrieving on the next request. I've seen tutorials but they seem to have it under index controller action but I really want it to point to users#show so it will use the assets, template, url path, etc.
So I want a form that would just perform a get request with /users/. Am I just missing something simple?
You don't need instance if you use form_tag. You can read more about form_tag here
Example:
<%= form_tag("/search", method: :get) do %>
<%= text_field_tag :user %>
<%= submit_tag "Search" %>
<% end %>
The html snippet from #Mihail well help you but the example given will require you to create a custom route for the search action.
Add in your routes:
GET '/search', to: 'users#show'
Then in your users show action attempt to look up the user by calling:
#user = User.find_by(id: params[:user])
Be sure to not use User.find, because you will get an error when the user doesn't exist. The last thing you'll need to do is make sure your users/show template is able to handle the case when #user is nil, or instead render some sort of custom user_not_found page instead of the users/show if #user is nil.
You can use form_tag instead of form_for. Also, all the form helpers have their _tag equivalents. I.e. select_tag instead of f.select and so on. And don't forget the case when user could not be found, You have to deal with it somehow.
I'll just add in what I did.
As advised, I've added a route for /search on config/routes.rb.
get '/search', to: 'users#search'
Then on UsersController, I've added a redirect on search action.
def search
#username = params[:search]
redirect_to "#{users_path}/#{#username}"
end
This would now allow me to reuse whatever I have under users#show (templates, url and everything). From what I understand, this will return a 301/302 that would cause another HTTP request so this is not at all optimal. It would probably be cleaner to just share functionality between show and search.
I am trying to create a friendship and I created a custom action called accept. however i can't reach it. Everytime I call it i get the action show could not be found.
Here my route.rb file
resources :friendships do
collection do
delete 'cancel'
get 'accept'
end
end
Here how i call it
<%= link_to 'Accept', accept_friendships_path(:friend_id => f) %>
accept_friendships was taken from rake routes commands. And here how i define my accept controller
#Accept friendships
def accept
if #customer.requested_friends.include?(#friend)
Friendship.accept(#customer, #friend)
flash[:notice] = "Friendship Accepted"
else
flash[:notice] = "No Friendship request"
end
redirect_to root_url
end
Here the error
Unknown action
The action 'show' could not be found for FriendshipsController
Maybe I am wrong, but why you want "accept" to be a collection? I guess you want it to be a member, since you are passing friend_id. If you change it to member and make the path accept_friendship_path(#friendship) [ note singular form of friendship ], you might have better luck. Beside an addition argument your case does not differ from example on Ruby on Rails Guides, that is why it is worth to try it
Forgive me if this is a very newby question.
how can i call a method (sharing an object on facebook) when user clicked on the share button in the view.
I can do the share/facebook parts, i just don't know how to call a method from the model when the user clicks on a button
my_controller.rb
def do_something
...
end
routes.rb
get "/something" => "my_controller#do_something", :as => :do_something
#you can also use post, put, delete or match instead of get
view
<%= link_to "call do something", do_something_path %>
for post etc...
<%= link_to "call do something", do_something_path, method: :post %>
I see two potential answers depending on your specific needs.
If you want to add a method to your model outside of the columns you've created for your object you can do so in the model.rb file:
model.rb
def name_twice
"#{self.name}#{self.name}"
end
You can then take an instance of a model to call this: "#model.name_twice".
If you want to add another routed method in the controller, you can define it in your models_controller file:
models_controller.rb
def approve
#model = Model.find_by_id(params[:model_id])
model.toggle!(:approved)
redirect_to #model
end
In order for the new controller function to work, you must add it in the routes file:
routes.rb
resources :models do
get 'approve', :on => :member
end
Hope this might be a little helpful. These examples should give you an idea of how to add other methods/actions to a model/controller.
I have a really hard time understanding routes and I hope someone can help me.
Here's my custom controller
class SettingsController < ApplicationController
before_filter :authenticate_user!
def edit
#user = current_user
end
def update
#user = User.find(current_user.id)
if #user.update_attributes(params[:user])
# Sign in the user bypassing validation in case his password changed
sign_in #user, :bypass => true
redirect_to root_path
else
render "edit"
end
end
end
and I have the file settings/edit.html.erb and my link
<li><%= link_to('Settings', edit_settings_path) %></li>
The route
get "settings/edit"
doesn't work for this, because then I get
undefined local variable or method `edit_settings_path' for #<#<Class:0x00000001814ad8>:0x00000002b40a80>
What route do I have to give this? I can't figure it out. If I put "/settings/edit" instead of a path it messes up as soon as I'm on a other resource page because the resource name is put BEFORE settings/edit
Thx
Following should do:
get 'settings/edit' => 'settings#edit', :as => :edit_settings
# you can change put to post as you see fit
put 'settings/edit' => 'settings#update'
If you use /settings/edit directly in link, you shouldn't have problem with other resource name being prepended in path. However, without the leading slash, i.e. settings/edit it might have that issue.
Reason why edit_settings_path is not working might be because you didn't declare a named route. You have to use :as option to define by which method you will be generating this path/url.
If you want to explicitly define the route, you would use something like
get 'settings/edit' => 'settings#edit', :as => edit_settings
This statement defines that when a GET request is received for setting/edit, call the SettingsController#edit method, and that views can reference this link using 'edit_settings_path'.
Take some time to read the Rails guide on routing. It explains routing better than any other reference out there.
Also keep in mind the rake routes task, that lists the details of all the routes defined in your application.
redirect_to :controller=>'groups',:action=>'invite'
but I got error because redirect_to send GET method I want to change this method to 'POST' there is no :method option in redirect_to what will I do ? Can I do this without redirect_to.
Edit:
I have this in groups/invite.html.erb
<%= link_to "Send invite", group_members_path(:group_member=>{:user_id=>friendship.friend.id, :group_id=>#group.id,:sender_id=>current_user.id,:status=>"requested"}), :method => :post %>
This link call create action in group_members controller,and after create action performed I want to show groups/invite.html.erb with group_id(I mean after click 'send invite' group_members will be created and then the current page will be shown) like this:
redirect_to :controller=>'groups',:action=>'invite',:group_id=>#group_member.group_id
After redirect_to request this with GET method, it calls show action in group and take invite as id and give this error
Couldn't find Group with ID=invite
My invite action in group
def invite
#friendships = current_user.friendships.find(:all,:conditions=>"status='accepted'")
#requested_friendships=current_user.requested_friendships.find(:all,:conditions=>"status='accepted'")
#group=Group.find(params[:group_id])
end
The solution is I have to redirect this with POST method but I couldn't find a way.
Ugly solution: I solved this problem which I don't prefer. I still wait if you have solution in fair way.
My solution is add route for invite to get rid of 'Couldn't find Group with ID=invite' error.
in routes.rb
map.connect "/invite",:controller=>'groups',:action=>'invite'
in create action
redirect_to "/invite?group_id=#{#group_member.group_id}"
I call this solution in may language 'amele yontemi' in english 'manual worker method' (I think).
The answer is that you cannot do a POST using a redirect_to.
This is because what redirect_to does is just send an HTTP 30x redirect header to the browser which in turn GETs the destination URL, and browsers do only GETs on redirects
It sounds like you are getting tripped up by how Rails routing works. This code:
redirect_to :controller=>'groups',:action=>'invite',:group_id=>#group_member.group_id
creates a URL that looks something like /groups/invite?group_id=1.
Without the mapping in your routes.rb, the Rails router maps this to the show action, not invite. The invite part of the URL is mapped to params[:id] and when it tries to find that record in the database, it fails and you get the message you found.
If you are using RESTful routes, you already have a map.resources line that looks like this:
map.resources :groups
You need to add a custom action for invite:
map.resources :groups, :member => { :invite => :get }
Then change your reference to params[:group_id] in the #invite method to use just params[:id].
I found a semi-workaround that I needed to make this happen in Rails 3. I made a route that would call the method in that controller that requires a post call. A line in "route.rb", such as:
match '/create', :to => "content#create"
It's probably ugly but desperate times call for desperate measures. Just thought I'd share.
The idea is to make a 'redirect' while under the hood you generate a form with method :post.
I was facing the same problem and extracted the solution into the gem repost, so it is doing all that work for you, so no need to create a separate view with the form, just use the provided by gem function redirect_post() on your controller.
class MyController < ActionController::Base
...
def some_action
redirect_post('url', params: {}, options: {})
end
...
end
Should be available on rubygems.