I am a new programmer on ruby on rails, and i have problem when upload picture. Deos anyone can help me. thanks in advance.
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy]
# GET /students
# GET /students.json
def index
#students = Student.all
end
# GET /students/1
# GET /students/1.json
def show
end
# GET /students/new
def new
#student = Student.new
end
# GET /students/1/edit
def edit
end
# POST /students
# POST /students.json
def create
#student = Student.create(student_params)
respond_to do |format|
if #student.save
format.html { redirect_to #student, notice: 'Student was successfully created.' }
format.json { render :show, status: :created, location: #student }
else
format.html { render :new }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
end
private
#def student_params
#params.require(:student).permit(:image_file)
#end
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
respond_to do |format|
if #student.update(student_params)
format.html { redirect_to #student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: #student }
else
format.html { render :edit }
format.json { render json: #student.errors, status: :unprocessable_entity }
end
end
end
# DELETE /students/1
# DELETE /students/1.json
def destroy
#student.destroy
respond_to do |format|
format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
#student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:name, :gender, :telephone, :address)#, :image_file
end
end
Can't really find a problem in your post. It's a bit hard to understand. Did you follow the docs on https://github.com/thoughtbot/paperclip? I take it your file attachment is in the Student model?
params.require(:student).permit(:name, :gender, :telephone, :address)#, :image_file
now you only permit to save name, gender, telephone and address, the image file wil not be saved. You still have to permit it.
Related
I'm using rails 5.1.4 , so I have a table USER within I store all the users, then a user can send friend request to other users, and I want to store that request into other table named FRIEND, like user_1 ( who send the request) - user_2 (who is requested for) , but you will understand that those two user (user_1, user_2) are coming from USER table.
It means: first I have this table Users which contains all the users. And what I want to say is that any user in this table user can be friend with any other user from the same table user.
I'm not 100% certain what your asking in your question but it sounds like what you're looking to do is to have a table called Users and another table called Friends and to be able to store a value for the friend request. I believe what you want to look into is a "has_many :through association" using a "join table". Check out this video it walks you through it http://blog.teamtreehouse.com/what-is-a-has_many-through-association-in-ruby-on-rails-treehouse-quick-tip
here's more info on the Active Record association http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
EDIT:
Based on your below post what you want to do is just a simple has_many association. In your User model add has_many :friends, dependent: :destroy and in your Friend model add belongs_to :users now go to your routes file and make friends a nested route of user by doing
# nested routes for users
resources :users do
resources :friends
end
in your friends table make sure you add a foreign key for users lik ethis in the migration t.references :user, foreign_key: true
now in your friends_controller you need to update the actions like this. Replace the example "patent" with your "friend" object.
class PatentsController < ApplicationController
# first loads #user and then #patent for the below actions.
before_action :set_user, only: [:index, :show, :new, :edit, :create, :update]
before_action :set_patent, only: [:show, :edit, :update, :destroy]
# GET /patents
# GET /patents.json
def index
#patents = #user.patents
end
# GET /patents/1
# GET /patents/1.json
def show
end
# GET /patents/new
def new
#patent = #user.patents.new
end
# GET /patents/1/edit
def edit
end
# POST /patents
# POST /patents.json
def create
#patent = #user.patents.new(patent_params)
respond_to do |format|
if #patent.save
format.html { redirect_to user_patents_path(#patent.user), notice: 'Patent was successfully created.' }
format.json { render :show, status: :created, location: #patent }
else
format.html { render :new }
format.json { render json: #patent.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /patents/1
# PATCH/PUT /patents/1.json
def update
respond_to do |format|
if #patent.update(patent_params)
format.html { redirect_to user_patents_path(#patent.user), notice: 'Patent was successfully updated.' }
format.json { render :show, status: :ok, location: #patent }
else
format.html { render :edit }
format.json { render json: #patent.errors, status: :unprocessable_entity }
end
end
end
# DELETE /patents/1
# DELETE /patents/1.json
def destroy
#patent.destroy
respond_to do |format|
format.html { redirect_to user_url(#user), notice: 'Patent was successfully destroyed.' }
format.json { head :no_content }
end
end
#######################
# private methods
#######################
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:user_id])
end
def set_patent
#patent = Patent.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def patent_params
params.require(:patent).permit(:patent_title, :patent_office, :patent_number, :patent_status, :patent_date, :patent_description, :user_id)
end
end
A company has many properties. A property has one company.
In my routes file I got:
resources :companies do
resources :property_managers
end
In the property_manager_controller, my create action looks like this (default scaffold implementation slightly modified to accommodate the company):
def create
#property_manager = PropertyManager.new(params[:property_manager])
#property_manager.company_id = params[:company_id]
respond_to do |format|
if #property_manager.save
format.html { redirect_to company_property_managers_path, notice: 'Property manager was successfully created.' }
format.json { render json: #property_manager, status: :created, location: #property_manager }
else
format.html { render action: "new" }
format.json { render json: #property_manager.errors, status: :unprocessable_entity }
end
end
end
Is there a way in which I do not have to explicitly set the company_id, since it is known within the context of the URL/route?
I guess you could do something like the following, not sure if it's better or not:
class PropertyManagersController < ApplicationController
before_filter :find_company
def new
#property_manager = #company.property_managers.build
end
def create
#property_manager = #company.property_managers.build(params[:property_manager])
respond_to do |format|
...
end
end
private
def find_company
#company ||= Company.find(params[:company_id])
end
end
My application uses tickets and call_logs. I have these nested in each other, so that tickets can have many call_logs.
I am getting the error with [GET] "/call_logs"
I don't know what I am missing here.
I do have my routes nested in routes.rb
resources :tickets do
resources :call_logs
end
rake routes:
ticket_call_logs GET /tickets/:ticket_id/call_logs(.:format) call_logs#index
POST /tickets/:ticket_id/call_logs(.:format) call_logs#create
new_ticket_call_log GET /tickets/:ticket_id/call_logs/new(.:format) call_logs#new
edit_ticket_call_log GET /tickets/:ticket_id/call_logs/:id/edit(.:format) call_logs#edit
ticket_call_log GET /tickets/:ticket_id/call_logs/:id(.:format) call_logs#show
PUT /tickets/:ticket_id/call_logs/:id(.:format) call_logs#update
DELETE /tickets/:ticket_id/call_logs/:id(.:format) call_logs#destroy
tickets GET /tickets(.:format) tickets#index
POST /tickets(.:format) tickets#create
new_ticket GET /tickets/new(.:format) tickets#new
edit_ticket GET /tickets/:id/edit(.:format) tickets#edit
ticket GET /tickets/:id(.:format) tickets#show
PUT /tickets/:id(.:format) tickets#update
DELETE /tickets/:id(.:format) tickets#destroy
call_logs controller:
class CallLogsController < ApplicationController
before_filter :get_ticket
# GET /call_logs
# GET /call_logs.json
def index
#call_logs = #ticket.call_logs.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #call_logs }
end
# GET /call_logs/1
# GET /call_logs/1.json
def show
#call_log = #ticket.call_logs.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #call_log }
end
end
# GET /call_logs/new
# GET /call_logs/new.json
def new
#call_log = #ticket.call_logs.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #call_log }
end
end
# GET /call_logs/1/edit
def edit
#call_log = #ticket.call_logs.find(params[:id])
end
# POST /call_logs
# POST /call_logs.json
def create
#call_log = CallLog.new(params[:call_log])
respond_to do |format|
if #call_log.save
format.html { redirect_to ticket_call_logs_url(#ticket), notice: 'Call log was successfully created.' }
format.json { render json: #call_log, status: :created, location: #call_log }
else
format.html { render action: "new" }
format.json { render json: #call_log.errors, status: :unprocessable_entity }
end
end
end
# PUT /call_logs/1
# PUT /call_logs/1.json
def update
#call_log = #ticket.call_logs.find(params[:id])
respond_to do |format|
if #call_log.update_attributes(params[:call_log])
format.html { redirect_to ticket_call_logs_url(#ticket), notice: 'Call log was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #call_log.errors, status: :unprocessable_entity }
end
end
end
# DELETE /call_logs/1
# DELETE /call_logs/1.json
def destroy
#call_log = #ticket.call_log.find(params[:id])
#call_log.destroy
respond_to do |format|
format.html { redirect_to ticket_call_logs_path(#call_log)}
format.json { head :no_content }
end
end
end
end
private
def get_ticket
#ticket = Ticket.find(params[:ticket_id])
end
I believe I have an error in my url paths when directing to the call_log page, but I can't seem to find it. Any advice or tips would be appreciated, as I am a noob.
Thank you.
As you can see from the output of rake routes, You don't have a route that matches /call_logs. Since this is a nested resource route you have to prepend the tickets and ticket id: /tickets/<id>/call_logs.
Looks like you have done changes in routes.rb but not maintaining relation in Models. As rake routes say
ticket_call_logs GET /tickets/:ticket_id/call_logs(.:format)
Should be accessible by /ticket/id/call_logs , Now here "id" is ticket id. This will list all call logs for that particular ticket_id.
I have a simple Rails 3 example application that I'm trying to work out how to parse a JSON POST and insert the data into my applications database.
I have a basic Activities controller and model, the controller is as follows:
class ActivitiesController < ApplicationController
# GET /activities
# GET /activities.json
def index
#activities = Activity.order('created_at DESC').limit(10)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #activities }
end
end
# GET /activities/1
# GET /activities/1.json
def show
#activity = Activity.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #activity }
end
end
# GET /activities/new
# GET /activities/new.json
def new
#activity = Activity.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #activity }
end
end
# GET /activities/1/edit
def edit
#activity = Activity.find(params[:id])
end
# POST /activities
# POST /activities.json
def create
#activity = Activity.new(params[:activity])
respond_to do |format|
if #activity.save
format.html { redirect_to #activity, notice: 'Activity was successfully created.' }
format.json { render json: #activity, status: :created, location: #activity }
else
format.html { render action: "new" }
format.json { render json: #activity.errors, status: :unprocessable_entity }
end
end
end
# PUT /activities/1
# PUT /activities/1.json
def update
#activity = Activity.find(params[:id])
respond_to do |format|
if #activity.update_attributes(params[:activity])
format.html { redirect_to #activity, notice: 'Activity was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #activity.errors, status: :unprocessable_entity }
end
end
end
# DELETE /activities/1
# DELETE /activities/1.json
def destroy
#activity = Activity.find(params[:id])
#activity.destroy
respond_to do |format|
format.html { redirect_to activities_url }
format.json { head :no_content }
end
end
end
I have also got an example JSON payload from the application I'm trying to receive the POST from:
payload {"files":{"changed":[],"removed":[".gitmodules","extensions","extensions/markdown"]},"project":{"permalink":"dans-stuff-2","public_key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuB80S+kMN1266LvCEXoU5hmjugcuXko61Hl7uD9VVgMgEOrLSkeYktOPshgCOwjQgGhSMVEHWrSuOddOi+V3rOu3v91jiU3vycRM3nK9GMaOFf+tTn0/4ReUvGOFHeir1vluHCmOAmpqsVhGWkCJY6eJE37muvGdTG4nW/VnpWYCTLfSOeaq5MVx5LU+zYLntvB5PXd7h1k5ROO8lW+5QzGVyHHEvilzJOqESIAWpNtcXGBt7clWpFH4lg2AT7JebYFuHl8Sl4LYzIRegcpg718CWJjrck9/xV6CBTGP520ifU6K0x4JOn/qFzVlqwnxmrPXwawptmCiYiJt3X8Oz DeployHQ.com Key for dans-stuff-2","name":"Dans Stuff","repository":{"port":null,"username":null,"branch":"master","url":"git#codebasehq.com:atech/deploytest/test1.git","scm_type":"git","cached":true,"hosting_service":{"name":"Codebase","url":"http://www.codebasehq.com","commits_url":"http://atech.codebasehq.com/projects/deploytest/repositories/test1/commits/master","tree_url":"http://atech.codebasehq.com/projects/deploytest/repositories/test1/tree/master"}}},"server":{"name":"cybele","server_path":"/home/dan/deploytest","port":22,"username":"dan","use_ssh_keys":true,"last_revision":"2ad223b9dd6d006d69909407ac7df322e35b009b","hostname":"cybele.phoenixdev.co.uk","identifier":"de99437e-da4a-0986-a7ab-e55805261a96","protocol_type":"ssh"},"start_revision":{"timestamp":"2011-11-30T13:48:22+00:00","author":"Dan Wentworth","ref":"a1c1073b85d1662c075d77f5d88be3fd291abd12","message":"nested markdown pathg","email":"dan#atechmedia.com"},"identifier":"9d0f9c21-f242-3966-fcd3-e821a65acd45","end_revision":{"timestamp":"2011-08-30T16:41:32+01:00","author":"Dan Wentworth","ref":"2ad223b9dd6d006d69909407ac7df322e35b009b","message":"arses","email":"dan#atechmedia.com"},"configuration":{"email_notify":true,"notification_addresses":"dan#atechmedia.com","copy_config_files":true},"timestamps":{"completed_at":"2012-01-17T09:22:15Z","duration":null,"queued_at":"2012-01-17T09:22:07Z","started_at":"2012-01-17T09:22:10Z"},"status":"completed"} -------------------------------------------------------------------------------- signature TMjVYhk/+lQigy3hdkgTZjAaaroxAi126XaDzrQ1xWGyMji9oR0F5nfE73Mo kNs7Hk7aY1GQgtDRdBjgPE+C/6etF9nPFXPacWobaxSP35TGOrpoSACNQDAQ Q0wvn0Bm/jMvhBKoQ6YYUSx8KP8nd7VkgQmv4S0cakgixA4LHfg=
Because the POST will need to be made to a URL, I would imagine I need to define a new method:
def deploy_receive
data = JSON.parse(request.body)
end
What I am unsure of, is where to go from here. How would I cherry pick the attributes from the JSON and put them into various attributes in the Activities table?
Something like this?
def deploy_receive
data = JSON.parse(request.body)
#activity = Activity.new(params[:fault])
#activity.who = data.author
end
Any pointers would be appreciated!
If I understand correctly I think you're looking for something like what's below:
Routes File
match '/api/activity/new' => 'activity#deploy_receive', :via => [:post]
Activity Controller (assuming the POSTed data is in correct format)
def deploy_receive
#activity = Activity.new(params[:fault])
if #activity.save
render json: #activity
else
#activity = "error"
render json: #activity
end
end
If I understand it correctly; Rails will already do this for you if the right headers are set. Checkout: POST json to rails server
I'm new to Ruby on Rails, so please pardon my ignorance. My issue is that I cannot access the Rails project from the browser. I assume it has something to do with the Rails config, but I am having no luck so far.
State of the Project
I have existing logic (controllers, models, migrations, ...).
If I leave index.html in place, I see the Rails welcome page because it is apparently rendered before Rails tries to parse the URL.
If I disable index.html, I get the 'Something went wrong' message which is generated by Rails.
If I have the httpd.conf document root set to 'my.domain.com/public' I get the Rails error, but if I have it set to 'my.domain.com/', I get an error served up by Apache instead. (So that seems to be correctly configured.)
The error logs show this error: File does not exist: /var/www/html/my.domain.com/zombies when I hit the URL.
My Environment
Rails 3.2.6
Ruby 1.9.3
Apache 2.2.3
CentOS
If it matters, I'm simply following the tutorial at Rails for Zombies 2.
Thanks in advance!
Reference
zombies_controller.rb
class ZombiesController < ApplicationController
# GET /zombies
# GET /zombies.json
def index
#zombies = Zombie.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #zombies }
end
end
# GET /zombies/1
# GET /zombies/1.json
def show
#zombie = Zombie.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #zombie }
end
end
# GET /zombies/new
# GET /zombies/new.json
def new
#zombie = Zombie.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #zombie }
end
end
# GET /zombies/1/edit
def edit
#zombie = Zombie.find(params[:id])
end
# POST /zombies
# POST /zombies.json
def create
#zombie = Zombie.new(params[:zombie])
respond_to do |format|
if #zombie.save
format.html { redirect_to #zombie, notice: 'Zombie was successfully created.' }
format.json { render json: #zombie, status: :created, location: #zombie }
else
format.html { render action: "new" }
format.json { render json: #zombie.errors, status: :unprocessable_entity }
end
end
end
# PUT /zombies/1
# PUT /zombies/1.json
def update
#zombie = Zombie.find(params[:id])
respond_to do |format|
if #zombie.update_attributes(params[:zombie])
format.html { redirect_to #zombie, notice: 'Zombie was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #zombie.errors, status: :unprocessable_entity }
end
end
end
# DELETE /zombies/1
# DELETE /zombies/1.json
def destroy
#zombie = Zombie.find(params[:id])
#zombie.destroy
respond_to do |format|
format.html { redirect_to zombies_url }
format.json { head :no_content }
end
end
end
Running rake assets:precompile seems to have fixed my issue. Thanks all.