I have a simple system in which photos have many comments.
I'm sure I'm not doing this right, but I am trying to build a simple rating system for the comments. comment.rating starts at 0 and can go up.
This is a portion of my Comments controller
class CommentsController < ApplicationController
def increment
#comment = Comment.find(params[:id])
#comment.rating += 1
redirect_to(#photo)
end
end
I think the increment method is fine, but how I'm calling it is not:
<%= link_to "+", :controller => 'comments', :method => 'increment' %>
That doesn't work. I realize this is a bit of a fundamental question, but I'd appreciate any advice. Thanks.
I believe you are not passing a comment :id to params.
You could do so by defining a route like this in your routes.rb file:
match 'increment_rating/:comment_id' => 'Comments#increment', :as => 'increment_rating'
with your increment action now beginning with
def increment
#comment = Comment.find(params[:comment_id])
...
end
and then call it from the view with
<%= link_to '+', increment_rating_path(comment.id) %>
where comment.id gives the id of the comment whose rating you want to increment
<%= link_to "+" , :controller => "comments", :action => "increment", :id => #comment %>
<%= link_to "+" , :controller => "comments", :action => "increment", :id => comment.id %>
Keep in mind usually such action would use POST or PUT, in which case you need to specify :method with either :post or :put.
If you have added the increment in routes, you could also use:
<%= link_to "+", increment_comment_path(comment.id) %>
Related
I have a model group -> has_many :questions and a question -> has_many :votes
In my my view show, for the controller groups, I have a partial that can display the list of the questions :
<%= render :partial => 'question', :collection => #group.questions.byvote %>
Then I have a link to vote with a style like this :
<%= link_to(question_votes_path(question), :controller => :vote, :action => :create, :method => :post, :id => 'vote') do %>
<%= image_tag("voteUp.png")%>
<%= question.votes_count%>
<%end%>
What I would like to do is make a condition, like :
<% if canvote?%>
… and change the style of the div.
BUT. Making a condition in the group controller can't be made because I need to make my request about the question and not about the group :
Vote.where(:user_id => current_user.id, :question_id => #question.id).first
How can I make it form the other controller group or tell an helper_method in the question controller ?
I think you should use a model method - you can then call it right in the view and do so unobtrusively. The following method will return true if the user has voted on a given question:
User.rb
def voted?(question)
#returns true if vote exists
Vote.exists?(:user_id => current_user.id, :question_id => question.id)
end
You can use it like this:
_question.html.erb
<% if current_user.voted?(#question) %>
#code to use if the user has voted on this question
<% else %>
#code to use if the user has NOT voted on this question
<% end %>
I want to search and return an index of Users based on their Profile data using Ransack in my Rails 3 app. In the docs it mentions nested attributes but since I'm a beginner I'm not sure how to use the helpers to accomplish what I want.
The ransack docs recommend the following:
users_controller.rb:
def index
#q = User.search(params[:q])
#users = #q.result(:distinct => true)
end
def search
index
render :index
end
routes.rb:
resources :users do
collection do
match 'search' => 'users#search', :via => [:get, :post], :as => :search
end
end
app/views/users/index.html.erb:
<%= search_form_for #q, :url => search_users_path,
:html => {:method => :post} do |f| %>
<% end %>
In the form I've tried to weave in the profile by using:
<%= current_user.profile.high_school %>
<%= f.text_field :profile_high_school_cont %>
Doing this doesn't work, however. The results show nothing when I search for a field belonging to at least one User's profile.
Actually, this does work. I had a f.check_box that was messing things up.
I have list of images and for every image I have link for deleting. That link looks this:
<%= link_to 'delete image', {:controller => 'shops', :action => 'delimg', :imgid => u.id}, :confirm => 'Really?' %>
def logo
puts params[:imgid]
...
end
And I am getting an error Couldn't find Shop with ID=logo and app/controllers/shops_controller.rb:17:in `show' - I tried to add puts 'IN SHOW and it really looks, that after click on that link is called method show. I have no idea, how it is possible...
Could someone help me, please, where is the problem?
This is probably how I would have done it:
#routes.rb
resources :shops do
delete :delimg, :on => :member
end
By adding that, there will be a defined route to the delimg action mapped to the delete method. And that makes it possible to do the following in the view:
<%= link_to 'delete image', delimg_shop_path(u.id), :method => :delete %>
delimg_shop_path is a path helper that exists because of what was added in the routes.rb
you are displaying params[:ingid] in the logo method but in the link to action u have specified delimg??
modiefy your link to as
<%= link_to 'delete image', {:controller => 'shops', :action => 'logo', :imgid => u.id}, :confirm => 'Really?' %>
then it will work
I have the following singular route:
scope '/seller' do
resource :seller_profile, :path => "/profile", :only => [:show, :edit, :update]
end
and the following controller:
class SellerProfilesController < ApplicationController
before_filter :validate_user_as_seller
def show
#seller_profile = current_user.seller_profile
end
def edit
#seller_profile = current_user.seller_profile
end
def update
#seller_profile = current_user.seller_profile
if #seller_profile.update_attributes(params[:seller_profile])
redirect_to(seller_profile_path, :notice => 'Profile was successfully updated.')
else
render :action => "edit"
end
end
end
I use a singular route given that the user must be authenticated before gaining access to the controller and therefore I can get the seller_profile from the user logged in.
This works like a charm, with only one problem. When I edit the seller_profile and validation error happen, the form is edited again and the errors are displayed correctly. The problem is that rails appends to the url the id of the edited record. For instance,
when I first edit the record, the url is:
http://0.0.0.0:3000/seller/profile/edit
but if the form is submitted with validation errors, the form itself is redisplayed under
http://0.0.0.0:3000/seller/profile.2
where 2 is the ID of the record being edited.
The form is the following:
<%= simple_form_for #seller_profile do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>
Everything, as said, works great but I would totally mask the ID in the url. What should I do?
I have not really worked too much with simple_form_for. But it looks like it is guessing your url always as if they were not single resources. You can provide a custom one:
<%= simple_form_for #seller_profile, :url => seller_profile_path do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>
I am a little confused on how to set the route for a custom action. I have the following link in my view:
<%= link_to 'mark done', finish_task_path(task.id), :method => :post %>
In my tasks_controller I have:
def finish
#task = Task.find(params[:id])
new = {:status => "done"}
#task.update_attributes(new)
redirect_to :action => "index"
end
In my routes file I have:
match '/tasks/:id/finish', :to => 'tasks#finish'
I have also tried the following in my view:
<%= link_to 'mark done', finish_task_path(task.id), :method => :post %>
Which also has not worked. How do I set the route correctly?
You've created a route, but it is not named. Does this work?
match '/tasks/:id/finish', :to => 'tasks#finish', :as => 'finish_task'
Have a look at the output of rake routes to ensure your routes are being declared as you want.