I want to fetch data from client side to ruby on rails by using post method in create action in my controller.But i dont know how to do for getting data from client side in def create action.How to create the api for to get data from client side.
Postcontroller.rb
class PostsController < ApplicationController
respond_to :json, :xml
before_filter :load
def load
if signed_in?
@my_posts = current_user.posts.paginate(page: params[:page],:per_page => 10)
@my_post = current_user.posts.new
end
@posts = Post.paginate(page: params[:page],:per_page => 10)
@post = Post.new
end
def index
@posts = Post.all
respond_with(@posts) do |format|
format.json { render json: @post_names = {:post => @posts.as_json(:only=> :content)} }
end
end
def show
if signed_in?
@post = Post.find(params[:id])
@my_posts = current_user.posts.paginate(page: params[:page],:per_page => 10)
current_user.vote_for(@post)
else
@post = Post.find(params[:id])
Guest.find(1).vote_for(@post)
end
@posts = Post.paginate(page: params[:page],:per_page => 10)
@guest = Guest.new
@user = User.new
@users = User.paginate(page: params[:page],:per_page => 10)
end
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
@posts = Post.paginate(page: params[:page],:per_page => 10)
format.json { render json: @post, status: :created }
else
@posts = Post.paginate(page: params[:page],:per_page => 10)
format.json { render json: @post.errors, status: :unprocessable_entity }
end
@guest = Guest.new
@users = User.paginate(page: params[:page],:per_page => 10)
end
end
def my_prayer_create
@my_post = current_user.posts.new(params[:post])
@post = Post.new(params[:post])
@guest = Guest.new
@user = User.new
@users = User.paginate(page: params[:page],:per_page => 10)
if @my_post.save
flash[:notice] = "Prayer Successfully created."
@my_posts = current_user.posts.paginate(page: params[:page],:per_page => 10)
else
flash[:notice] = "Error"
@my_posts = current_user.posts.paginate(page: params[:page],:per_page => 10)
end
end
def edit
if signed_in?
@my_post = current_user.posts.find(params[:id])
else
@post = Post.find(params[:id])
end
end
def update
if signed_in?
@my_post = current_user.posts.find(params[:id])
if @my_post.update_attributes(params[:post])
flash[:notice] = "Prayer Successfully updated."
@my_posts = current_user.posts.paginate(page: params[:page],:per_page => 10)
end
else
@post = Post.find(params[:id])
end
end
def destroy
if signed_in?
@my_post = current_user.posts.find(params[:id])
@users = User.paginate(page: params[:page],:per_page => 10)
@guest = Guest.new
@my_post.destroy
flash[:notice] = "Prayer Successfully destroyed."
@my_posts = current_user.posts.paginate(page: params[:page],:per_page => 10)
end
end
For my controller how can i fetch data from client side.from this only i have to write the url in my client side also like this "http://localhost:3000/posts"
You want to create a method called new
Get all the user inputs in new method and pass it to create method.
new method:
def new
@post = Post.new
end
create method:
@post = Post.new(params[:post])
params[:post] pass all params and save it by @post.save
.
Read rails doc for CURD operations first.
EDIT:
in routes.rb:
match '/action' => "controller#action"
write view file for new method to get use input in "views/controller/new.html.erb"
it seems if post saved successfully you will get created post. but if save fails then you are facing some posts
@posts = Post.paginate(page: params[:page],:per_page => 10)
but responding using :
format.json { render json: @post.errors, status: :unprocessable_entity }
So if you want to get some posts if post save fails then it has to respond_with @posts
I got solution for my question,what to use correct params in def create action
def create
@post = Post.create(params[:post])
#@post = Post.create(:content=>params[:post][":content"],:title=>params[:post][":title"])
respond_to do |format|
if @post.save
@posts = Post.paginate(page: params[:page],:per_page => 10)
format.json { render json: @post, status: :created }
else
@posts = Post.paginate(page: params[:page],:per_page => 10)
format.json { render json: @post.errors, status: :unprocessable_entity }
end
@guest = Guest.new
@users = User.paginate(page: params[:page],:per_page => 10)
end
end
the params should be same as like the client side given parameter also.
To be precise and exact .. thats the style the normal ruby on rails crud operations follow.
let me be clear..
If you want to create a record in table USERS lets say fields are "name" and "age" the params in the client side need not be same as the fields of the table.
In the above case we have handle the saving in controller,framework don't take of it. say on client side 'x' for name and 'y' for age then the params built in the client side and passed to server is say params=>{:post=>{:x=>"siva", :y => "23"}}
In the controller. user=User.new user.name=params[:post][:x] user.age=params[:post][:y] user.save
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.