i have a little problem with a display of a "delete button" i would like that only the user who have post the product can see the button delete so i have try to do a methode who compared id :
def user_publis?
#user=current_user
#publication_id = #publication.user.id
#user_id= #user.id
#publication_id = #user_id
end
a méthode who ask if the publication have build by the current_user
def user_publis?
if #publication=current_user.publications.build
content_tag( :span ,class: "btn btn-rounded") do
content_tag( "my delete button")
end
end
but no one are good....
thx for help and sorry for my english ..^^
You could just write something like this in your view:
<% if #publication.user == current_user %>
<span class="btn btn-rounded">
my delete button...
<% end %>
Or: You can add the following method to your Publication:
def published_by?(user)
self.user == user
end
and use it in your view like:
<% if #publication.published_by? current_user %>
<span class="btn btn-rounded">
my delete button...
<% end %>
Related
I am currently working on a Friendship feature. I would like to let the user select or type the name of another user in order to send a friend request.
Here is my form :
<%= form_tag users_path, method: 'get' do %>
<%= label_tag(:search, "Who are your friends ?") %>
<%= text_field_tag :search, params[:search] %>
<%= link_to user_friendsips_path(), method: :post do %>
<button class= "btn btn-user shadow"> <i class="fas fa-share-square"></i> Send Friend Request? </button>
<% end %>
<% end %>
In the link_to where there is the button I would like to pass between the bracket the user_id the person selected in the form but I don't know how to retrieve it
Here is a part of my User Model
def self.search(search)
if search
user_pseudo = User.find_by(pseudo: search)
if user_pseudo
self.where(user_id: user_pseudo)
else
#users = User.all
end
else
#users = User.all
end
end
Here is a part of my User controller with the Index
def index
#friends = current_user.friends
#pending_requests = current_user.pending_requests
#friend_requests = current_user.received_requests
#users = User.search(params[:search])
end
Thanks by advance
self.where(user_id: user_pseudo)
## I think you need to replace user_id with just id
##or Provide more details please
I have a form like this:
<div id="page_wrapper">
<p>Please insert the usernumber of the user that you want to make admin</p>
<%= form_tag admins_path do %>
<%= text_field_tag "account" %> <br/> <br/>
<%= submit_tag "Make admin" %>
<% end %>
</div>
and I want to get he userinput in the account text field into the controller. I have the naming right. In the controller I have something like this
def create
getUserNum = :account
end
def admin_params
params.require(:admin).permit(:account)
end
So I want to get the userinput from the account textfield and store it into a variable called getUserNum. But it doesn't work. What am I doing wrong ?
Thanks for your time
def create
ad_params = admin_params
getUserNum = ad_params[:account]
end
def admin_params
params.require(:admin).permit(:account)
end
I am building an application on Rails where a user creates a Test, goes to the show view for that test and fills in a form with an answer to a question. If the answer to the question matches the "correct_answer" (defined in the controller), there will be flash message that the answer was correct and a button to continue to the root_path will appear. If the answer is wrong, the flash says "Wrong answer."
My problem is that the flash message will say wrong answer even if no answer has been given. I ONLY want that message to appear AFTER the user has submitted the form. I understand why this is happening, I just am unsure about how to fix it. Here is the show view for a test:
<div class="col-md-8 col-md-push-2">
<h4>Current Score: <%= #test.score %></h4>
<br /><br />
<div class="form_group">
<%= form_tag test_path(#test), :method=> 'get' do %>
<h4>What is my first name?</h4>
<div class="form-group">
<%= text_field_tag :answer, params[:answer], class: 'form-control' %>
</div>
<% if !flash[:success] %>
<div class="form-group">
<%= submit_tag "Submit", class: "btn btn-primary" %>
</div>
<% end %>
<% end %>
</div>
<% if flash[:success] %>
<%= link_to "Continue", root_path, class: "btn btn-success pull-right" %>
<% end %>
</div>
Here is the controller for tests, which contains the offending show action:
class TestsController < ApplicationController
def index
#test = Test.new
#tests = Test.all
end
def show
#test = Test.find(params[:id])
correct_answer = "jack"
user_answer = params[:answer]
if user_answer == correct_answer
flash.now[:success] = "That is correct!"
new_score = #test.score += 1
#test.update(score: new_score)
elsif params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
end
def create
#test = Test.create(test_params)
if #test.save
redirect_to test_path(#test)
flash[:success] = "Test created"
else
flash[:danger] = "There was a problem"
render "index"
end
end
def destroy
#test = Test.find(params[:id])
if #test.destroy
flash[:success] = "Your test was removed"
redirect_to root_path
end
end
private
def test_params
params.require(:test).permit(:score, :user_id)
end
end
Is there a better way to do this? If not, can I somehow stop the flash message from appearing on the initial load? I ONLY want it to appear once the form has been submitted. Thanks in advance.
So your flash is triggered because params[:answer] is undefined and not == to jack.
You say:
My problem is that the flash message will say wrong answer even if no answer has been given.
But your logic is off for this outcome, your code says that params[:answer] is defined, which it can't be since the form is not rendered yet.
Maybe your condition should be:
elsif params[:answer].present? && params[:answer] != correct_answer
flash.now[:danger] = "Wrong answer"
end
Traditionally, the showing of the form and POST to a form are separete actions, which is why you're seeing this flash before anything even happens.
So I'm obviously confused with boolean flow because I run into a problem each time. NOTE: I am teaching myself to program, you are all my only hope in learning! I am trying to define a method that checks if a user is an admin, so that I can display certain objects in views to ONLY admins simple enough...or not, for some reason it's not recognizing that I'm an admin (when I truly am, I've checked!). With this all being said, I am a newb so go easy on me!
helpers/sessions_helper is used by both my User and Blogpost models:
def current_user #determines if user is logged out or is the current user of the session/cookie of profile
if (user_id = session[:user_id])
#current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
#current_user = user
end
end
end
def current_user?(user)
user == current_user
end
def is_an_admin
if current_user && current_user.admin?
end
end
<div class="col-xs-12">
<h1><%= #blogpost.title %></h1>
<p><%= #blogpost.content %></p>
<% if is_an_admin %>
<%= link_to "Return to blog", blogposts_path %>
<%= link_to "Edit Post", edit_blogpost_path, class: 'btn btn-primary' %> |
<%= link_to "Delete Post", blogpost_path(#blogpost),
method: :delete, data: {confirm: "Are you sure?"}, class: 'btn btn-danger' %>
<% else %>
<%= link_to "Return to blog", blogposts_path %>
<% end %>
</div>
I'm unsure if maybe I have the method in the wrong place? I have tried placing it inside my applications controller and my user controller to no avail. I must be missing something.
Your sintax got messed up, but I think you're always returning nil on the is_an_admin method:
def is_an_admin
if current_user && current_user.admin?
end
end
It does nothing if the condition is true so it's always returning nil
Change it to something like:
def is_an_admin?
current_user and current_user.admin? # or shorter: current_user.try('admin?')
end
I have a book review/discussion site and would like to order all the users books on their profile page based on wether they are currently reading that book. I added a column to the book model called current and when users add a book they can check a box that says "I'm currently reading this." If the user clicks the box then the book object has a :current attribute with a value of "1". If they don't click the box then the value of :current is "0".
In my view I have this code to tell the user which books they are currently reading, and it works fine.
<% if book.current == "1" %>
<h4>I am currently reading this book</h4>
<% end %>
However in my show user controller I can't make it order the incomiing objects by the :current status.
Here's what I've tried:
def show
#user = User.find_by_id(params[:id])
#books= Book.all.sort { |p1, p2| p1.current <=> p2.current }
end
and
def show
#user = User.find_by_id(params[:id])
#books = Book.all
#books = #books.order(:current)
end
and
def show
#user = User.find_by_id(params[:id])
#books = Book.all
#books = #books.order(current: :desc)
end
and, just to make sure it was going the right way
def show
#user = User.find_by_id(params[:id])
#books = Book.all
#books = #books.order(current: :asc)
end
None work. And, none throw errors, they just don't sort it. The "current" book that is last instead of first. Here's the code that displays and loops out the users books. This code also works fine.
<% #user.books.each do |book| %>
<% if params[:id].to_s == book.user_id.to_s %>
<ul class="profileDisplay" >
<img alt="Book Jacket" class="homepageImages" src=<%= book.image %> >
<% if book.current == "1" %>
<h4>I am currently reading this book</h4>
<% end %>
<p><b>Contributor: </b><%= book.user.name %></p>
<p><b>Title: </b><%= book.title %></p>
<p><b>Author: </b><%= book.author %></p>
<p><b>Genre: </b><%= book.genre %></p>
<p><b>Publication Date: </b><%= book.publication_date %></p>
<p><b>Publisher: </b><%= book.publisher %></p>
<p><b>Synopsis: </b><%= book.synopsis %></p>
<% if params[:id] == session[:user_id].to_s || params[:action] == "profile" %>
<%= button_to "Edit Book", book_edit_path(book.id), method: "get", class: "btn btn-primary col-xs-2" %>
<%= button_to "Delete Book", delete_book_path(book.id), method: :delete, data: {confirm: "Are you sure you want to delete this book and all it's reviews?"}, class: "btn btn-danger col-xs-2" %>
<% end %>
</ul>
<% end %>
<% end %>
On view, #user.books are not sorted yet.
You sorted them and assigned it into #books
I think it should be like this
CONTROLLER
def show
#user = User.find_by_id(params[:id])
#books = #user.books.order(current: :desc)
end
VIEW
<% #books.each do |book| %>
<!-- show book records here -->
<% end %>
add default scope in book model
default_scope { order(:current=> :desc) }
and your controller
#user = User.find_by_id(params[:id])
and views
<% #user.books.each do |book| %>
#do whatever
<% end %>